Class: Yes::Core::Commands::Group
- Inherits:
-
Object
- Object
- Yes::Core::Commands::Group
- 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
-
.commands ⇒ Array<Class>
readonly
List of command classes used in this group.
Instance Attribute Summary collapse
-
#commands ⇒ Array<Command>
readonly
Command instances of the group’s commands.
-
#group_attributes ⇒ Attributes
readonly
The attributes of the command group.
-
#payload ⇒ Hash
readonly
The payload of the command group.
Class Method Summary collapse
-
.command(command_name, context: to_s.split('::')[0], subject: to_s.split('::')[1]) ⇒ void
Defines a command for the command group.
-
.command_contexts ⇒ Array<Symbol>
List of unique command contexts.
-
.own_context ⇒ Symbol
The context of the current command group.
-
.own_context_subjects ⇒ Array<Symbol>
List of subjects in the current context.
-
.own_subject ⇒ Symbol
The subject of the current command group.
Instance Method Summary collapse
-
#aggregate_id ⇒ String?
Returns the aggregate ID from the first command in the group.
-
#initialize(params) ⇒ Group
constructor
Initialize a new Group.
-
#to_h ⇒ Hash
Returns the command group as a hash for serialization.
Constructor Details
#initialize(params) ⇒ Group
Initialize a new Group.
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
.commands ⇒ Array<Class> (readonly)
Returns 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
#commands ⇒ Array<Command> (readonly)
Returns Command instances of the group’s commands.
79 80 81 |
# File 'lib/yes/core/commands/group.rb', line 79 def commands @commands end |
#group_attributes ⇒ Attributes (readonly)
Returns The attributes of the command group.
76 77 78 |
# File 'lib/yes/core/commands/group.rb', line 76 def group_attributes @group_attributes end |
#payload ⇒ Hash (readonly)
Returns 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.
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_contexts ⇒ Array<Symbol>
Returns 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_context ⇒ Symbol
Returns 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_subjects ⇒ Array<Symbol>
Returns 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_subject ⇒ Symbol
Returns 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_id ⇒ String?
Returns the aggregate ID from the first command in the group.
86 87 88 |
# File 'lib/yes/core/commands/group.rb', line 86 def aggregate_id commands.first&.aggregate_id end |
#to_h ⇒ Hash
Returns the command group as a hash for serialization.
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 |