Skip to content

Commit

Permalink
Refactor AS::Callbacks
Browse files Browse the repository at this point in the history
Extracted `__reset_runner` from `__define_runner`
And call it in proper places
  • Loading branch information
bogdan authored and lest committed Jan 3, 2012
1 parent 4e8286f commit 5b8d270
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions activesupport/lib/active_support/callbacks.rb
Expand Up @@ -378,9 +378,6 @@ def compile(key=nil, object=nil)
module ClassMethods module ClassMethods
# Generate the internal runner method called by +run_callbacks+. # Generate the internal runner method called by +run_callbacks+.
def __define_runner(symbol) #:nodoc: def __define_runner(symbol) #:nodoc:
name = __callback_runner_name(nil, symbol)
undef_method(name) if method_defined?(name)

runner_method = "_run_#{symbol}_callbacks" runner_method = "_run_#{symbol}_callbacks"
unless private_method_defined?(runner_method) unless private_method_defined?(runner_method)
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
Expand Down Expand Up @@ -408,6 +405,11 @@ def #{name}() #{str} end
object.send(name, &blk) object.send(name, &blk)
end end


def __reset_runner(symbol)
name = __callback_runner_name(nil, symbol)
undef_method(name) if method_defined?(name)
end

def __callback_runner_name(key, kind) def __callback_runner_name(key, kind)
"_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks"
end end
Expand All @@ -423,7 +425,7 @@ def __update_callbacks(name, filters = [], block = nil) #:nodoc:
([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target|
chain = target.send("_#{name}_callbacks") chain = target.send("_#{name}_callbacks")
yield target, chain.dup, type, filters, options yield target, chain.dup, type, filters, options
target.__define_runner(name) target.__reset_runner(name)
end end
end end


Expand Down Expand Up @@ -537,12 +539,12 @@ def reset_callbacks(symbol)
chain = target.send("_#{symbol}_callbacks").dup chain = target.send("_#{symbol}_callbacks").dup
callbacks.each { |c| chain.delete(c) } callbacks.each { |c| chain.delete(c) }
target.send("_#{symbol}_callbacks=", chain) target.send("_#{symbol}_callbacks=", chain)
target.__define_runner(symbol) target.__reset_runner(symbol)
end end


self.send("_#{symbol}_callbacks=", callbacks.dup.clear) self.send("_#{symbol}_callbacks=", callbacks.dup.clear)


__define_runner(symbol) __reset_runner(symbol)
end end


# Define sets of events in the object lifecycle that support callbacks. # Define sets of events in the object lifecycle that support callbacks.
Expand Down

0 comments on commit 5b8d270

Please sign in to comment.