Class: Callback::CallbackContainer

Inherits:
Object
  • Object
show all
Defined in:
lib/callback/callback_container.rb

Instance Method Summary collapse

Constructor Details

#initializeCallbackContainer

Returns a new instance of CallbackContainer.



3
4
5
6
7
# File 'lib/callback/callback_container.rb', line 3

def initialize
  @callback_registry = Hash.new do |hash, key|
    hash[key] = Array.new
  end
end

Instance Method Details

#clearObject



35
36
37
# File 'lib/callback/callback_container.rb', line 35

def clear
  @callback_registry.clear
end

#define(key, callback_proc = nil, &callback_block) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/callback/callback_container.rb', line 9

def define(key, callback_proc=nil, &callback_block)
  callback = extract_callback(callback_block, callback_proc) do
    raise "You must define the callback that accepts the call method."
  end
  @callback_registry[key] << callback
  callback
end

#notify(key, *args, &error_handler) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/callback/callback_container.rb', line 25

def notify(key, *args, &error_handler)
  @callback_registry[key].collect do |callback|
    begin
      callback.call(*args)
    rescue Exception => e
      yield(e) if error_handler
    end
  end
end

#undefine(key, callback_proc) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/callback/callback_container.rb', line 17

def undefine(key, callback_proc)
  callback = extract_callback(callback_proc) do
    raise "You may only undefine callbacks that use the call method."
  end
  @callback_registry[key].delete callback
  callback
end