Class: Yes::Core::CommandHandling::GuardEvaluator
- Inherits:
-
Object
- Object
- Yes::Core::CommandHandling::GuardEvaluator
- 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
-
.guard(name, error_extra: {}) { ... } ⇒ void
Defines a new guard with a name and evaluation block.
-
.guards ⇒ Hash<Symbol, Proc>
Hash of registered guards with names as keys and blocks as values.
Instance Method Summary collapse
-
#accessed_external_aggregates ⇒ Array<Hash>
List of accessed external aggregates with their revisions.
-
#call ⇒ void
Evaluates all registered guards.
-
#initialize(payload:, metadata:, aggregate:, command_name:) ⇒ GuardEvaluator
constructor
A new instance of GuardEvaluator.
Constructor Details
#initialize(payload:, metadata:, aggregate:, command_name:) ⇒ GuardEvaluator
Returns a new instance of GuardEvaluator.
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
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
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 |
.guards ⇒ Hash<Symbol, Proc>
Returns 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_aggregates ⇒ Array<Hash>
Returns 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 |
#call ⇒ void
This method returns an undefined value.
Evaluates all registered guards
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 |