Class: DataMapper::Callbacks
Overview
Callbacks is a collection to assign and execute blocks of code when hooks throughout the DataMapper call Callbacks#execute. A set of the standard callbacks is declared in the Callbacks::EVENTS array.
Constant Summary collapse
- EVENTS =
These are a collection of default callbacks that are hooked into the DataMapper. You’re free to add your own events just by calling #add, but you’ll have add the appropriate hooks into the system to actually execute them yourself.
[ :before_materialize, :after_materialize, :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy, :before_validation, :after_validation ]
Instance Method Summary collapse
-
#add(name, block) ⇒ Object
Asign delegating code to a callback.
- #dup ⇒ Object
-
#execute(name, instance, *args) ⇒ Object
Executes a given callback and returns TRUE or FALSE depending on the return value of the callbacks.
-
#initialize ⇒ Callbacks
constructor
Initializes an internal Hash that ensures callback names are always of type Symbol, and assigns an Array to store your delegating code when the callback is looked-up by name.
Constructor Details
#initialize ⇒ Callbacks
Initializes an internal Hash that ensures callback names are always of type Symbol, and assigns an Array to store your delegating code when the callback is looked-up by name.
60 61 62 63 64 65 |
# File 'lib/data_mapper/callbacks.rb', line 60 def initialize @callbacks = Hash.new do |h,k| raise 'Callback names must be Symbols' unless k.kind_of?(Symbol) h[k] = Set.new end end |
Instance Method Details
#add(name, block) ⇒ Object
Asign delegating code to a callback. The block
parameter can be a Proc object, a String which will be eval’ed when the callback is executed, or a Symbol, which will be sent to the instance executed against (as a method call).
90 91 92 93 94 |
# File 'lib/data_mapper/callbacks.rb', line 90 def add(name, block) callback = @callbacks[name] raise ArgumentError.new("You didn't specify a callback in String, Symbol or Proc form.") unless [String, Proc, Symbol].detect { |type| block.is_a?(type) } callback << block end |
#dup ⇒ Object
96 97 98 99 100 101 102 103 104 |
# File 'lib/data_mapper/callbacks.rb', line 96 def dup copy = self.class.new @callbacks.each_pair do |name, callbacks| callbacks.each do |callback| copy.add(name, callback) end end return copy end |
#execute(name, instance, *args) ⇒ Object
Executes a given callback and returns TRUE or FALSE depending on the return value of the callbacks. All callbacks must return successfully in order for the call to #execute to return TRUE. Callbacks always execute against an instance
. You may pass additional arguments which will in turn be passed to any Proc objects assigned to a specific callback. Strings assigned to callbacks do not accept parameters. They are instance-eval’ed instead. When the callback is a Symbol, it is sent to the instance under the assumption it is a method call.
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/data_mapper/callbacks.rb', line 75 def execute(name, instance, *args) @callbacks[name].all? do |callback| case callback when String then instance.instance_eval(callback) when Proc then callback[instance, *args] when Symbol then instance.send(callback, *args) else raise '' end end end |