2008-01-18 21:44:45 -05:00
|
|
|
module ActiveSupport
|
2008-03-05 05:53:34 -05:00
|
|
|
# Callbacks are hooks into the lifecycle of an object that allow you to trigger logic
|
|
|
|
# before or after an alteration of the object state.
|
|
|
|
#
|
2008-03-07 06:17:38 -05:00
|
|
|
# Mixing in this module allows you to define callbacks in your class.
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# class Storage
|
|
|
|
# include ActiveSupport::Callbacks
|
|
|
|
#
|
|
|
|
# define_callbacks :before_save, :after_save
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class ConfigStorage < Storage
|
|
|
|
# before_save :saving_message
|
|
|
|
# def saving_message
|
|
|
|
# puts "saving..."
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# after_save do |object|
|
|
|
|
# puts "saved"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def save
|
|
|
|
# run_callbacks(:before_save)
|
|
|
|
# puts "- save"
|
|
|
|
# run_callbacks(:after_save)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# config = ConfigStorage.new
|
|
|
|
# config.save
|
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
# saving...
|
|
|
|
# - save
|
|
|
|
# saved
|
|
|
|
#
|
|
|
|
# Callbacks from parent classes are inherited.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# class Storage
|
|
|
|
# include ActiveSupport::Callbacks
|
|
|
|
#
|
|
|
|
# define_callbacks :before_save, :after_save
|
|
|
|
#
|
|
|
|
# before_save :prepare
|
|
|
|
# def prepare
|
|
|
|
# puts "preparing save"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# class ConfigStorage < Storage
|
|
|
|
# before_save :saving_message
|
|
|
|
# def saving_message
|
|
|
|
# puts "saving..."
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# after_save do |object|
|
|
|
|
# puts "saved"
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# def save
|
|
|
|
# run_callbacks(:before_save)
|
|
|
|
# puts "- save"
|
|
|
|
# run_callbacks(:after_save)
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# config = ConfigStorage.new
|
|
|
|
# config.save
|
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
# preparing save
|
|
|
|
# saving...
|
|
|
|
# - save
|
|
|
|
# saved
|
2008-01-18 21:44:45 -05:00
|
|
|
module Callbacks
|
2008-03-18 13:56:05 -04:00
|
|
|
class CallbackChain < Array
|
|
|
|
def self.build(kind, *methods, &block)
|
|
|
|
methods, options = extract_options(*methods, &block)
|
|
|
|
methods.map! { |method| Callback.new(kind, method, options) }
|
|
|
|
new(methods)
|
|
|
|
end
|
|
|
|
|
|
|
|
def run(object, options = {}, &terminator)
|
|
|
|
enumerator = options[:enumerator] || :each
|
2008-01-18 21:44:45 -05:00
|
|
|
|
|
|
|
unless block_given?
|
2008-03-18 13:56:05 -04:00
|
|
|
send(enumerator) { |callback| callback.call(object) }
|
2008-01-18 21:44:45 -05:00
|
|
|
else
|
2008-03-18 13:56:05 -04:00
|
|
|
send(enumerator) do |callback|
|
2008-01-18 21:44:45 -05:00
|
|
|
result = callback.call(object)
|
|
|
|
break result if terminator.call(result, object)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-08-21 01:51:06 -04:00
|
|
|
# TODO: Decompose into more Array like behavior
|
|
|
|
def replace_or_append!(chain)
|
|
|
|
if index = index(chain)
|
|
|
|
self[index] = chain
|
2008-04-20 12:45:44 -04:00
|
|
|
else
|
2008-08-21 01:51:06 -04:00
|
|
|
self << chain
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(callback, &block)
|
2008-03-18 13:56:05 -04:00
|
|
|
select { |c| c == callback && (!block_given? || yield(c)) }.first
|
|
|
|
end
|
|
|
|
|
2008-04-18 00:30:01 -04:00
|
|
|
def delete(callback)
|
2008-04-18 15:44:55 -04:00
|
|
|
super(callback.is_a?(Callback) ? callback : find(callback))
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def self.extract_options(*methods, &block)
|
|
|
|
methods.flatten!
|
|
|
|
options = methods.extract_options!
|
|
|
|
methods << block if block_given?
|
|
|
|
return methods, options
|
|
|
|
end
|
|
|
|
|
|
|
|
def extract_options(*methods, &block)
|
|
|
|
self.class.extract_options(*methods, &block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Callback
|
2008-01-18 21:44:45 -05:00
|
|
|
attr_reader :kind, :method, :identifier, :options
|
|
|
|
|
|
|
|
def initialize(kind, method, options = {})
|
|
|
|
@kind = kind
|
|
|
|
@method = method
|
|
|
|
@identifier = options[:identifier]
|
|
|
|
@options = options
|
|
|
|
end
|
|
|
|
|
2008-03-18 13:56:05 -04:00
|
|
|
def ==(other)
|
|
|
|
case other
|
|
|
|
when Callback
|
|
|
|
(self.identifier && self.identifier == other.identifier) || self.method == other.method
|
|
|
|
else
|
|
|
|
(self.identifier && self.identifier == other) || self.method == other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def eql?(other)
|
|
|
|
self == other
|
|
|
|
end
|
|
|
|
|
|
|
|
def dup
|
|
|
|
self.class.new(@kind, @method, @options.dup)
|
|
|
|
end
|
|
|
|
|
2008-08-21 01:51:06 -04:00
|
|
|
def hash
|
|
|
|
if @identifier
|
|
|
|
@identifier.hash
|
|
|
|
else
|
|
|
|
@method.hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-04 16:26:42 -04:00
|
|
|
def call(*args, &block)
|
|
|
|
evaluate_method(method, *args, &block) if should_run_callback?(*args)
|
2008-03-18 13:56:05 -04:00
|
|
|
rescue LocalJumpError
|
|
|
|
raise ArgumentError,
|
|
|
|
"Cannot yield from a Proc type filter. The Proc must take two " +
|
|
|
|
"arguments and execute #call on the second argument."
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2008-04-04 16:26:42 -04:00
|
|
|
def evaluate_method(method, *args, &block)
|
2008-01-18 21:44:45 -05:00
|
|
|
case method
|
|
|
|
when Symbol
|
2008-04-04 16:26:42 -04:00
|
|
|
object = args.shift
|
|
|
|
object.send(method, *args, &block)
|
2008-01-18 21:44:45 -05:00
|
|
|
when String
|
2008-04-04 16:26:42 -04:00
|
|
|
eval(method, args.first.instance_eval { binding })
|
2008-01-18 21:44:45 -05:00
|
|
|
when Proc, Method
|
2008-04-04 16:26:42 -04:00
|
|
|
method.call(*args, &block)
|
2008-01-18 21:44:45 -05:00
|
|
|
else
|
|
|
|
if method.respond_to?(kind)
|
2008-04-04 16:26:42 -04:00
|
|
|
method.send(kind, *args, &block)
|
2008-01-18 21:44:45 -05:00
|
|
|
else
|
|
|
|
raise ArgumentError,
|
|
|
|
"Callbacks must be a symbol denoting the method to call, a string to be evaluated, " +
|
|
|
|
"a block to be invoked, or an object responding to the callback method."
|
|
|
|
end
|
2008-09-08 21:08:56 -04:00
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2008-04-04 16:26:42 -04:00
|
|
|
def should_run_callback?(*args)
|
2008-01-18 21:44:45 -05:00
|
|
|
if options[:if]
|
2008-04-04 16:26:42 -04:00
|
|
|
evaluate_method(options[:if], *args)
|
2008-01-18 21:44:45 -05:00
|
|
|
elsif options[:unless]
|
2008-04-04 16:26:42 -04:00
|
|
|
!evaluate_method(options[:unless], *args)
|
2008-01-18 21:44:45 -05:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.included(base)
|
|
|
|
base.extend ClassMethods
|
|
|
|
end
|
|
|
|
|
|
|
|
module ClassMethods
|
|
|
|
def define_callbacks(*callbacks)
|
|
|
|
callbacks.each do |callback|
|
|
|
|
class_eval <<-"end_eval"
|
|
|
|
def self.#{callback}(*methods, &block)
|
2008-03-18 13:56:05 -04:00
|
|
|
callbacks = CallbackChain.build(:#{callback}, *methods, &block)
|
|
|
|
(@#{callback}_callbacks ||= CallbackChain.new).concat callbacks
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.#{callback}_callback_chain
|
2008-03-18 13:56:05 -04:00
|
|
|
@#{callback}_callbacks ||= CallbackChain.new
|
2008-01-18 21:44:45 -05:00
|
|
|
|
|
|
|
if superclass.respond_to?(:#{callback}_callback_chain)
|
2008-03-18 13:56:05 -04:00
|
|
|
CallbackChain.new(superclass.#{callback}_callback_chain + @#{callback}_callbacks)
|
2008-01-18 21:44:45 -05:00
|
|
|
else
|
|
|
|
@#{callback}_callbacks
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end_eval
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-04-20 12:45:44 -04:00
|
|
|
# Runs all the callbacks defined for the given options.
|
|
|
|
#
|
2008-03-07 06:17:38 -05:00
|
|
|
# If a block is given it will be called after each callback receiving as arguments:
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
|
|
|
# * the result from the callback
|
|
|
|
# * the object which has the callback
|
|
|
|
#
|
2008-03-07 06:17:38 -05:00
|
|
|
# If the result from the block evaluates to false, the callback chain is stopped.
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
# class Storage
|
|
|
|
# include ActiveSupport::Callbacks
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# define_callbacks :before_save, :after_save
|
|
|
|
# end
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# class ConfigStorage < Storage
|
|
|
|
# before_save :pass
|
|
|
|
# before_save :pass
|
|
|
|
# before_save :stop
|
|
|
|
# before_save :pass
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# def pass
|
|
|
|
# puts "pass"
|
|
|
|
# end
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# def stop
|
|
|
|
# puts "stop"
|
|
|
|
# return false
|
|
|
|
# end
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# def save
|
|
|
|
# result = run_callbacks(:before_save) { |result, object| result == false }
|
|
|
|
# puts "- save" if result
|
|
|
|
# end
|
|
|
|
# end
|
2008-04-20 12:45:44 -04:00
|
|
|
#
|
2008-03-05 05:53:34 -05:00
|
|
|
# config = ConfigStorage.new
|
|
|
|
# config.save
|
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
# pass
|
|
|
|
# pass
|
|
|
|
# stop
|
2008-01-18 21:44:45 -05:00
|
|
|
def run_callbacks(kind, options = {}, &block)
|
2008-03-18 13:56:05 -04:00
|
|
|
self.class.send("#{kind}_callback_chain").run(self, options, &block)
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|