Class: Yes::Core::CommandHandling::GuardEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/command_handling/guard_evaluator.rb

Overview

Base class for evaluating guards on command attributes

Defined Under Namespace

Classes: InvalidTransition, NoChangeTransition, TransitionError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload:, metadata:, aggregate:, command_name:) ⇒ GuardEvaluator

Returns a new instance of GuardEvaluator.

Parameters:

  • payload (Hash)

    The command payload

  • metadata (Hash)

    The command metadata

  • aggregate (Yes::Core::Aggregate)

    The aggregate instance

  • command_name (Symbol)

    The command name



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 34

def initialize(payload:, metadata:, aggregate:, command_name:)
  @raw_payload = payload
  @raw_metadata = 
  @aggregate = aggregate
  @aggregate_tracker = AggregateTracker.new
  @command_name = command_name
  @payload = PayloadProxy.new(
    raw_payload:,
    raw_metadata:,
    context: aggregate.class.context,
    aggregate_tracker:,
    parent_aggregates: aggregate.class.parent_aggregates
  )
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) { ... } ⇒ Object (private)

Handles method missing to delegate attribute calls to the current aggregate

Parameters:

  • method_name (Symbol)

    The method name being called

Yields:

  • Optional block passed to the method (unused)

Yield Returns:

  • (void)

Returns:

  • (Object)

    The result of calling the method on the current aggregate



108
109
110
111
112
113
114
115
116
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 108

def method_missing(method_name, *, &)
  if aggregate.respond_to?(method_name)
    result = aggregate.public_send(method_name, *, &)
    track_external_aggregate(method_name, result) if aggregate_attribute?(method_name)
    result
  else
    super
  end
end

Class Method Details

.guard(name, error_extra: {}) { ... } ⇒ void

This method returns an undefined value.

Defines a new guard with a name and evaluation block

Parameters:

  • name (Symbol)

    Name of the guard

  • error_extra (Hash, Proc) (defaults to: {})

    The extra information to be added to the error message payload

Yields:

  • Block to evaluate the guard condition

Yield Returns:

  • (Boolean)

    True if the guard passes, false otherwise



25
26
27
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 25

def guard(name, error_extra: {}, &block)
  guards[name] = { block:, error_extra: }
end

.guardsHash<Symbol, Proc>

Returns Hash of registered guards with names as keys and blocks as values.

Returns:

  • (Hash<Symbol, Proc>)

    Hash of registered guards with names as keys and blocks as values



14
15
16
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 14

def guards
  @guards ||= {}
end

Instance Method Details

#accessed_external_aggregatesArray<Hash>

Returns List of accessed external aggregates with their revisions.

Returns:

  • (Array<Hash>)

    List of accessed external aggregates with their revisions



61
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 61

delegate :accessed_external_aggregates, to: :aggregate_tracker

#callvoid

This method returns an undefined value.

Evaluates all registered guards

Raises:



54
55
56
57
58
# File 'lib/yes/core/command_handling/guard_evaluator.rb', line 54

def call
  self.class.guards.each do |name, guard_data|
    evaluate_guard(name, error_extra: guard_data[:error_extra], block: guard_data[:block])
  end
end