1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

separate identification computation

This commit is contained in:
Aaron Patterson 2013-05-08 13:36:04 -07:00
parent 8038f7eb10
commit 9e323e7dab

View file

@ -102,7 +102,7 @@ module ActiveSupport
def self.build(chain, filter, kind, options, _klass) def self.build(chain, filter, kind, options, _klass)
klass = case filter klass = case filter
when Array, Symbol, String, Proc when Array, Symbol, String
Callback::Basic Callback::Basic
else else
Callback::Object Callback::Object
@ -110,7 +110,7 @@ module ActiveSupport
klass.new chain, filter, kind, options, _klass klass.new chain, filter, kind, options, _klass
end end
attr_accessor :chain, :filter, :kind, :options, :klass, :raw_filter attr_accessor :chain, :kind, :options, :klass, :raw_filter
def initialize(chain, filter, kind, options, klass) def initialize(chain, filter, kind, options, klass)
@chain, @kind, @klass = chain, kind, klass @chain, @kind, @klass = chain, kind, klass
@ -118,11 +118,15 @@ module ActiveSupport
normalize_options!(options) normalize_options!(options)
@raw_filter, @options = filter, options @raw_filter, @options = filter, options
@filter = _compile_filter(filter) @key = compute_identifier filter
@source = _compile_source(filter) @source = _compile_source(filter)
recompile_options! recompile_options!
end end
def filter
@key
end
def deprecate_per_key_option(options) def deprecate_per_key_option(options)
if options[:per_key] if options[:per_key]
raise NotImplementedError, ":per_key option is no longer supported. Use generic :if and :unless options instead." raise NotImplementedError, ":per_key option is no longer supported. Use generic :if and :unless options instead."
@ -211,6 +215,15 @@ module ActiveSupport
private private
def compute_identifier(filter)
case filter
when String, ::Proc
filter.object_id
else
filter
end
end
# Compile around filters with conditions into proxy methods # Compile around filters with conditions into proxy methods
# that contain the conditions. # that contain the conditions.
# #
@ -268,25 +281,6 @@ module ActiveSupport
method_name method_name
end end
def _compile_filter(filter)
case filter
when Array
filter.map {|f| _compile_filter(f)}
when Symbol
filter
when String
"(#{filter})"
when Proc
method_name = "_callback_#{@kind}_#{next_id}"
@klass.send(:define_method, method_name, &filter)
return method_name if filter.arity <= 0
method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ")
else
filter
end
end
# Filters support: # Filters support:
# #
# Arrays:: Used in conditions. This is used to specify # Arrays:: Used in conditions. This is used to specify
@ -315,12 +309,12 @@ module ActiveSupport
filter filter
when String when String
"(#{filter})" "(#{filter})"
when Proc when ::Proc
method_name = "_callback_#{@kind}_#{next_id}" method_name = "_callback_#{@kind}_#{next_id}"
@klass.send(:define_method, method_name, &filter) @klass.send(:define_method, method_name, &filter)
return method_name if filter.arity <= 0 return method_name if filter.arity <= 0
method_name << (filter.arity == 1 ? "(self)" : " self, Proc.new ") method_name << (filter.arity == 1 ? "(self)" : " self, ::Proc.new ")
else else
method_name = _method_name_for_object_filter(kind, filter) method_name = _method_name_for_object_filter(kind, filter)
@klass.send(:define_method, "#{method_name}_object") { filter } @klass.send(:define_method, "#{method_name}_object") { filter }