Class: Cucumber::Core::EventBus
- Inherits:
-
Object
- Object
- Cucumber::Core::EventBus
- Defined in:
- lib/cucumber/core/event_bus.rb
Overview
Event Bus
Implements an in-process pub-sub event broadcaster allowing multiple observers to subscribe to events that fire as your tests are executed.
Instance Attribute Summary collapse
-
#event_types ⇒ Object
readonly
Returns the value of attribute event_types.
Instance Method Summary collapse
-
#broadcast(event) ⇒ Object
Broadcast an event.
-
#initialize(registry = Events.registry) ⇒ EventBus
constructor
A new instance of EventBus.
- #method_missing(event_id, *args) ⇒ Object
-
#on(event_id, handler_object = nil, &handler_proc) ⇒ Object
Register for an event.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(event_id, *args) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/cucumber/core/event_bus.rb', line 35 def method_missing(event_id, *args) event_class = event_types.fetch(event_id) { super } broadcast event_class.new(*args) rescue NameError => error raise error, error. + "\nDid you get the ID of the event wrong? Try one of these:\n#{event_types.keys.join("\n")}", error.backtrace end |
Instance Attribute Details
#event_types ⇒ Object (readonly)
Returns the value of attribute event_types.
12 13 14 |
# File 'lib/cucumber/core/event_bus.rb', line 12 def event_types @event_types end |
Instance Method Details
#broadcast(event) ⇒ Object
Broadcast an event
30 31 32 33 |
# File 'lib/cucumber/core/event_bus.rb', line 30 def broadcast(event) raise ArgumentError, "Event type #{event.class} is not registered. Try one of these:\n#{event_types.values.join("\n")}" unless is_registered_type?(event.class) handlers_for(event.class).each { |handler| handler.call(event) } end |
#on(event_id, handler_object = nil, &handler_proc) ⇒ Object
Register for an event. The handler proc will be called back with each of the attributes of the event.
22 23 24 25 26 27 |
# File 'lib/cucumber/core/event_bus.rb', line 22 def on(event_id, handler_object = nil, &handler_proc) handler = handler_proc || handler_object validate_handler_and_event_id!(handler, event_id) event_class = event_types[event_id] handlers_for(event_class) << handler end |