module ActiveSupport::Callbacks::ClassMethods
def __callback_runner_name(key, kind)
def __callback_runner_name(key, kind) "_run__#{self.name.hash.abs}__#{kind}__#{key.hash.abs}__callbacks" end
def __define_runner(symbol) #:nodoc:
Generate the internal runner method called by +run_callbacks+.
def __define_runner(symbol) #:nodoc: runner_method = "_run_#{symbol}_callbacks" unless private_method_defined?(runner_method) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{runner_method}(key = nil, &blk) self.class.__run_callback(key, :#{symbol}, self, &blk) end private :#{runner_method} RUBY_EVAL end end
def __reset_runner(symbol)
def __reset_runner(symbol) name = __callback_runner_name(nil, symbol) undef_method(name) if method_defined?(name) end
def __run_callback(key, kind, object, &blk) #:nodoc:
calculating which callbacks can be omitted because of per_key conditions.
If this called first time it creates a new callback method for the key,
This method calls the callback method for the given key.
def __run_callback(key, kind, object, &blk) #:nodoc: name = __callback_runner_name(key, kind) unless object.respond_to?(name, true) str = object.send("_#{kind}_callbacks").compile(key, object) class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{name}() #{str} end protected :#{name} RUBY_EVAL end object.send(name, &blk) end
def __update_callbacks(name, filters = [], block = nil) #:nodoc:
CallbackChain.
This is used internally to append, prepend and skip callbacks to the
def __update_callbacks(name, filters = [], block = nil) #:nodoc: type = filters.first.in?([:before, :after, :around]) ? filters.shift : :before options = filters.last.is_a?(Hash) ? filters.pop : {} filters.unshift(block) if block ([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target| chain = target.send("_#{name}_callbacks") yield target, chain.dup, type, filters, options target.__reset_runner(name) end end
def define_callbacks(*callbacks)
would call Audit#save.
define_callbacks :save, :scope => [:name]
A declaration like
method on which callbacks are being defined.
refers to the kind of callback (before/after/around) and +:name+ refers to the
"name" is "save". In this context +:kind+ and +:name+ have special meanings: +:kind+
#{kind}_#{name} on the given instance. In this case "kind" is "before" and
would trigger Audit#before_save instead. That's constructed by calling
define_callbacks :save, :scope => [:kind, :name]
be called. On the other hand
In the above case whenever you save an account the method Audit#before will
end
end
end
puts 'save in main'
run_callbacks :save do
def save
set_callback :save, :before, Audit.new
define_callbacks :save
include ActiveSupport::Callbacks
class Account
end
end
puts 'Audit: before_save is called'
def before_save(caller)
end
puts 'Audit: before is called'
def before(caller)
class Audit
is used as a callback.
* :scope - Indicates which methods should be executed when an object
executing all the after callbacks the stored exception is raised.
to true exception raised by given block is stored and after
the given block or a before filter raises an error. By setting this option
* :rescuable - By default, after filters are not executed if
halts the chain.
other callbacks are not executed. Defaults to "false", meaning no value
In this example, if any before validate callbacks returns +false+,
define_callbacks :validate, :terminator => "result == false"
in the result variable.
triggered. This is a string to be eval'ed. The result of the callback is available
chain, preventing following callbacks from being called and the event from being
* :terminator - Determines when a before filter will halt the callback
===== Options
define_callbacks :initialize, :save, :destroy
define_callbacks :validate
Define sets of events in the object lifecycle that support callbacks.
def define_callbacks(*callbacks) config = callbacks.last.is_a?(Hash) ? callbacks.pop : {} callbacks.each do |callback| class_attribute "_#{callback}_callbacks" send("_#{callback}_callbacks=", CallbackChain.new(callback, config)) __define_runner(callback) end end
def reset_callbacks(symbol)
Remove all set callbacks for the given event.
def reset_callbacks(symbol) callbacks = send("_#{symbol}_callbacks") ActiveSupport::DescendantsTracker.descendants(self).each do |target| chain = target.send("_#{symbol}_callbacks").dup callbacks.each { |c| chain.delete(c) } target.send("_#{symbol}_callbacks=", chain) target.__reset_runner(symbol) end self.send("_#{symbol}_callbacks=", callbacks.dup.clear) __reset_runner(symbol) end
def set_callback(name, *filter_list, &block)
is a speed improvement for ActionPack.
method that took into consideration the per_key conditions. This
In that case, each action_name would get its own compiled callback
run_callbacks(:process_action, action_name) { ... dispatch stuff ... }
In the case of the above example, you would do:
Per-key conditions are evaluated only once per use of a given key.
set_callback :process_action, :before, :authenticate, :per_key => {:unless => proc {|c| c.action_name == "index"}}
becomes
before_filter :authenticate, :except => "index"
we convert :only and :except conditions into per-key conditions.
are always the same for a given key. For instance, in Action Pack,
When creating or skipping callbacks, you can specify conditions that
===== Per-key conditions
see "Per-key conditions" below.
* :per_key - A hash with :if and :unless options;
chain rather than appended.
* :prepend - If true, the callback will be prepended to the existing
will be called only when it returns a false value.
* :unless - A symbol naming an instance method or a proc; the callback
will be called only when it returns a true value.
* :if - A symbol naming an instance method or a proc; the callback
===== Options
wasn't halted, from the +yield+ call.
Around callbacks can access the return value from the event, if it
callbacks are called in the reverse order.
Before and around callbacks are called in the order that they are set; after
an argument.
of the current object. It can also optionally accept the current object as
If a proc, lambda, or block is given, its body is evaluated in the context
+define_callback+.
responds to a certain method determined by the :scope argument to
lambda, or block; as a string to be instance evaluated; or as an object that
The callback can specified as a symbol naming an instance method; as a proc,
set_callback :save, :before_meth
means the first example above can also be written as:
+:after+, or +:around+ the event. If omitted, +:before+ is assumed. This
The second arguments indicates whether the callback is to be run +:before+,
set_callback :save, :around, lambda { |r| stuff; result = yield; stuff }
set_callback :save, :after, :after_meth, :if => :condition
set_callback :save, :before, :before_meth
Install a callback for the given event.
def set_callback(name, *filter_list, &block) mapped = nil __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) end filters.each do |filter| chain.delete_if {|c| c.matches?(type, filter) } end options[:prepend] ? chain.unshift(*(mapped.reverse)) : chain.push(*mapped) target.send("_#{name}_callbacks=", chain) end end
def skip_callback(name, *filter_list, &block)
end
skip_callback :validate, :before, :check_membership, :if => lambda { self.age > 18 }
class Writer < Person
options may be passed in order to control when the callback is skipped.
Skip a previously set callback. Like +set_callback+, :if or :unless
def skip_callback(name, *filter_list, &block) __update_callbacks(name, filter_list, block) do |target, chain, type, filters, options| filters.each do |filter| filter = chain.find {|c| c.matches?(type, filter) } if filter && options.any? new_filter = filter.clone(chain, self) chain.insert(chain.index(filter), new_filter) new_filter.recompile!(options, options[:per_key] || {}) end chain.delete(filter) end target.send("_#{name}_callbacks=", chain) end end