Class: Yes::Core::CommandHandling::StateUpdater

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload:, aggregate:, event: nil) ⇒ StateUpdater

Returns a new instance of StateUpdater.

Parameters:

  • payload (Hash)

    The command payload

  • aggregate (Yes::Core::Aggregate)

    The aggregate instance

  • event (Event, nil) (defaults to: nil)

    The event instance (optional)



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

Parameters:

  • method_name (Symbol)

    The method name being called

Yields:

  • Optional block to evaluate for the attribute value

Yield Returns:

  • (Object)

    The value to set for the attribute

Returns:

  • (Object)

    The result of calling the method on 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_blockHash (readonly)

Returns The update state block.

Returns:

  • (Hash)

    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_attributesObject (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

Parameters:

  • custom (Boolean) (defaults to: false)

    Skip attribute analysis when true

Yields:

  • Block to evaluate for state updates

Yield Returns:

  • (void)


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

#callHash

Evaluates the update state block and returns the updated attributes If no block is defined, returns the payload attributes except aggregate_id

Returns:

  • (Hash)

    The updated attributes



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