Skip to content

Commit

Permalink
separate identification computation
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed May 8, 2013
1 parent 8038f7e commit 9e323e7
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions activesupport/lib/active_support/callbacks.rb
Expand Up @@ -102,27 +102,31 @@ def duplicates?(other)

def self.build(chain, filter, kind, options, _klass)
klass = case filter
when Array, Symbol, String, Proc
when Array, Symbol, String
Callback::Basic
else
Callback::Object
end
klass.new chain, filter, kind, options, _klass
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)
@chain, @kind, @klass = chain, kind, klass
deprecate_per_key_option(options)
normalize_options!(options)

@raw_filter, @options = filter, options
@filter = _compile_filter(filter)
@key = compute_identifier filter
@source = _compile_source(filter)
recompile_options!
end

def filter
@key
end

def deprecate_per_key_option(options)
if options[:per_key]
raise NotImplementedError, ":per_key option is no longer supported. Use generic :if and :unless options instead."
Expand Down Expand Up @@ -211,6 +215,15 @@ def apply(code)

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
# that contain the conditions.
#
Expand Down Expand Up @@ -268,25 +281,6 @@ def _method_name_for_object_filter(kind, filter, append_next_id = true)
method_name
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:
#
# Arrays:: Used in conditions. This is used to specify
Expand Down Expand Up @@ -315,12 +309,12 @@ def _compile_source(filter)
filter
when String
"(#{filter})"
when Proc
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 ")
method_name << (filter.arity == 1 ? "(self)" : " self, ::Proc.new ")
else
method_name = _method_name_for_object_filter(kind, filter)
@klass.send(:define_method, "#{method_name}_object") { filter }
Expand Down

0 comments on commit 9e323e7

Please sign in to comment.