class Midnight::BusinessLogic::EventDispatcher

Public Class Methods

new(*handlers) click to toggle source
# File lib/midnight/business_logic/event_dispatcher.rb, line 4
def initialize(*handlers)
  handlers = handlers.map do |handler|
    if handler.respond_to?(:arity)
      handler
    else
      handler.method(:call)
    end
  rescue ::NameError
    reject_handlers
  end
  @handlers = handlers
  @arity_mismatched = /wrong\s+number\s+of\s+arguments/i
  freeze
end

Public Instance Methods

call(\ events, _event_handlers: @handlers, **metadata ) click to toggle source
# File lib/midnight/business_logic/event_dispatcher.rb, line 19
def call(\
  events,
  # consumers are forbidden
  # to pass metadata with _event_handlers as a key
  _event_handlers: @handlers,
  **metadata
)
  _event_handlers.each do |handler|
    handler.call(events, **metadata)
  rescue ::ArgumentError => e
    raise e unless @arity_mismatched =~ e.message
    handler.call(events)
  end
end

Private Instance Methods

reject_handlers() click to toggle source
# File lib/midnight/business_logic/event_dispatcher.rb, line 36
def reject_handlers
  raise ::ArgumentError, 'Invalid event handler'
end