diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index 9f13214a920..1a02ea785d1 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -9,7 +9,7 @@ Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].each {|f| module Oneboxer extend Oneboxer::Base - Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].each do |f| + Dir["#{Rails.root}/lib/oneboxer/*_onebox.rb"].sort.each do |f| add_onebox "Oneboxer::#{Pathname.new(f).basename.to_s.gsub(/\.rb$/, '').classify}".constantize end @@ -19,9 +19,12 @@ module Oneboxer # Return a oneboxer for a given URL def self.onebox_for_url(url) - matchers.each do |regexp, oneboxer| + matchers.each do |matcher| + regexp = matcher.regexp + klass = matcher.klass + regexp = regexp.call if regexp.class == Proc - return oneboxer.new(url) if url =~ regexp + return klass.new(url) if url =~ regexp end nil end diff --git a/lib/oneboxer/base.rb b/lib/oneboxer/base.rb index 7e45daff0a9..19bbb01aaa4 100644 --- a/lib/oneboxer/base.rb +++ b/lib/oneboxer/base.rb @@ -28,22 +28,23 @@ module Oneboxer end end - module Base + class Matcher + attr_reader :regexp, :klass - def matchers - @matchers ||= {} - @matchers + def initialize(klass) + @klass = klass + @regexp = klass.regexp end + end - # Add a matcher - def add_matcher(regexp, klass) - matchers[regexp] = klass + module Base + def matchers + @matchers ||= [] end def add_onebox(klass) - matchers[klass.regexp] = klass + matchers << Matcher.new(klass) end - end end