Class: Pokan::EventHandler
- Inherits:
-
Object
- Object
- Pokan::EventHandler
- Defined in:
- lib/pokan/event_handler.rb
Overview
EventHandler is a class that associate blocks with events, making it possible to emit events and executing all the related callbacks (sequentially, in the order they were registered).
Usage
e = EventHandler.new
e.register(:my_event) { puts 'oh my!' }
e.events #=> [:my_event]
e.subscribers(:my_event) #=> Proc array with callbacks
e.emit(:my_event) #=> 'oh my!' is printed
Instance Method Summary collapse
-
#emit(event, options = {}) ⇒ Object
Emits the given event, thus calling all the registered callbacks up to that point.
-
#events ⇒ Object
Retrieves the names of all the events for which there is at least one associated callback.
-
#initialize ⇒ EventHandler
constructor
creates a new instance of EventHandler, ready for event registrations.
-
#register(event, &block) ⇒ Object
register
is the method used to associate a block with an event. -
#reset(event) ⇒ Object
Removes all the callbacks registered with event.
-
#subscribers(event) ⇒ Object
Returns an array with all the Proc objects for callbacks registered for the given event.
Constructor Details
#initialize ⇒ EventHandler
creates a new instance of EventHandler, ready for event registrations
22 23 24 |
# File 'lib/pokan/event_handler.rb', line 22 def initialize @callbacks = {} end |
Instance Method Details
#emit(event, options = {}) ⇒ Object
Emits the given event, thus calling all the registered callbacks up to that point. If there are no registered callbacks for the event passed, nothing is done.
If the registered callbacks expected parameters, pass them using the with
option
e = EventHandler.new
e.register(:awesome) { puts 'awesome!' }
e.register(:awesome) { puts 'not that awesome...' }
e.emit(:awesome) #=> messages are print
Using the +with+ option:
e = EventHandler.new
e.register(:awesome) { |msg| puts msg }
e.emit(:awesome, :with => ['awesome!']) #=> 'awesome!' is print
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pokan/event_handler.rb', line 88 def emit(event, = {}) raise_unless_symbol(event) args = [:with] || [] if @callbacks.has_key? event @callbacks[event].each { |c| c.call(*args) } end self end |
#events ⇒ Object
Retrieves the names of all the events for which there is at least one associated callback
49 50 51 |
# File 'lib/pokan/event_handler.rb', line 49 def events @callbacks.keys end |
#register(event, &block) ⇒ Object
register
is the method used to associate a block with an event. The event name must be passed as a symbol, otherwise an exception is raised. You can call register
on the same event any number of times, but it is important to note that the blocks associated will be called in the same order they were registered.
e = EventHandler.new
e.register(:my_event) { puts 'my event' }.register(:my_event) { puts 'is cool!' }
36 37 38 39 40 41 42 43 44 |
# File 'lib/pokan/event_handler.rb', line 36 def register(event, &block) raise_unless_symbol(event) raise NoBlockGivenError, 'must provide a block' unless block_given? initialize_callbacks_for(event) @callbacks[event] << block self end |
#reset(event) ⇒ Object
Removes all the callbacks registered with event
63 64 65 66 67 68 |
# File 'lib/pokan/event_handler.rb', line 63 def reset(event) raise_unless_symbol(event) @callbacks.delete(event) self end |
#subscribers(event) ⇒ Object
Returns an array with all the Proc objects for callbacks registered for the given event
56 57 58 59 60 |
# File 'lib/pokan/event_handler.rb', line 56 def subscribers(event) raise_unless_symbol(event) @callbacks[event] || [] end |