Class: Aruba::EventBus
- Inherits:
-
Object
- Object
- Aruba::EventBus
- Defined in:
- lib/aruba/event_bus.rb,
lib/aruba/event_bus/name_resolver.rb
Overview
EventBus
Defined Under Namespace
Classes: NameResolver
Instance Method Summary collapse
-
#initialize(resolver) ⇒ EventBus
constructor
Create EventBus.
-
#notify(event) ⇒ Object
Broadcast an event.
-
#register(event_ids, handler_object = nil) { ... } ⇒ Object
Register for an event.
Constructor Details
#initialize(resolver) ⇒ EventBus
Create EventBus
15 16 17 18 |
# File 'lib/aruba/event_bus.rb', line 15 def initialize(resolver) @resolver = resolver @handlers = Hash.new { |h, k| h[k] = [] } end |
Instance Method Details
#notify(event) ⇒ Object
Broadcast an event
55 56 57 58 59 |
# File 'lib/aruba/event_bus.rb', line 55 def notify(event) raise NoEventError, "Please pass an event object, not a class" if event.is_a?(Class) @handlers[event.class.to_s].each { |handler| handler.call(event) } end |
#register(event_ids, handler_object = nil) { ... } ⇒ Object
Register for an event
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/aruba/event_bus.rb', line 33 def register(event_ids, handler_object = nil, &handler_proc) handler = handler_proc || handler_object if handler.nil? || !handler.respond_to?(:call) raise ArgumentError, "Please pass either an object#call or a handler block" end Array(event_ids).flatten.each do |id| @handlers[ @resolver.transform(id).to_s ] << handler end nil end |