Class: SagaForge::Execution::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/saga_forge/execution/facade.rb

Overview

The saga object yielded to forward blocks (§A.1 verbs). Everything is staged in memory; the Runner commits it.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:, correlation_id:, current_state:, context:) ⇒ Facade

Returns a new instance of Facade.



14
15
16
17
18
19
20
21
# File 'lib/saga_forge/execution/facade.rb', line 14

def initialize(definition:, correlation_id:, current_state:, context:)
  @definition = definition
  @correlation_id = correlation_id
  @current_state = current_state
  @context = context
  @staged_publishes = []
  @outcome = nil
end

Instance Attribute Details

#contextObject (readonly)

Verb semantics: last verb call wins — calling transition_to twice simply overwrites @outcome with whatever ran last, and the block keeps executing. fail! is the one exception: it both records its outcome AND throws :saga_forge_fail, short-circuiting the rest of the block immediately. Every other verb is just a plain method call with no control-flow effect.



12
13
14
# File 'lib/saga_forge/execution/facade.rb', line 12

def context
  @context
end

#correlation_idObject (readonly)

Verb semantics: last verb call wins — calling transition_to twice simply overwrites @outcome with whatever ran last, and the block keeps executing. fail! is the one exception: it both records its outcome AND throws :saga_forge_fail, short-circuiting the rest of the block immediately. Every other verb is just a plain method call with no control-flow effect.



12
13
14
# File 'lib/saga_forge/execution/facade.rb', line 12

def correlation_id
  @correlation_id
end

#current_stateObject (readonly)

Verb semantics: last verb call wins — calling transition_to twice simply overwrites @outcome with whatever ran last, and the block keeps executing. fail! is the one exception: it both records its outcome AND throws :saga_forge_fail, short-circuiting the rest of the block immediately. Every other verb is just a plain method call with no control-flow effect.



12
13
14
# File 'lib/saga_forge/execution/facade.rb', line 12

def current_state
  @current_state
end

#outcomeObject (readonly)

Verb semantics: last verb call wins — calling transition_to twice simply overwrites @outcome with whatever ran last, and the block keeps executing. fail! is the one exception: it both records its outcome AND throws :saga_forge_fail, short-circuiting the rest of the block immediately. Every other verb is just a plain method call with no control-flow effect.



12
13
14
# File 'lib/saga_forge/execution/facade.rb', line 12

def outcome
  @outcome
end

#staged_publishesObject (readonly)

Verb semantics: last verb call wins — calling transition_to twice simply overwrites @outcome with whatever ran last, and the block keeps executing. fail! is the one exception: it both records its outcome AND throws :saga_forge_fail, short-circuiting the rest of the block immediately. Every other verb is just a plain method call with no control-flow effect.



12
13
14
# File 'lib/saga_forge/execution/facade.rb', line 12

def staged_publishes
  @staged_publishes
end

Instance Method Details

#fail!(reason: nil) ⇒ Object



31
32
33
34
# File 'lib/saga_forge/execution/facade.rb', line 31

def fail!(reason: nil)
  @outcome = [:fail, reason]
  throw :saga_forge_fail
end

#publish(event_name, **payload) ⇒ Object

Staged publish (§A.2): resolve recipients NOW (call-site stack trace, MissingCorrelationError surfaces under the block's retry policy), hold fully-built rows; the Runner inserts them inside its commit.



39
40
41
42
# File 'lib/saga_forge/execution/facade.rb', line 39

def publish(event_name, **payload)
  @staged_publishes.concat(Router.resolve(event_name, payload))
  nil
end

#transition_to(state) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/saga_forge/execution/facade.rb', line 23

def transition_to(state)
  unless @definition.declared?(state)
    raise UnknownStateError, "transition_to #{state.inspect} — undeclared state"
  end
  @outcome = [:transition_to, state.to_sym]
  nil
end