Class: Yes::Core::Commands::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/yes/core/commands/group.rb

Overview

Represents a group of commands executed in a transaction. Provides DSL for defining which commands belong to the group and handles payload normalization across contexts and subjects.

Defined Under Namespace

Classes: Attributes

Constant Summary collapse

RESERVED_KEYS =
Yes::Core::Command::RESERVED_KEYS

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Group

Initialize a new Group.

Parameters:

  • params (Hash)

    Parameters for the command group (meta attributes and command payload)



93
94
95
96
97
98
99
100
101
# File 'lib/yes/core/commands/group.rb', line 93

def initialize(params)
  @group_attributes = Attributes.new(params.slice(*Yes::Core::Command::RESERVED_KEYS))
  @payload = normalized_payloads(params)
  @commands = self.class.commands.map do |command|
    command.new(
      payload.dig(command.to_s.split('::')[0].underscore.to_sym, command.to_s.split('::')[1].underscore.to_sym)
    )
  end
end

Class Attribute Details

.commandsArray<Class> (readonly)

Returns List of command classes used in this group.

Returns:

  • (Array<Class>)

    List of command classes used in this group



33
34
35
# File 'lib/yes/core/commands/group.rb', line 33

def commands
  @commands
end

Instance Attribute Details

#commandsArray<Command> (readonly)

Returns Command instances of the group’s commands.

Returns:

  • (Array<Command>)

    Command instances of the group’s commands



79
80
81
# File 'lib/yes/core/commands/group.rb', line 79

def commands
  @commands
end

#group_attributesAttributes (readonly)

Returns The attributes of the command group.

Returns:

  • (Attributes)

    The attributes of the command group



76
77
78
# File 'lib/yes/core/commands/group.rb', line 76

def group_attributes
  @group_attributes
end

#payloadHash (readonly)

Returns The payload of the command group.

Returns:

  • (Hash)

    The payload of the command group



73
74
75
# File 'lib/yes/core/commands/group.rb', line 73

def payload
  @payload
end

Class Method Details

.command(command_name, context: to_s.split('::')[0], subject: to_s.split('::')[1]) ⇒ void

This method returns an undefined value.

Defines a command for the command group.

Parameters:

  • command_name (String)

    the command class name, e.g. ‘NameChanged’

  • context (String) (defaults to: to_s.split('::')[0])

    the context of the command, defaults to the first module

  • subject (String) (defaults to: to_s.split('::')[1])

    the subject of the command, defaults to the second module



63
64
65
66
67
68
69
# File 'lib/yes/core/commands/group.rb', line 63

def command(command_name, context: to_s.split('::')[0], subject: to_s.split('::')[1])
  @commands ||= []
  @commands <<
    Object.const_get(
      "#{context}::#{subject}::Commands::#{command_name}::Command"
    )
end

.command_contextsArray<Symbol>

Returns List of unique command contexts.

Returns:

  • (Array<Symbol>)

    List of unique command contexts



36
37
38
# File 'lib/yes/core/commands/group.rb', line 36

def command_contexts
  commands.map { _1.to_s.split('::')[0].underscore.to_sym }.uniq
end

.own_contextSymbol

Returns The context of the current command group.

Returns:

  • (Symbol)

    The context of the current command group



48
49
50
# File 'lib/yes/core/commands/group.rb', line 48

def own_context
  to_s.split('::')[0].underscore.to_sym
end

.own_context_subjectsArray<Symbol>

Returns List of subjects in the current context.

Returns:

  • (Array<Symbol>)

    List of subjects in the current context



41
42
43
44
45
# File 'lib/yes/core/commands/group.rb', line 41

def own_context_subjects
  commands.
    select { _1.to_s.split('::')[0].underscore.to_sym == own_context }.
    map { _1.to_s.split('::')[1].underscore.to_sym }.uniq
end

.own_subjectSymbol

Returns The subject of the current command group.

Returns:

  • (Symbol)

    The subject of the current command group



53
54
55
# File 'lib/yes/core/commands/group.rb', line 53

def own_subject
  to_s.split('::')[1].underscore.to_sym
end

Instance Method Details

#aggregate_idString?

Returns the aggregate ID from the first command in the group.

Returns:

  • (String, nil)

    the aggregate ID



86
87
88
# File 'lib/yes/core/commands/group.rb', line 86

def aggregate_id
  commands.first&.aggregate_id
end

#to_hHash

Returns the command group as a hash for serialization.

Returns:

  • (Hash)

    payloads and meta attributes merged



106
107
108
109
110
# File 'lib/yes/core/commands/group.rb', line 106

def to_h
  transaction = group_attributes.transaction
  merged = payload.merge(group_attributes.to_h)
  transaction ? merged.merge(transaction:) : merged
end