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.
Class Method Summary collapse
-
.build_registry(*types) ⇒ Hash{Symbol => Class}
Build an event registry to be passed to the EventBus constructor from a list of types.
-
.registry ⇒ Object
The registry contains all the event registered in the core, that will be used by the EventBus by default.
Instance Method Summary collapse
- #broadcast(event) ⇒ Object
-
#initialize(registry = self.class.registry) ⇒ EventBus
constructor
A new instance of EventBus.
- #method_missing(event_id) ⇒ Object
-
#on(event_id, handler_object = nil, &handler_proc) ⇒ Object
Register for an event.
- #respond_to_missing?(event_id, *args) ⇒ Boolean
Constructor Details
#initialize(registry = self.class.registry) ⇒ EventBus
Returns a new instance of EventBus.
37 38 39 40 41 |
# File 'lib/cucumber/core/event_bus.rb', line 37 def initialize(registry = self.class.registry) @event_types = registry.freeze @handlers = {} @event_queue = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(event_id) ⇒ Object
59 60 61 62 |
# File 'lib/cucumber/core/event_bus.rb', line 59 def method_missing(event_id, *) event_class = event_types.fetch(event_id) { super } broadcast event_class.new(*) 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 |
Class Method Details
.build_registry(*types) ⇒ Hash{Symbol => Class}
Build an event registry to be passed to the Cucumber::Core::EventBus constructor from a list of types.
Each type must respond to event_id so that it can be added to the registry hash
32 33 34 |
# File 'lib/cucumber/core/event_bus.rb', line 32 def self.build_registry(*types) types.to_h { |type| [type.event_id, type] } end |
.registry ⇒ Object
The registry contains all the event registered in the core, that will be used by the Cucumber::Core::EventBus by default.
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/cucumber/core/event_bus.rb', line 15 def self.registry build_registry( Event::Envelope, Event::GherkinSourceParsed, Event::TestCaseCreated, Event::TestCaseStarted, Event::TestCaseFinished, Event::TestStepCreated, Event::TestStepStarted, Event::TestStepFinished ) end |
Instance Method Details
#broadcast(event) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/cucumber/core/event_bus.rb', line 52 def broadcast(event) raise ArgumentError, "Event type #{event.class} is not registered. Try one of these:\n#{event_types.values.join("\n")}" unless registered_type?(event.class) handlers_for(event.class).each { |handler| handler.call(event) } @event_queue << 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
44 45 46 47 48 49 50 |
# File 'lib/cucumber/core/event_bus.rb', line 44 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 broadcast_queued_events_to handler, event_class end |
#respond_to_missing?(event_id, *args) ⇒ Boolean
64 65 66 |
# File 'lib/cucumber/core/event_bus.rb', line 64 def respond_to_missing?(event_id, *args) event_types.key?(event_id) || super end |