Class: SimpleCommandDispatcher::Services::CommandNamespaceService

Inherits:
Object
  • Object
show all
Includes:
Helpers::Camelize, Helpers::TrimAll
Defined in:
lib/simple_command_dispatcher/services/command_namespace_service.rb

Overview

Returns a string of modules that can be subsequently prepended to a class, to create a fully qualified, constantized class.

The command_namespace is provided during initialization and can be a Hash, Array, or String.

Examples:


to_class_modules_string("Api") # => "Api::"
to_class_modules_string([:Api, :AppName, :V1]) # => "Api::AppName::V1::"
to_class_modules_string({ api: :Api, app_name: :AppName, api_version: :V1 }) # => "Api::AppName::V1::"
to_class_modules_string({ api: :api, app_name: :app_name, api_version: :v1 }, { titleize_module: true })
   # => "Api::AppName::V1::"

Returns:

  • (String)

    a string of modules that can be subsequently prepended to a class, to create a constantized class.

Raises:

  • (ArgumentError)

    if the command_namespace is not of type String, Hash or Array.

Instance Method Summary collapse

Methods included from Helpers::TrimAll

#trim_all

Methods included from Helpers::Camelize

#camelize

Constructor Details

#initialize(command_namespace:) ⇒ CommandNamespaceService

Returns a new instance of CommandNamespaceService.



30
31
32
# File 'lib/simple_command_dispatcher/services/command_namespace_service.rb', line 30

def initialize(command_namespace:)
  @command_namespace = command_namespace
end

Instance Method Details

#to_class_modules_stringObject

Handles command module transformations from String, Hash or Array into a fully qualified class modules string (e.g. “A::B::C::”).



36
37
38
39
40
41
42
# File 'lib/simple_command_dispatcher/services/command_namespace_service.rb', line 36

def to_class_modules_string
  return '' if command_namespace.blank?

  class_modules_string = join_class_modules_if(command_namespace:)
  class_modules_string = trim_all(camelize(class_modules_string))
  "#{class_modules_string}::"
end