From b9903c398eda827729160b1653aff4e8dbba8121 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 7 May 2013 13:26:23 -0700 Subject: polymorphic comparison operator --- activesupport/lib/active_support/callbacks.rb | 35 ++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) (limited to 'activesupport/lib') diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb index 1dcacf0b12..9c330d9e2d 100644 --- a/activesupport/lib/active_support/callbacks.rb +++ b/activesupport/lib/active_support/callbacks.rb @@ -91,6 +91,28 @@ module ActiveSupport class Callback #:nodoc:# @@_callback_sequence = 0 + class Basic < Callback + def matches?(_kind, _filter) + super && @filter == _filter + end + end + + class Object < Callback + def matches?(_kind, _filter) + super && @filter.to_s.start_with?(_method_name_for_object_filter(_kind, _filter, false)) + end + end + + def self.build(chain, filter, kind, options, _klass) + klass = case filter + when Array, Symbol, String, Proc + Callback::Basic + else + Callback::Object + end + klass.new chain, filter, kind, options, _klass + end + attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter def initialize(chain, filter, kind, options, klass) @@ -133,13 +155,7 @@ module ActiveSupport end def matches?(_kind, _filter) - if @_is_object_filter && !_filter.is_a?(String) - _filter_matches = @filter.to_s.start_with?(_method_name_for_object_filter(_kind, _filter, false)) - else - _filter_matches = (@filter == _filter) - end - - @kind == _kind && _filter_matches + @kind == _kind end def duplicates?(other) @@ -273,8 +289,6 @@ module ActiveSupport # a method is created that calls the before_foo method # on the object. def _compile_filter(filter) - @_is_object_filter = false - case filter when Array filter.map {|f| _compile_filter(f)} @@ -290,7 +304,6 @@ module ActiveSupport method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") else method_name = _method_name_for_object_filter(kind, filter) - @_is_object_filter = true @klass.send(:define_method, "#{method_name}_object") { filter } _normalize_legacy_filter(kind, filter) @@ -465,7 +478,7 @@ module ActiveSupport __update_callbacks(name, filter_list, block) do |target, chain, type, filters, options| mapped ||= filters.map do |filter| - Callback.new(chain, filter, type, options.dup, self) + Callback.build(chain, filter, type, options.dup, self) end options[:prepend] ? chain.prepend(*mapped) : chain.append(*mapped) -- cgit v1.2.3