Class: Midnight::BusinessLogic::EventDispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/midnight/business_logic/event_dispatcher.rb

Direct Known Subclasses

EventQueue, EventRouter

Instance Method Summary collapse

Constructor Details

#initialize(*handlers) ⇒ EventDispatcher

Returns a new instance of EventDispatcher.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

Instance Method Details

#call(events, _event_handlers: @handlers, **metadata) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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,
  **
)
  _event_handlers.each do |handler|
    handler.call(events, **)
  rescue ::ArgumentError => e
    raise e unless @arity_mismatched =~ e.message
    handler.call(events)
  end
end