Class: Yes::Core::Aggregate::Dsl::MethodDefiners::Command::Command

Inherits:
Base
  • Object
show all
Defined in:
lib/yes/core/aggregate/dsl/method_definers/command/command.rb

Overview

Defines a command method on the aggregate class

Since:

  • 0.1.0

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Yes::Core::Aggregate::Dsl::MethodDefiners::Command::Base

Instance Method Details

#callvoid

This method returns an undefined value.

Since:

  • 0.1.0



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yes/core/aggregate/dsl/method_definers/command/command.rb', line 12

def call
  command_name = @name

  aggregate_class.define_method(command_name) do |payload = nil, **options|
    payload = payload.clone if payload.is_a?(Hash)

    # Extract and remove guards option from options, default to true
    guards = options.delete(:guards)
    guards = true if guards.nil?

    # Extract and remove metadata option from options if present
     = options.delete(:metadata)

    # Handle different calling patterns:
    # 1. command(value) or command(value, guards: false) - shorthand form with single value
    # 2. command({attr: value}) or command({attr: value}, guards: false) - hash form
    # 3. command(attr: value) - ALL kwargs are treated as payload (no options)
    # 4. command() - no arguments (for commands without payload)

    # If no positional argument was provided but kwargs were given (after removing guards/metadata),
    # treat all remaining kwargs as payload
    if payload.nil? && !options.empty?
      payload = options
      {}
    elsif payload.nil?
      payload = {}
    end

    # Pass metadata to CommandHandler which will merge it into the event metadata
    Yes::Core::CommandHandling::CommandHandler.new(self).call(command_name, payload, guards:, metadata:)
  end
end