Class | Callback::CallbackContainer |
In: |
lib/spec/callback/callback_container.rb
|
Parent: | Object |
# File lib/spec/callback/callback_container.rb, line 3 3: def initialize 4: @callback_registry = Hash.new do |hash, key| 5: hash[key] = Array.new 6: end 7: end
Clears all of the callbacks in this container.
# File lib/spec/callback/callback_container.rb, line 42 42: def clear 43: @callback_registry.clear 44: end
Defines the callback with the key in this container.
# File lib/spec/callback/callback_container.rb, line 10 10: def define(key, callback_proc=nil, &callback_block) 11: callback = extract_callback(callback_block, callback_proc) do 12: raise "You must define the callback that accepts the call method." 13: end 14: @callback_registry[key] << callback 15: callback 16: end
Notifies the callbacks for the key. Arguments may be passed. An error handler may be passed in as a block. If there is an error, the block is called with error object as an argument. An array of the return values of the callbacks is returned.
# File lib/spec/callback/callback_container.rb, line 31 31: def notify(key, *args, &error_handler) 32: @callback_registry[key].collect do |callback| 33: begin 34: callback.call(*args) 35: rescue Exception => e 36: yield(e) if error_handler 37: end 38: end 39: end
Undefines the callback with the key in this container.
# File lib/spec/callback/callback_container.rb, line 19 19: def undefine(key, callback_proc) 20: callback = extract_callback(callback_proc) do 21: raise "You may only undefine callbacks that use the call method." 22: end 23: @callback_registry[key].delete callback 24: callback 25: end
# File lib/spec/callback/callback_container.rb, line 47 47: def extract_callback(first_choice_callback, second_choice_callback = nil) 48: callback = nil 49: if first_choice_callback 50: callback = first_choice_callback 51: elsif second_choice_callback 52: callback = second_choice_callback 53: end 54: unless callback.respond_to? :call 55: yield 56: end 57: return callback 58: end