Class: RbMinivents::Events
- Inherits:
-
Object
- Object
- RbMinivents::Events
- Defined in:
- lib/rbminivents.rb
Instance Method Summary collapse
-
#emit(name, *args) ⇒ Object
Emit: send event, callbacks will be triggered.
-
#initialize ⇒ Events
constructor
A new instance of Events.
-
#off(name, handler) ⇒ Object
Off: stop listening to event / specific callback.
-
#on(name, &handler) ⇒ Object
On: listen to events.
Constructor Details
#initialize ⇒ Events
Returns a new instance of Events.
4 5 6 |
# File 'lib/rbminivents.rb', line 4 def initialize @handlers = {} end |
Instance Method Details
#emit(name, *args) ⇒ Object
Emit: send event, callbacks will be triggered
22 23 24 25 |
# File 'lib/rbminivents.rb', line 22 def emit(name, *args) @handlers[name] ||= [] @handlers[name].each { |handler| handler.call(*args) } end |
#off(name, handler) ⇒ Object
Off: stop listening to event / specific callback
16 17 18 19 |
# File 'lib/rbminivents.rb', line 16 def off(name, handler) @handlers[name] ||= [] @handlers[name].delete(handler) end |
#on(name, &handler) ⇒ Object
On: listen to events
9 10 11 12 13 |
# File 'lib/rbminivents.rb', line 9 def on(name, &handler) @handlers[name] ||= [] @handlers[name] << handler handler end |