Class: Acta::Command

Inherits:
Model
  • Object
show all
Defined in:
lib/acta/command.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

attribute, from_acta_hash, #to_acta_hash

Constructor Details

#initialize(**params) ⇒ Command

Returns a new instance of Command.

Raises:



27
28
29
30
# File 'lib/acta/command.rb', line 27

def initialize(**params)
  super
  raise InvalidCommand, self unless valid?
end

Class Method Details

.call(**params) ⇒ Object

Instantiate the command with the given params, run it, and return the command instance. Callers that need to know what the command emitted read it back off the instance:

cmd = CreateOrder.call(customer_id: "c_1")
cmd.emitted_events                              # => [<OrderCreated …>]
cmd.emitted_events.find { _1.is_a?(OrderCreated) }.order_id

Returning the instance keeps the framework honest about multiplicity — commands can emit zero, one, or many events, and the caller (who knows the domain) picks what matters. The framework does not invent a “primary” event.



20
21
22
23
24
# File 'lib/acta/command.rb', line 20

def call(**params)
  instance = new(**params)
  instance.call
  instance
end

Instance Method Details

#emit(event, if_version: nil) ⇒ Object

Emit an event. Pass ‘if_version:` to assert the stream’s current high-water mark for optimistic locking — see Acta.version_of.



34
35
36
37
38
# File 'lib/acta/command.rb', line 34

def emit(event, if_version: nil)
  Acta.emit(event, if_version: if_version)
  emitted_events << event
  event
end

#emitted_eventsObject

Every event emitted during this command instance’s invocation, in the order ‘emit` was called. Empty until #call runs; cascading commands invoked from inside #call produce events in their own instances, not this one.



44
45
46
# File 'lib/acta/command.rb', line 44

def emitted_events
  @emitted_events ||= []
end