From 9d4ecd7ef8bbb79f456908579a072a2bc2494916 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 21 Mar 2013 11:47:01 -0400 Subject: [PATCH] Oneboxes should use a sorted array for ordering, not a hash. --- lib/oneboxer.rb | 9 ++++++--- lib/oneboxer/base.rb | 19 ++++++++++--------- 2 files changed, 16 insertions(+), 12 deletions(-) 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