Oneboxes should use a sorted array for ordering, not a hash.

This commit is contained in:
Robin Ward 2013-03-21 11:47:01 -04:00
parent e3d30f1366
commit 9d4ecd7ef8
2 changed files with 16 additions and 12 deletions

View file

@ -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

View file

@ -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