2011-03-12 08:24:36 -05:00
|
|
|
require 'active_support/concern'
|
2010-06-19 10:58:12 -04:00
|
|
|
require 'active_support/descendants_tracker'
|
2013-07-01 05:14:09 -04:00
|
|
|
require 'active_support/core_ext/array/extract_options'
|
2010-11-17 18:32:43 -05:00
|
|
|
require 'active_support/core_ext/class/attribute'
|
2009-10-12 23:15:43 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2010-04-05 15:15:08 -04:00
|
|
|
require 'active_support/core_ext/kernel/singleton_class'
|
2013-05-14 19:18:11 -04:00
|
|
|
require 'thread'
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2008-01-18 21:44:45 -05:00
|
|
|
module ActiveSupport
|
2013-12-12 02:25:35 -05:00
|
|
|
# Callbacks are code hooks that are run at key points in an object's life cycle.
|
2012-09-17 01:22:18 -04:00
|
|
|
# The typical use case is to have a base class define a set of callbacks
|
|
|
|
# relevant to the other functionality it supplies, so that subclasses can
|
|
|
|
# install callbacks that enhance or modify the base functionality without
|
|
|
|
# needing to override or redefine methods of the base class.
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Mixing in this module allows you to define the events in the object's
|
2013-12-12 02:25:35 -05:00
|
|
|
# life cycle that will support callbacks (via +ClassMethods.define_callbacks+),
|
2012-09-17 01:22:18 -04:00
|
|
|
# set the instance methods, procs, or callback objects to be called (via
|
2012-12-05 01:11:54 -05:00
|
|
|
# +ClassMethods.set_callback+), and run the installed callbacks at the
|
2012-09-17 01:22:18 -04:00
|
|
|
# appropriate times (via +run_callbacks+).
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Three kinds of callbacks are supported: before callbacks, run before a
|
|
|
|
# certain event; after callbacks, run after the event; and around callbacks,
|
|
|
|
# blocks that surround the event, triggering it when they yield. Callback code
|
|
|
|
# can be contained in instance methods, procs or lambdas, or callback objects
|
|
|
|
# that respond to certain predetermined methods. See +ClassMethods.set_callback+
|
|
|
|
# for details.
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# class Record
|
|
|
|
# include ActiveSupport::Callbacks
|
|
|
|
# define_callbacks :save
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
|
|
|
# def save
|
2009-10-12 23:15:43 -04:00
|
|
|
# run_callbacks :save do
|
|
|
|
# puts "- save"
|
|
|
|
# end
|
2008-03-05 05:53:34 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# class PersonRecord < Record
|
2009-10-12 23:15:43 -04:00
|
|
|
# set_callback :save, :before, :saving_message
|
2008-03-05 05:53:34 -05:00
|
|
|
# def saving_message
|
|
|
|
# puts "saving..."
|
|
|
|
# end
|
|
|
|
#
|
2009-10-12 23:15:43 -04:00
|
|
|
# set_callback :save, :after do |object|
|
2008-03-05 05:53:34 -05:00
|
|
|
# puts "saved"
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# person = PersonRecord.new
|
|
|
|
# person.save
|
2008-03-05 05:53:34 -05:00
|
|
|
#
|
|
|
|
# Output:
|
|
|
|
# saving...
|
|
|
|
# - save
|
|
|
|
# saved
|
2008-01-18 21:44:45 -05:00
|
|
|
module Callbacks
|
2009-10-14 00:30:06 -04:00
|
|
|
extend Concern
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2010-06-19 10:58:12 -04:00
|
|
|
included do
|
|
|
|
extend ActiveSupport::DescendantsTracker
|
|
|
|
end
|
|
|
|
|
2013-04-21 23:27:27 -04:00
|
|
|
CALLBACK_FILTER_TYPES = [:before, :after, :around]
|
|
|
|
|
2011-01-09 13:00:15 -05:00
|
|
|
# Runs the callbacks for the given event.
|
|
|
|
#
|
|
|
|
# Calls the before and around callbacks in the order they were set, yields
|
2012-09-17 01:22:18 -04:00
|
|
|
# the block (if given one), and then runs the after callbacks in reverse
|
|
|
|
# order.
|
2011-01-09 13:00:15 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# If the callback chain was halted, returns +false+. Otherwise returns the
|
2014-06-29 14:14:40 -04:00
|
|
|
# result of the block, +nil+ if no callbacks have been set, or +true+
|
2014-06-27 16:23:32 -04:00
|
|
|
# if callbacks have been set but no block is given.
|
2011-01-09 13:00:15 -05:00
|
|
|
#
|
|
|
|
# run_callbacks :save do
|
|
|
|
# save
|
|
|
|
# end
|
2012-05-10 03:21:03 -04:00
|
|
|
def run_callbacks(kind, &block)
|
2013-05-16 17:19:36 -04:00
|
|
|
cbs = send("_#{kind}_callbacks")
|
2013-05-13 19:43:27 -04:00
|
|
|
if cbs.empty?
|
|
|
|
yield if block_given?
|
|
|
|
else
|
|
|
|
runner = cbs.compile
|
|
|
|
e = Filters::Environment.new(self, false, nil, block)
|
|
|
|
runner.call(e).value
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2011-11-30 03:52:52 -05:00
|
|
|
private
|
|
|
|
|
2013-12-12 00:59:23 -05:00
|
|
|
# A hook invoked every time a before callback is halted.
|
2012-07-26 14:44:36 -04:00
|
|
|
# This can be overridden in AS::Callback implementors in order
|
2011-11-30 03:52:52 -05:00
|
|
|
# to provide better debugging/logging.
|
|
|
|
def halted_callback_hook(filter)
|
|
|
|
end
|
|
|
|
|
2013-06-11 13:20:29 -04:00
|
|
|
module Conditionals # :nodoc:
|
|
|
|
class Value
|
|
|
|
def initialize(&block)
|
|
|
|
@block = block
|
|
|
|
end
|
|
|
|
def call(target, value); @block.call(value); end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-09 18:22:27 -04:00
|
|
|
module Filters
|
|
|
|
Environment = Struct.new(:target, :halted, :value, :run_block)
|
2013-05-13 19:59:05 -04:00
|
|
|
|
|
|
|
class End
|
|
|
|
def call(env)
|
|
|
|
block = env.run_block
|
|
|
|
env.value = !env.halted && (!block || block.call)
|
|
|
|
env
|
|
|
|
end
|
|
|
|
end
|
|
|
|
ENDING = End.new
|
2013-05-14 14:20:34 -04:00
|
|
|
|
|
|
|
class Before
|
|
|
|
def self.build(next_callback, user_callback, user_conditions, chain_config, filter)
|
2013-05-14 19:03:09 -04:00
|
|
|
halted_lambda = chain_config[:terminator]
|
|
|
|
|
2013-05-14 14:20:34 -04:00
|
|
|
if chain_config.key?(:terminator) && user_conditions.any?
|
2013-05-14 14:30:14 -04:00
|
|
|
halting_and_conditional(next_callback, user_callback, user_conditions, halted_lambda, filter)
|
2013-05-14 14:20:34 -04:00
|
|
|
elsif chain_config.key? :terminator
|
2013-05-14 14:30:14 -04:00
|
|
|
halting(next_callback, user_callback, halted_lambda, filter)
|
2013-05-14 14:20:34 -04:00
|
|
|
elsif user_conditions.any?
|
2013-05-14 14:27:24 -04:00
|
|
|
conditional(next_callback, user_callback, user_conditions)
|
2013-05-14 14:20:34 -04:00
|
|
|
else
|
2013-05-14 14:27:24 -04:00
|
|
|
simple next_callback, user_callback
|
2013-05-14 14:20:34 -04:00
|
|
|
end
|
|
|
|
end
|
2013-05-14 14:27:24 -04:00
|
|
|
|
2013-05-14 14:30:14 -04:00
|
|
|
def self.halting_and_conditional(next_callback, user_callback, user_conditions, halted_lambda, filter)
|
2013-05-14 14:27:24 -04:00
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
halted = env.halted
|
|
|
|
|
|
|
|
if !halted && user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
result = user_callback.call target, value
|
2013-05-14 19:03:09 -04:00
|
|
|
env.halted = halted_lambda.call(target, result)
|
2013-05-14 14:27:24 -04:00
|
|
|
if env.halted
|
|
|
|
target.send :halted_callback_hook, filter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next_callback.call env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting_and_conditional
|
2013-05-14 14:27:24 -04:00
|
|
|
|
2013-05-14 14:30:14 -04:00
|
|
|
def self.halting(next_callback, user_callback, halted_lambda, filter)
|
2013-05-14 14:27:24 -04:00
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
halted = env.halted
|
|
|
|
|
2013-05-15 12:18:08 -04:00
|
|
|
unless halted
|
2013-05-14 14:27:24 -04:00
|
|
|
result = user_callback.call target, value
|
2013-05-14 19:03:09 -04:00
|
|
|
env.halted = halted_lambda.call(target, result)
|
2013-05-14 14:27:24 -04:00
|
|
|
if env.halted
|
|
|
|
target.send :halted_callback_hook, filter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
next_callback.call env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting
|
2013-05-14 14:27:24 -04:00
|
|
|
|
|
|
|
def self.conditional(next_callback, user_callback, user_conditions)
|
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
|
|
|
|
if user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
user_callback.call target, value
|
|
|
|
end
|
|
|
|
next_callback.call env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :conditional
|
2013-05-14 14:27:24 -04:00
|
|
|
|
|
|
|
def self.simple(next_callback, user_callback)
|
|
|
|
lambda { |env|
|
|
|
|
user_callback.call env.target, env.value
|
|
|
|
next_callback.call env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :simple
|
2013-05-14 14:20:34 -04:00
|
|
|
end
|
2013-05-14 14:37:34 -04:00
|
|
|
|
|
|
|
class After
|
|
|
|
def self.build(next_callback, user_callback, user_conditions, chain_config)
|
|
|
|
if chain_config[:skip_after_callbacks_if_terminated]
|
|
|
|
if chain_config.key?(:terminator) && user_conditions.any?
|
|
|
|
halting_and_conditional(next_callback, user_callback, user_conditions)
|
|
|
|
elsif chain_config.key?(:terminator)
|
|
|
|
halting(next_callback, user_callback)
|
|
|
|
elsif user_conditions.any?
|
|
|
|
conditional next_callback, user_callback, user_conditions
|
|
|
|
else
|
|
|
|
simple next_callback, user_callback
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if user_conditions.any?
|
|
|
|
conditional next_callback, user_callback, user_conditions
|
|
|
|
else
|
|
|
|
simple next_callback, user_callback
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.halting_and_conditional(next_callback, user_callback, user_conditions)
|
|
|
|
lambda { |env|
|
|
|
|
env = next_callback.call env
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
halted = env.halted
|
|
|
|
|
|
|
|
if !halted && user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
user_callback.call target, value
|
|
|
|
end
|
|
|
|
env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting_and_conditional
|
2013-05-14 14:37:34 -04:00
|
|
|
|
|
|
|
def self.halting(next_callback, user_callback)
|
|
|
|
lambda { |env|
|
|
|
|
env = next_callback.call env
|
2013-05-15 12:18:08 -04:00
|
|
|
unless env.halted
|
2013-05-14 14:37:34 -04:00
|
|
|
user_callback.call env.target, env.value
|
|
|
|
end
|
|
|
|
env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting
|
2013-05-14 14:37:34 -04:00
|
|
|
|
|
|
|
def self.conditional(next_callback, user_callback, user_conditions)
|
|
|
|
lambda { |env|
|
|
|
|
env = next_callback.call env
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
|
|
|
|
if user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
user_callback.call target, value
|
|
|
|
end
|
|
|
|
env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :conditional
|
2013-05-14 14:37:34 -04:00
|
|
|
|
|
|
|
def self.simple(next_callback, user_callback)
|
|
|
|
lambda { |env|
|
|
|
|
env = next_callback.call env
|
|
|
|
user_callback.call env.target, env.value
|
|
|
|
env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :simple
|
2013-05-14 14:37:34 -04:00
|
|
|
end
|
2013-05-14 14:48:04 -04:00
|
|
|
|
|
|
|
class Around
|
|
|
|
def self.build(next_callback, user_callback, user_conditions, chain_config)
|
|
|
|
if chain_config.key?(:terminator) && user_conditions.any?
|
|
|
|
halting_and_conditional(next_callback, user_callback, user_conditions)
|
|
|
|
elsif chain_config.key? :terminator
|
|
|
|
halting(next_callback, user_callback)
|
|
|
|
elsif user_conditions.any?
|
|
|
|
conditional(next_callback, user_callback, user_conditions)
|
|
|
|
else
|
|
|
|
simple(next_callback, user_callback)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.halting_and_conditional(next_callback, user_callback, user_conditions)
|
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
halted = env.halted
|
|
|
|
|
|
|
|
if !halted && user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
user_callback.call(target, value) {
|
|
|
|
env = next_callback.call env
|
|
|
|
env.value
|
|
|
|
}
|
|
|
|
env
|
|
|
|
else
|
|
|
|
next_callback.call env
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting_and_conditional
|
2013-05-14 14:48:04 -04:00
|
|
|
|
|
|
|
def self.halting(next_callback, user_callback)
|
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
|
2014-05-12 19:23:04 -04:00
|
|
|
if env.halted
|
|
|
|
next_callback.call env
|
|
|
|
else
|
2013-05-14 14:48:04 -04:00
|
|
|
user_callback.call(target, value) {
|
|
|
|
env = next_callback.call env
|
|
|
|
env.value
|
|
|
|
}
|
|
|
|
env
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :halting
|
2013-05-14 14:48:04 -04:00
|
|
|
|
|
|
|
def self.conditional(next_callback, user_callback, user_conditions)
|
|
|
|
lambda { |env|
|
|
|
|
target = env.target
|
|
|
|
value = env.value
|
|
|
|
|
|
|
|
if user_conditions.all? { |c| c.call(target, value) }
|
|
|
|
user_callback.call(target, value) {
|
|
|
|
env = next_callback.call env
|
|
|
|
env.value
|
|
|
|
}
|
|
|
|
env
|
|
|
|
else
|
|
|
|
next_callback.call env
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :conditional
|
2013-05-14 14:48:04 -04:00
|
|
|
|
|
|
|
def self.simple(next_callback, user_callback)
|
|
|
|
lambda { |env|
|
|
|
|
user_callback.call(env.target, env.value) {
|
|
|
|
env = next_callback.call env
|
|
|
|
env.value
|
|
|
|
}
|
|
|
|
env
|
|
|
|
}
|
|
|
|
end
|
2013-07-12 10:55:29 -04:00
|
|
|
private_class_method :simple
|
2013-05-14 14:48:04 -04:00
|
|
|
end
|
2013-05-09 18:22:27 -04:00
|
|
|
end
|
|
|
|
|
2011-01-09 13:00:15 -05:00
|
|
|
class Callback #:nodoc:#
|
2013-05-10 14:55:27 -04:00
|
|
|
def self.build(chain, filter, kind, options)
|
2013-05-10 18:43:34 -04:00
|
|
|
new chain.name, filter, kind, options, chain.config
|
2013-05-07 16:26:23 -04:00
|
|
|
end
|
|
|
|
|
2013-05-15 12:39:23 -04:00
|
|
|
attr_accessor :kind, :name
|
2013-05-10 18:43:34 -04:00
|
|
|
attr_reader :chain_config
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2013-05-10 18:43:34 -04:00
|
|
|
def initialize(name, filter, kind, options, chain_config)
|
|
|
|
@chain_config = chain_config
|
|
|
|
@name = name
|
2013-05-10 14:50:50 -04:00
|
|
|
@kind = kind
|
|
|
|
@filter = filter
|
|
|
|
@key = compute_identifier filter
|
2013-05-15 12:39:23 -04:00
|
|
|
@if = Array(options[:if])
|
|
|
|
@unless = Array(options[:unless])
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2013-05-10 14:50:50 -04:00
|
|
|
def filter; @key; end
|
|
|
|
def raw_filter; @filter; end
|
2013-05-08 16:36:04 -04:00
|
|
|
|
2013-05-10 17:12:56 -04:00
|
|
|
def merge(chain, new_options)
|
2013-05-15 12:43:17 -04:00
|
|
|
options = {
|
2013-05-15 12:39:23 -04:00
|
|
|
:if => @if.dup,
|
|
|
|
:unless => @unless.dup
|
2013-05-10 15:00:35 -04:00
|
|
|
}
|
2013-05-10 17:12:56 -04:00
|
|
|
|
2013-05-15 12:43:17 -04:00
|
|
|
options[:if].concat Array(new_options.fetch(:unless, []))
|
|
|
|
options[:unless].concat Array(new_options.fetch(:if, []))
|
2013-05-10 17:12:56 -04:00
|
|
|
|
2013-05-15 12:43:17 -04:00
|
|
|
self.class.build chain, @filter, @kind, options
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
def matches?(_kind, _filter)
|
2013-05-08 15:01:55 -04:00
|
|
|
@kind == _kind && filter == _filter
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2012-08-15 08:43:04 -04:00
|
|
|
def duplicates?(other)
|
2013-05-10 14:50:50 -04:00
|
|
|
case @filter
|
2013-05-10 14:45:48 -04:00
|
|
|
when Symbol, String
|
|
|
|
matches?(other.kind, other.filter)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
end
|
|
|
|
|
2011-11-15 03:33:25 -05:00
|
|
|
# Wraps code with filter
|
2013-05-09 17:47:17 -04:00
|
|
|
def apply(next_callback)
|
2013-05-09 17:55:15 -04:00
|
|
|
user_conditions = conditions_lambdas
|
2013-05-10 14:50:50 -04:00
|
|
|
user_callback = make_lambda @filter
|
2013-05-09 14:58:55 -04:00
|
|
|
|
2013-05-10 14:50:50 -04:00
|
|
|
case kind
|
2010-12-07 12:00:49 -05:00
|
|
|
when :before
|
2013-05-14 14:20:34 -04:00
|
|
|
Filters::Before.build(next_callback, user_callback, user_conditions, chain_config, @filter)
|
2010-12-07 12:00:49 -05:00
|
|
|
when :after
|
2013-05-14 14:37:34 -04:00
|
|
|
Filters::After.build(next_callback, user_callback, user_conditions, chain_config)
|
2010-12-07 12:00:49 -05:00
|
|
|
when :around
|
2013-05-14 14:48:04 -04:00
|
|
|
Filters::Around.build(next_callback, user_callback, user_conditions, chain_config)
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-05-08 20:57:06 -04:00
|
|
|
def invert_lambda(l)
|
|
|
|
lambda { |*args, &blk| !l.call(*args, &blk) }
|
|
|
|
end
|
|
|
|
|
2013-05-09 14:58:55 -04:00
|
|
|
# Filters support:
|
|
|
|
#
|
|
|
|
# Symbols:: A method to call.
|
|
|
|
# Strings:: Some content to evaluate.
|
|
|
|
# Procs:: A proc to call with the object.
|
|
|
|
# Objects:: An object with a <tt>before_foo</tt> method on it to call.
|
|
|
|
#
|
2014-07-03 17:52:03 -04:00
|
|
|
# All of these objects are converted into a lambda and handled
|
|
|
|
# the same after this point.
|
2013-05-08 20:57:06 -04:00
|
|
|
def make_lambda(filter)
|
|
|
|
case filter
|
|
|
|
when Symbol
|
2013-05-13 19:53:08 -04:00
|
|
|
lambda { |target, _, &blk| target.send filter, &blk }
|
2013-05-08 20:57:06 -04:00
|
|
|
when String
|
|
|
|
l = eval "lambda { |value| #{filter} }"
|
2013-05-13 18:50:16 -04:00
|
|
|
lambda { |target, value| target.instance_exec(value, &l) }
|
2013-06-11 13:20:29 -04:00
|
|
|
when Conditionals::Value then filter
|
2013-05-08 20:57:06 -04:00
|
|
|
when ::Proc
|
2013-05-14 17:02:33 -04:00
|
|
|
if filter.arity > 1
|
|
|
|
return lambda { |target, _, &block|
|
|
|
|
raise ArgumentError unless block
|
|
|
|
target.instance_exec(target, block, &filter)
|
|
|
|
}
|
|
|
|
end
|
2013-05-08 20:57:06 -04:00
|
|
|
|
2013-05-13 18:50:16 -04:00
|
|
|
if filter.arity <= 0
|
|
|
|
lambda { |target, _| target.instance_exec(&filter) }
|
2013-05-08 20:57:06 -04:00
|
|
|
else
|
2013-05-13 18:50:16 -04:00
|
|
|
lambda { |target, _| target.instance_exec(target, &filter) }
|
2013-05-08 20:57:06 -04:00
|
|
|
end
|
|
|
|
else
|
2013-05-10 18:43:34 -04:00
|
|
|
scopes = Array(chain_config[:scope])
|
2013-05-08 20:57:06 -04:00
|
|
|
method_to_call = scopes.map{ |s| public_send(s) }.join("_")
|
|
|
|
|
|
|
|
lambda { |target, _, &blk|
|
|
|
|
filter.public_send method_to_call, target, &blk
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-08 16:36:04 -04:00
|
|
|
def compute_identifier(filter)
|
|
|
|
case filter
|
|
|
|
when String, ::Proc
|
|
|
|
filter.object_id
|
|
|
|
else
|
|
|
|
filter
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-09 14:58:55 -04:00
|
|
|
def conditions_lambdas
|
2013-05-15 12:39:23 -04:00
|
|
|
@if.map { |c| make_lambda c } +
|
|
|
|
@unless.map { |c| invert_lambda make_lambda c }
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# An Array with a compile method.
|
2013-05-10 13:56:09 -04:00
|
|
|
class CallbackChain #:nodoc:#
|
|
|
|
include Enumerable
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
attr_reader :name, :config
|
|
|
|
|
|
|
|
def initialize(name, config)
|
|
|
|
@name = name
|
|
|
|
@config = {
|
|
|
|
:scope => [ :kind ]
|
2013-03-29 11:28:46 -04:00
|
|
|
}.merge!(config)
|
2013-05-10 13:56:09 -04:00
|
|
|
@chain = []
|
2013-05-10 14:00:25 -04:00
|
|
|
@callbacks = nil
|
2013-05-14 19:18:11 -04:00
|
|
|
@mutex = Mutex.new
|
2013-05-10 13:56:09 -04:00
|
|
|
end
|
|
|
|
|
2013-05-14 15:03:33 -04:00
|
|
|
def each(&block); @chain.each(&block); end
|
|
|
|
def index(o); @chain.index(o); end
|
|
|
|
def empty?; @chain.empty?; end
|
2013-05-10 13:56:09 -04:00
|
|
|
|
2013-05-10 14:00:25 -04:00
|
|
|
def insert(index, o)
|
|
|
|
@callbacks = nil
|
|
|
|
@chain.insert(index, o)
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(o)
|
|
|
|
@callbacks = nil
|
|
|
|
@chain.delete(o)
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear
|
|
|
|
@callbacks = nil
|
|
|
|
@chain.clear
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2013-05-10 13:56:09 -04:00
|
|
|
def initialize_copy(other)
|
2013-05-10 14:00:25 -04:00
|
|
|
@callbacks = nil
|
|
|
|
@chain = other.chain.dup
|
2013-05-14 19:18:11 -04:00
|
|
|
@mutex = Mutex.new
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
|
|
|
|
2012-02-05 05:58:44 -05:00
|
|
|
def compile
|
2013-05-14 19:18:11 -04:00
|
|
|
@callbacks || @mutex.synchronize do
|
|
|
|
@callbacks ||= @chain.reverse.inject(Filters::ENDING) do |chain, callback|
|
|
|
|
callback.apply chain
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2011-11-15 03:33:25 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2012-08-15 08:43:04 -04:00
|
|
|
def append(*callbacks)
|
|
|
|
callbacks.each { |c| append_one(c) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def prepend(*callbacks)
|
|
|
|
callbacks.each { |c| prepend_one(c) }
|
|
|
|
end
|
|
|
|
|
2013-05-10 13:56:09 -04:00
|
|
|
protected
|
|
|
|
def chain; @chain; end
|
|
|
|
|
2012-08-15 08:43:04 -04:00
|
|
|
private
|
|
|
|
|
2013-05-03 07:44:49 -04:00
|
|
|
def append_one(callback)
|
2013-05-10 14:00:25 -04:00
|
|
|
@callbacks = nil
|
2013-05-03 07:44:49 -04:00
|
|
|
remove_duplicates(callback)
|
2013-05-10 13:56:09 -04:00
|
|
|
@chain.push(callback)
|
2013-05-03 07:44:49 -04:00
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2013-05-03 07:44:49 -04:00
|
|
|
def prepend_one(callback)
|
2013-05-10 14:00:25 -04:00
|
|
|
@callbacks = nil
|
2013-05-03 07:44:49 -04:00
|
|
|
remove_duplicates(callback)
|
2013-05-10 13:56:09 -04:00
|
|
|
@chain.unshift(callback)
|
2013-05-03 07:44:49 -04:00
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2013-05-03 07:44:49 -04:00
|
|
|
def remove_duplicates(callback)
|
2013-05-10 14:00:25 -04:00
|
|
|
@callbacks = nil
|
2013-05-10 13:56:09 -04:00
|
|
|
@chain.delete_if { |c| callback.duplicates?(c) }
|
2013-05-03 07:44:49 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
module ClassMethods
|
2013-05-16 23:34:44 -04:00
|
|
|
def normalize_callback_params(filters, block) # :nodoc:
|
2013-04-21 23:27:27 -04:00
|
|
|
type = CALLBACK_FILTER_TYPES.include?(filters.first) ? filters.shift : :before
|
2013-07-01 05:14:09 -04:00
|
|
|
options = filters.extract_options!
|
2009-10-12 23:15:43 -04:00
|
|
|
filters.unshift(block) if block
|
2013-05-14 19:22:29 -04:00
|
|
|
[type, filters, options.dup]
|
2013-05-13 17:11:09 -04:00
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2013-05-13 17:11:09 -04:00
|
|
|
# This is used internally to append, prepend and skip callbacks to the
|
|
|
|
# CallbackChain.
|
2013-05-13 17:14:50 -04:00
|
|
|
def __update_callbacks(name) #:nodoc:
|
2011-03-12 10:05:52 -05:00
|
|
|
([self] + ActiveSupport::DescendantsTracker.descendants(self)).reverse.each do |target|
|
2013-05-13 17:23:03 -04:00
|
|
|
chain = target.get_callbacks name
|
2013-05-13 17:14:50 -04:00
|
|
|
yield target, chain.dup
|
2010-06-12 03:00:45 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2011-01-09 13:00:15 -05:00
|
|
|
# Install a callback for the given event.
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
|
|
|
# set_callback :save, :before, :before_meth
|
2012-09-17 01:22:18 -04:00
|
|
|
# set_callback :save, :after, :after_meth, if: :condition
|
|
|
|
# set_callback :save, :around, ->(r, &block) { stuff; result = block.call; stuff }
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# The second arguments indicates whether the callback is to be run +:before+,
|
|
|
|
# +:after+, or +:around+ the event. If omitted, +:before+ is assumed. This
|
|
|
|
# means the first example above can also be written as:
|
|
|
|
#
|
2010-12-13 22:13:33 -05:00
|
|
|
# set_callback :save, :before_meth
|
|
|
|
#
|
2013-09-06 04:53:17 -04:00
|
|
|
# The callback can be specified as a symbol naming an instance method; as a
|
2012-12-05 01:11:54 -05:00
|
|
|
# proc, lambda, or block; as a string to be instance evaluated; or as an
|
2012-09-17 01:22:18 -04:00
|
|
|
# object that responds to a certain method determined by the <tt>:scope</tt>
|
2013-12-21 11:44:43 -05:00
|
|
|
# argument to +define_callbacks+.
|
2011-01-09 13:00:15 -05:00
|
|
|
#
|
|
|
|
# If a proc, lambda, or block is given, its body is evaluated in the context
|
|
|
|
# of the current object. It can also optionally accept the current object as
|
|
|
|
# an argument.
|
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Before and around callbacks are called in the order that they are set;
|
|
|
|
# after callbacks are called in the reverse order.
|
|
|
|
#
|
2011-03-07 08:07:46 -05:00
|
|
|
# Around callbacks can access the return value from the event, if it
|
|
|
|
# wasn't halted, from the +yield+ call.
|
2011-01-09 13:00:15 -05:00
|
|
|
#
|
|
|
|
# ===== Options
|
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# * <tt>:if</tt> - A symbol naming an instance method or a proc; the
|
|
|
|
# callback will be called only when it returns a +true+ value.
|
|
|
|
# * <tt>:unless</tt> - A symbol naming an instance method or a proc; the
|
|
|
|
# callback will be called only when it returns a +false+ value.
|
|
|
|
# * <tt>:prepend</tt> - If +true+, the callback will be prepended to the
|
|
|
|
# existing chain rather than appended.
|
2009-12-28 19:28:26 -05:00
|
|
|
def set_callback(name, *filter_list, &block)
|
2013-05-16 23:34:44 -04:00
|
|
|
type, filters, options = normalize_callback_params(filter_list, block)
|
2013-05-14 15:03:33 -04:00
|
|
|
self_chain = get_callbacks name
|
2013-05-13 17:23:03 -04:00
|
|
|
mapped = filters.map do |filter|
|
2013-05-14 19:22:29 -04:00
|
|
|
Callback.build(self_chain, filter, type, options)
|
2013-05-13 17:23:03 -04:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2013-05-13 17:14:50 -04:00
|
|
|
__update_callbacks(name) do |target, chain|
|
2012-08-15 08:43:04 -04:00
|
|
|
options[:prepend] ? chain.prepend(*mapped) : chain.append(*mapped)
|
2013-05-13 17:23:03 -04:00
|
|
|
target.set_callbacks name, chain
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-17 01:22:18 -04:00
|
|
|
# Skip a previously set callback. Like +set_callback+, <tt>:if</tt> or
|
|
|
|
# <tt>:unless</tt> options may be passed in order to control when the
|
|
|
|
# callback is skipped.
|
2010-08-05 16:57:20 -04:00
|
|
|
#
|
|
|
|
# class Writer < Person
|
2012-09-17 01:22:18 -04:00
|
|
|
# skip_callback :validate, :before, :check_membership, if: -> { self.age > 18 }
|
2010-08-05 16:57:20 -04:00
|
|
|
# end
|
2009-12-28 19:28:26 -05:00
|
|
|
def skip_callback(name, *filter_list, &block)
|
2013-05-16 23:34:44 -04:00
|
|
|
type, filters, options = normalize_callback_params(filter_list, block)
|
2013-05-13 17:14:50 -04:00
|
|
|
|
|
|
|
__update_callbacks(name) do |target, chain|
|
2009-10-12 23:15:43 -04:00
|
|
|
filters.each do |filter|
|
|
|
|
filter = chain.find {|c| c.matches?(type, filter) }
|
|
|
|
|
|
|
|
if filter && options.any?
|
2013-05-10 17:12:56 -04:00
|
|
|
new_filter = filter.merge(chain, options)
|
2009-12-31 19:48:12 -05:00
|
|
|
chain.insert(chain.index(filter), new_filter)
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2009-12-31 19:48:12 -05:00
|
|
|
|
|
|
|
chain.delete(filter)
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2013-05-13 17:23:03 -04:00
|
|
|
target.set_callbacks name, chain
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-09 13:00:15 -05:00
|
|
|
# Remove all set callbacks for the given event.
|
2013-06-30 06:21:12 -04:00
|
|
|
def reset_callbacks(name)
|
|
|
|
callbacks = get_callbacks name
|
2010-06-12 03:00:45 -04:00
|
|
|
|
2010-07-05 06:50:08 -04:00
|
|
|
ActiveSupport::DescendantsTracker.descendants(self).each do |target|
|
2013-06-30 06:21:12 -04:00
|
|
|
chain = target.get_callbacks(name).dup
|
2010-06-12 03:00:45 -04:00
|
|
|
callbacks.each { |c| chain.delete(c) }
|
2013-06-30 06:21:12 -04:00
|
|
|
target.set_callbacks name, chain
|
2010-06-12 03:00:45 -04:00
|
|
|
end
|
|
|
|
|
2013-06-30 06:21:12 -04:00
|
|
|
self.set_callbacks name, callbacks.dup.clear
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2013-12-12 02:25:35 -05:00
|
|
|
# Define sets of events in the object life cycle that support callbacks.
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
|
|
|
# define_callbacks :validate
|
2011-01-09 13:00:15 -05:00
|
|
|
# define_callbacks :initialize, :save, :destroy
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# ===== Options
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# * <tt>:terminator</tt> - Determines when a before filter will halt the
|
|
|
|
# callback chain, preventing following callbacks from being called and
|
2013-06-30 05:53:11 -04:00
|
|
|
# the event from being triggered. This should be a lambda to be executed.
|
|
|
|
# The current object and the return result of the callback will be called
|
|
|
|
# with the lambda.
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2013-07-01 05:14:09 -04:00
|
|
|
# define_callbacks :validate, terminator: ->(target, result) { result == false }
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# In this example, if any before validate callbacks returns +false+,
|
2012-09-17 01:22:18 -04:00
|
|
|
# other callbacks are not executed. Defaults to +false+, meaning no value
|
2011-01-09 13:00:15 -05:00
|
|
|
# halts the chain.
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# * <tt>:skip_after_callbacks_if_terminated</tt> - Determines if after
|
|
|
|
# callbacks should be terminated by the <tt>:terminator</tt> option. By
|
|
|
|
# default after callbacks executed no matter if callback chain was
|
|
|
|
# terminated or not. Option makes sense only when <tt>:terminator</tt>
|
|
|
|
# option is specified.
|
2012-02-03 10:57:53 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# * <tt>:scope</tt> - Indicates which methods should be executed when an
|
|
|
|
# object is used as a callback.
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# class Audit
|
|
|
|
# def before(caller)
|
|
|
|
# puts 'Audit: before is called'
|
|
|
|
# end
|
2010-06-14 16:52:47 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# def before_save(caller)
|
|
|
|
# puts 'Audit: before_save is called'
|
|
|
|
# end
|
|
|
|
# end
|
2010-06-14 15:48:09 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# class Account
|
|
|
|
# include ActiveSupport::Callbacks
|
2010-06-14 16:52:47 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# define_callbacks :save
|
|
|
|
# set_callback :save, :before, Audit.new
|
2010-06-14 16:52:47 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# def save
|
|
|
|
# run_callbacks :save do
|
|
|
|
# puts 'save in main'
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2010-06-14 15:48:09 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# In the above case whenever you save an account the method
|
|
|
|
# <tt>Audit#before</tt> will be called. On the other hand
|
2010-06-14 15:48:09 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# define_callbacks :save, scope: [:kind, :name]
|
2010-06-14 15:48:09 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# would trigger <tt>Audit#before_save</tt> instead. That's constructed
|
|
|
|
# by calling <tt>#{kind}_#{name}</tt> on the given instance. In this
|
|
|
|
# case "kind" is "before" and "name" is "save". In this context +:kind+
|
|
|
|
# and +:name+ have special meanings: +:kind+ refers to the kind of
|
|
|
|
# callback (before/after/around) and +:name+ refers to the method on
|
|
|
|
# which callbacks are being defined.
|
2010-06-14 15:48:09 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# A declaration like
|
2009-10-12 23:15:43 -04:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# define_callbacks :save, scope: [:name]
|
2010-06-14 16:18:29 -04:00
|
|
|
#
|
2011-01-09 13:00:15 -05:00
|
|
|
# would call <tt>Audit#save</tt>.
|
2013-06-30 06:21:12 -04:00
|
|
|
def define_callbacks(*names)
|
2013-07-01 05:14:09 -04:00
|
|
|
options = names.extract_options!
|
2013-05-14 19:03:09 -04:00
|
|
|
|
2013-06-30 06:21:12 -04:00
|
|
|
names.each do |name|
|
|
|
|
class_attribute "_#{name}_callbacks"
|
2013-07-01 05:14:09 -04:00
|
|
|
set_callbacks name, CallbackChain.new(name, options)
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
end
|
2013-05-13 17:23:03 -04:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def get_callbacks(name)
|
|
|
|
send "_#{name}_callbacks"
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_callbacks(name, callbacks)
|
|
|
|
send "_#{name}_callbacks=", callbacks
|
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|