Sha256: b473786830826a5d6f5b659abd38af265ba8cf0aaa8020d859a9c31c90a6baa3

Contents?: true

Size: 1.77 KB

Versions: 24

Compression:

Stored size: 1.77 KB

Contents

require 'aruba/event_bus/name_resolver'
require 'aruba/errors'

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

      fail ArgumentError, 'Please pass either an object#call or a handler block' if handler.nil? || !handler.respond_to?(:call)

      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)
      fail 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
  end
end

Version data entries

24 entries across 24 versions & 3 rubygems

Version Path
aruba-1.0.0 lib/aruba/event_bus.rb
aruba-0.14.14 lib/aruba/event_bus.rb
aruba-0.14.13 lib/aruba/event_bus.rb
aruba-1.0.0.pre.alpha.5 lib/aruba/event_bus.rb
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/aruba-0.14.12/lib/aruba/event_bus.rb
aruba-0.14.12 lib/aruba/event_bus.rb
aruba-0.14.11 lib/aruba/event_bus.rb
aruba-0.14.10 lib/aruba/event_bus.rb
aruba-1.0.0.pre.alpha.4 lib/aruba/event_bus.rb
aruba-1.0.0.pre.alpha.3 lib/aruba/event_bus.rb
aruba-0.14.9 lib/aruba/event_bus.rb
aruba-0.14.8 lib/aruba/event_bus.rb
aruba-0.14.7 lib/aruba/event_bus.rb
aruba-0.14.6 lib/aruba/event_bus.rb
aruba-0.14.5 lib/aruba/event_bus.rb
aruba-0.14.4 lib/aruba/event_bus.rb
aruba-0.14.3 lib/aruba/event_bus.rb
aruba-1.0.0.pre.alpha.2 lib/aruba/event_bus.rb
aruba-1.0.0.pre.alpha.1 lib/aruba/event_bus.rb
aruba-win-fix-0.14.2 lib/aruba/event_bus.rb