Class: Yes::Core::CommandHandling::StateUpdater
- Inherits:
-
Object
- Object
- Yes::Core::CommandHandling::StateUpdater
- Defined in:
- lib/yes/core/command_handling/state_updater.rb
Overview
Base class for handling custom state updates on command attributes
Defined Under Namespace
Classes: BlockAnalyzer
Class Attribute Summary collapse
-
.update_state_block ⇒ Hash
readonly
The update state block.
-
.updated_attributes ⇒ Object
readonly
Returns the value of attribute updated_attributes.
Class Method Summary collapse
-
.update_state(custom: false) { ... } ⇒ void
Defines the update state block and analyzes it for attribute updates.
Instance Method Summary collapse
-
#call ⇒ Hash
Evaluates the update state block and returns the updated attributes If no block is defined, returns the payload attributes except aggregate_id.
-
#initialize(payload:, aggregate:, event: nil) ⇒ StateUpdater
constructor
A new instance of StateUpdater.
Constructor Details
#initialize(payload:, aggregate:, event: nil) ⇒ StateUpdater
Returns a new instance of StateUpdater.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 57 def initialize(payload:, aggregate:, event: nil) @raw_payload = payload @aggregate = aggregate @event = event @payload = PayloadProxy.new( raw_payload:, context: aggregate.class.context, 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
92 93 94 95 96 97 98 99 100 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 92 def method_missing(method_name, *, &block) if block @updates[method_name] = instance_eval(&block) elsif aggregate.respond_to?(method_name) aggregate.public_send(method_name, *, &block) else super end end |
Class Attribute Details
.update_state_block ⇒ Hash (readonly)
Returns The update state block.
10 11 12 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 10 def update_state_block @update_state_block end |
.updated_attributes ⇒ Object (readonly)
Returns the value of attribute updated_attributes.
11 12 13 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 11 def updated_attributes @updated_attributes end |
Class Method Details
.update_state(custom: false) { ... } ⇒ void
This method returns an undefined value.
Defines the update state block and analyzes it for attribute updates
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 19 def update_state(custom: false, &block) @update_state_block = block @updated_attributes = [] # Only analyze the block if not in custom mode return if custom analyzer = BlockAnalyzer.new analyzer.instance_eval(&block) @updated_attributes = analyzer.updated_attributes end |
Instance Method Details
#call ⇒ Hash
Evaluates the update state block and returns the updated attributes If no block is defined, returns the payload attributes except aggregate_id
72 73 74 75 76 77 78 79 80 |
# File 'lib/yes/core/command_handling/state_updater.rb', line 72 def call if self.class.update_state_block @updates = {} instance_eval(&self.class.update_state_block) @updates else raw_payload.except(:"#{aggregate.class.aggregate.underscore}_id") end end |