Class: Midnight::BusinessLogic::EventRouter

Inherits:
EventDispatcher show all
Defined in:
lib/midnight/business_logic/event_router.rb

Instance Method Summary collapse

Constructor Details

#initialize(handlers_map = {}) ⇒ EventRouter

Returns a new instance of EventRouter.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/midnight/business_logic/event_router.rb', line 6

def initialize(handlers_map = {})
  # construction example:
  # EventRouter.new(SomeEvent => [
  #   method(:puts)
  # ])
  begin
    handlers_map.each do |_event_class, handlers|
      Array(handlers).each do |handler|
        reject_handlers unless handler.respond_to?(:call)
      end
    end
  rescue ::NoMethodError => e
    raise e unless e.name == :each
    reject_handlers
  end
  @handlers_map = handlers_map
  @arity_mismatched = /wrong\s+number\s+of\s+arguments/i
  freeze
end

Instance Method Details

#call(event, **metadata) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/midnight/business_logic/event_router.rb', line 26

def call(event, **)
  handlers = @handlers_map[event.class]
  return if handlers.blank?
  super(
    event,
    _event_handlers: Array(handlers),
    **
  )
end