Class: Yes::Core::CommandHandling::GuardRunner

Inherits:
Object
  • Object
show all
Includes:
OpenTelemetry::Trackable
Defined in:
lib/yes/core/command_handling/guard_runner.rb

Overview

Evaluates guards for commands and manages command-specific errors on aggregates Handles the decision of whether to skip guards and properly sets/clears error states

Examples:

runner = GuardRunner.new(aggregate)
evaluator = runner.call(cmd, command_name, guard_evaluator_class, skip_guards: false)

Instance Method Summary collapse

Constructor Details

#initialize(aggregate) ⇒ GuardRunner

Initializes a new GuardRunner

Parameters:



19
20
21
# File 'lib/yes/core/command_handling/guard_runner.rb', line 19

def initialize(aggregate)
  @aggregate = aggregate
end

Instance Method Details

#call(cmd, command_name, guard_evaluator_class, skip_guards:) ⇒ GuardEvaluator?

Evaluates guards for the command

Parameters:

  • cmd (Yes::Core::Command)

    The command to be handled

  • command_name (Symbol)

    The name of the command being executed

  • guard_evaluator_class (Class)

    The guard evaluator class

  • skip_guards (Boolean)

    Whether to skip guard evaluation

Returns:

  • (GuardEvaluator, nil)

    The guard evaluator instance or nil if guards skipped

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/yes/core/command_handling/guard_runner.rb', line 33

def call(cmd, command_name, guard_evaluator_class, skip_guards:)
  if skip_guards
    clear_command_error(command_name)
    return nil
  end

  evaluator = guard_evaluator_class.new(
    payload: cmd.payload,
    metadata: cmd.,
    aggregate: aggregate,
    command_name: command_name
  )
  evaluator.call

  clear_command_error(command_name)

  evaluator
rescue GuardEvaluator::InvalidTransition,
       GuardEvaluator::NoChangeTransition,
       Yes::Core::Command::Invalid => e
  aggregate.send(:"#{command_name.to_s.underscore}_error=", e.message)
  raise e
end