Class | DataMapper::Callbacks |
In: |
lib/data_mapper/callbacks.rb
lib/data_mapper/callbacks.rb |
Parent: | Object |
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.
EVENTS | = | [ :before_materialize, :after_materialize, :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy, :before_validation, :after_validation | 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. | |
EVENTS | = | [ :before_materialize, :after_materialize, :before_save, :after_save, :before_create, :after_create, :before_update, :after_update, :before_destroy, :after_destroy, :before_validation, :after_validation | 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. |
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.
# File lib/data_mapper/callbacks.rb, line 61 61: def initialize 62: @callbacks = Hash.new do |h,k| 63: raise 'Callback names must be Symbols' unless k.kind_of?(Symbol) 64: h[k] = Set.new 65: end 66: end
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.
# File lib/data_mapper/callbacks.rb, line 61 61: def initialize 62: @callbacks = Hash.new do |h,k| 63: raise 'Callback names must be Symbols' unless k.kind_of?(Symbol) 64: h[k] = Set.new 65: end 66: end
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).
# File lib/data_mapper/callbacks.rb, line 91 91: def add(name, block) 92: callback = @callbacks[name] 93: 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) } 94: callback << block 95: end
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).
# File lib/data_mapper/callbacks.rb, line 91 91: def add(name, block) 92: callback = @callbacks[name] 93: 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) } 94: callback << block 95: end
# File lib/data_mapper/callbacks.rb, line 97 97: def dup 98: copy = self.class.new 99: @callbacks.each_pair do |name, callbacks| 100: callbacks.each do |callback| 101: copy.add(name, callback) 102: end 103: end 104: return copy 105: end
# File lib/data_mapper/callbacks.rb, line 97 97: def dup 98: copy = self.class.new 99: @callbacks.each_pair do |name, callbacks| 100: callbacks.each do |callback| 101: copy.add(name, callback) 102: end 103: end 104: return copy 105: end
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.
# File lib/data_mapper/callbacks.rb, line 76 76: def execute(name, instance, *args) 77: @callbacks[name].all? do |callback| 78: case callback 79: when String then instance.instance_eval(callback) 80: when Proc then callback[instance, *args] 81: when Symbol then instance.send(callback, *args) 82: else raise '' 83: end 84: end 85: end
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.
# File lib/data_mapper/callbacks.rb, line 76 76: def execute(name, instance, *args) 77: @callbacks[name].all? do |callback| 78: case callback 79: when String then instance.instance_eval(callback) 80: when Proc then callback[instance, *args] 81: when Symbol then instance.send(callback, *args) 82: else raise '' 83: end 84: end 85: end