Class: Yes::Core::Commands::Helper
- Inherits:
-
Object
- Object
- Yes::Core::Commands::Helper
- Defined in:
- lib/yes/core/commands/helper.rb
Overview
Provides naming resolution helpers for commands following the V2 folder structure (e.g. Context::Subject::Commands::DoSomething::Command).
Constant Summary collapse
- AGGREGATE_CLASSNAME =
'Aggregate'- VERSION_REGEXP =
/::(?<version>V\d+)::/
Class Method Summary collapse
-
.splitted_command(cmd) ⇒ Array<String>
Splits the command class name into module parts.
Instance Method Summary collapse
-
#aggregate_class ⇒ Class
Returns the aggregate class constant.
-
#aggregate_classname ⇒ String
Returns the aggregate class name.
-
#aggregate_id ⇒ String
Returns the aggregate ID from the command.
-
#aggregate_module ⇒ String
(also: #subject)
Returns the aggregate module name.
-
#authorizer_classname ⇒ String
Returns the fully qualified authorizer class name.
-
#command_context ⇒ String
(also: #context)
Returns the top-level context module of the command.
-
#command_locale ⇒ Symbol
(also: #locale)
Returns the locale for the command.
-
#command_name ⇒ String
Returns the underscored command name.
-
#command_version ⇒ String?
Extracts the version from the command class name.
-
#event_payload ⇒ Hash
Returns the event payload with stringified keys.
-
#initialize(cmd) ⇒ Helper
constructor
A new instance of Helper.
-
#validator_classname ⇒ String
Returns the fully qualified validator class name.
Constructor Details
#initialize(cmd) ⇒ Helper
Returns a new instance of Helper.
32 33 34 35 |
# File 'lib/yes/core/commands/helper.rb', line 32 def initialize(cmd) @inflector = Dry::Inflector.new @cmd = cmd end |
Class Method Details
.splitted_command(cmd) ⇒ Array<String>
Splits the command class name into module parts.
26 27 28 |
# File 'lib/yes/core/commands/helper.rb', line 26 def splitted_command(cmd) cmd.class.to_s.split('::') end |
Instance Method Details
#aggregate_class ⇒ Class
Returns the aggregate class constant.
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/yes/core/commands/helper.rb', line 108 def aggregate_class inflector.constantize( [ command_context, command_version, aggregate_module, aggregate_classname ].compact.join('::') ) end |
#aggregate_classname ⇒ String
Returns the aggregate class name.
77 78 79 |
# File 'lib/yes/core/commands/helper.rb', line 77 def aggregate_classname AGGREGATE_CLASSNAME end |
#aggregate_id ⇒ String
Returns the aggregate ID from the command.
122 |
# File 'lib/yes/core/commands/helper.rb', line 122 delegate :aggregate_id, to: :cmd |
#aggregate_module ⇒ String Also known as: subject
Returns the aggregate module name.
84 85 86 |
# File 'lib/yes/core/commands/helper.rb', line 84 def aggregate_module splitted_command(cmd)[-4] end |
#authorizer_classname ⇒ String
Returns the fully qualified authorizer class name.
92 93 94 95 |
# File 'lib/yes/core/commands/helper.rb', line 92 def spl = splitted_command(cmd) spl[0] == 'CommandGroups' ? "#{spl[0..1].join('::')}::Authorizer" : "#{spl[0..3].join('::')}::Authorizer" end |
#command_context ⇒ String Also known as: context
Returns the top-level context module of the command.
40 41 42 |
# File 'lib/yes/core/commands/helper.rb', line 40 def command_context splitted_command(cmd).first end |
#command_locale ⇒ Symbol Also known as: locale
Returns the locale for the command.
48 49 50 |
# File 'lib/yes/core/commands/helper.rb', line 48 def command_locale cmd.respond_to?(:locale) ? cmd.locale : I18n.locale end |
#command_name ⇒ String
Returns the underscored command name.
70 71 72 |
# File 'lib/yes/core/commands/helper.rb', line 70 def command_name inflector.underscore(splitted_command(cmd)[-2]) end |
#command_version ⇒ String?
Extracts the version from the command class name.
56 57 58 |
# File 'lib/yes/core/commands/helper.rb', line 56 def command_version VERSION_REGEXP.match(cmd.class.to_s)&.[](:version) end |
#event_payload ⇒ Hash
Returns the event payload with stringified keys.
63 64 65 |
# File 'lib/yes/core/commands/helper.rb', line 63 def event_payload cmd.payload.deep_stringify_keys end |
#validator_classname ⇒ String
Returns the fully qualified validator class name.
100 101 102 103 |
# File 'lib/yes/core/commands/helper.rb', line 100 def validator_classname spl = splitted_command(cmd) spl[0] == 'CommandGroups' ? "#{spl[0..1].join('::')}::Validator" : "#{spl[0..3].join('::')}::Validator" end |