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.



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

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

Instance Method Details

#call(command) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/ruby_event_store/correlated_commands.rb', line 13

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) { command_bus.call(command) }
  else
    event_store.(correlation_id: command.message_id, causation_id: command.message_id) do
      command_bus.call(command)
    end
  end
end