Class: Yes::Core::Commands::Helper

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

Examples:

helper = Yes::Core::Commands::Helper.new(command)
helper.command_name

Constant Summary collapse

AGGREGATE_CLASSNAME =
'Aggregate'
VERSION_REGEXP =
/::(?<version>V\d+)::/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cmd) ⇒ Helper

Returns a new instance of Helper.

Parameters:



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.

Parameters:

Returns:

  • (Array<String>)

    the split class name 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_classClass

Returns the aggregate class constant.

Returns:

  • (Class)

    the aggregate class



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_classnameString

Returns the aggregate class name.

Returns:

  • (String)

    the aggregate class name



77
78
79
# File 'lib/yes/core/commands/helper.rb', line 77

def aggregate_classname
  AGGREGATE_CLASSNAME
end

#aggregate_idString

Returns the aggregate ID from the command.

Returns:

  • (String)

    the aggregate ID



122
# File 'lib/yes/core/commands/helper.rb', line 122

delegate :aggregate_id, to: :cmd

#aggregate_moduleString Also known as: subject

Returns the aggregate module name.

Returns:

  • (String)

    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_classnameString

Returns the fully qualified authorizer class name.

Returns:

  • (String)

    the authorizer class name



92
93
94
95
# File 'lib/yes/core/commands/helper.rb', line 92

def authorizer_classname
  spl = splitted_command(cmd)
  spl[0] == 'CommandGroups' ? "#{spl[0..1].join('::')}::Authorizer" : "#{spl[0..3].join('::')}::Authorizer"
end

#command_contextString Also known as: context

Returns the top-level context module of the command.

Returns:

  • (String)

    the command context



40
41
42
# File 'lib/yes/core/commands/helper.rb', line 40

def command_context
  splitted_command(cmd).first
end

#command_localeSymbol Also known as: locale

Returns the locale for the command.

Returns:

  • (Symbol)

    the locale



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_nameString

Returns the underscored command name.

Returns:

  • (String)

    the 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_versionString?

Extracts the version from the command class name.

Returns:

  • (String, nil)

    the version string (e.g. “V1”) or nil



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_payloadHash

Returns the event payload with stringified keys.

Returns:

  • (Hash)

    the deep stringified event payload



63
64
65
# File 'lib/yes/core/commands/helper.rb', line 63

def event_payload
  cmd.payload.deep_stringify_keys
end

#validator_classnameString

Returns the fully qualified validator class name.

Returns:

  • (String)

    the 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