Class: Lieutenant::EventBus

Inherits:
Object
  • Object
show all
Defined in:
lib/lieutenant/event_bus.rb

Overview

Publishes and receives messages from the aggregates updates

Instance Method Summary collapse

Constructor Details

#initializeEventBus

Returns a new instance of EventBus.



6
7
8
# File 'lib/lieutenant/event_bus.rb', line 6

def initialize
  @handlers = Hash.new { [] }
end

Instance Method Details

#publish(event) ⇒ Object



16
17
18
# File 'lib/lieutenant/event_bus.rb', line 16

def publish(event)
  handlers[event.class].each { |handler| handler.call(event) }
end

#subscribe(*event_classes, &handler) ⇒ Object



10
11
12
13
14
# File 'lib/lieutenant/event_bus.rb', line 10

def subscribe(*event_classes, &handler)
  event_classes.each do |event_class|
    handlers[event_class] = handlers[event_class].push(handler)
  end
end