-
-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #930 from cucumber/core-event-bus
Use core cucumber event bus implementation
- Loading branch information
Showing
8 changed files
with
31 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
require "aruba/event_bus/name_resolver" | ||
require "aruba/errors" | ||
require "cucumber/core/event_bus" | ||
|
||
module Aruba | ||
# Event bus | ||
# | ||
# Implements and in-process pub-sub events broadcaster allowing multiple observers | ||
# to subscribe to different events that fire as your tests are executed. | ||
# | ||
class EventBus | ||
# Create EventBus | ||
# | ||
# @param [#transform] resolver | ||
# A resolver which transforms Symbol, String, Class into an event Class. | ||
def initialize(resolver) | ||
@resolver = resolver | ||
@handlers = Hash.new { |h, k| h[k] = [] } | ||
end | ||
|
||
# Register for an event | ||
# | ||
# @param [String, Symbol, Class, Array] event_ids | ||
# If Array, register multiple events witht the same handler. If String, | ||
# Symbol, Class register handler for given event. | ||
# | ||
# @param [#call] handler_object | ||
# The handler object, needs to have method `#call`. Either | ||
# `handler_object` or `block` can be defined. The handler object gets the | ||
# event passed to `#call`. | ||
# | ||
# @yield | ||
# Handler block which gets the event passed as parameter. | ||
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" | ||
class EventBus < Cucumber::Core::EventBus | ||
def register(ids, handler_object = nil, &handler_proc) | ||
Array(ids).each do |event_id| | ||
on(event_id, handler_object, &handler_proc) | ||
end | ||
|
||
Array(event_ids).flatten.each do |id| | ||
@handlers[ | ||
@resolver.transform(id).to_s | ||
] << handler | ||
end | ||
|
||
nil | ||
end | ||
|
||
# Broadcast an event | ||
# | ||
# @param [Object] event | ||
# An object of registered event class. This object is passed to the event | ||
# handler. | ||
# | ||
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) } | ||
broadcast(event) | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.