Class: RubyEventStore::CorrelatedCommands

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/correlated_commands.rb

Defined Under Namespace

Classes: MiniEvent

Instance Method Summary collapse

Constructor Details

#initialize(event_store, command_bus) ⇒ CorrelatedCommands

Returns a new instance of CorrelatedCommands.


6
7
8
9
# File 'lib/ruby_event_store/correlated_commands.rb', line 6

def initialize(event_store, command_bus)
  @event_store = event_store
  @command_bus = command_bus
end

Instance Method Details

#call(command) ⇒ Object


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_event_store/correlated_commands.rb', line 14

def call(command)
  correlation_id = event_store.[:correlation_id]
  causation_id   = event_store.[:causation_id]

  if correlation_id && causation_id
    command.correlate_with(MiniEvent.new(
      correlation_id,
      causation_id,
    )) if command.respond_to?(:correlate_with)
    event_store.(
      causation_id: command.message_id,
    ) do
      command_bus.call(command)
    end
  else
    event_store.(
      correlation_id: command.message_id,
      causation_id: command.message_id,
    ) do
      command_bus.call(command)
    end
  end
end