Class: SimpleCommandDispatcher::Services::CommandService

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

Overview

Handles class and module transformations and instantiation.

Instance Method Summary collapse

Methods included from Helpers::Camelize

#camelize

Methods included from Helpers::TrimAll

#trim_all

Constructor Details

#initialize(command:, command_namespace: {}) ⇒ CommandService



13
14
15
16
# File 'lib/simple_command_dispatcher/services/command_service.rb', line 13

def initialize(command:, command_namespace: {})
  @command = validate_command(command:)
  @command_namespace = validate_command_namespace(command_namespace:)
end

Instance Method Details

#to_classClass

Returns a constantized class (as a Class constant), given the command and command_namespace that were provided during initialization.

Examples:


to_class("Authenticate", "Api") # => Api::Authenticate
to_class(:Authenticate, [:Api, :AppName, :V1]) # => Api::AppName::V1::Authenticate
to_class(:Authenticate, { :api :Api, app_name: :AppName, api_version: :V2 })
  # => Api::AppName::V2::Authenticate
to_class("authenticate", { :api :api, app_name: :app_name, api_version: :v1 },
  { titleize_class: true, titleize_module: true }) # => Api::AppName::V1::Authenticate

Raises:



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

def to_class
  qualified_class_string = to_qualified_class_string(command, command_namespace)

  begin
    qualified_class_string.constantize
  rescue StandardError => e
    raise Errors::InvalidClassConstantError.new(qualified_class_string, e.message)
  end
end

#validate_command(command:) ⇒ String

Validates command and returns command as a string after all blanks have been removed using command.gsub(/s+/, “”).

Examples:


validate_command(" My Class ") # => "MyClass"
validate_command(:MyClass) # => "MyClass"

Raises:

  • (ArgumentError)

    if the command is empty? or not of type String or Symbol.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/simple_command_dispatcher/services/command_service.rb', line 104

def validate_command(command:)
  unless command.is_a?(Symbol) || command.is_a?(String)
    raise ArgumentError,
      'command is not a String or Symbol. command must equal the class name of the ' \
      'command to call in the form of a String or Symbol.'
  end

  command = command.to_s.strip

  raise ArgumentError, 'command is empty?' if command.empty?

  command
end

#validate_command_namespace(command_namespace:) ⇒ Hash, Array or String

Validates and returns command_namespace.

Examples:


validate_command_namespace(" Module ") # => " Module "
validate_command_namespace(:Module) # => :Module
validate_command_namespace("ModuleA::ModuleB") # => "ModuleA::ModuleB"

Raises:

  • (ArgumentError)

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



134
135
136
137
138
139
140
141
142
143
# File 'lib/simple_command_dispatcher/services/command_service.rb', line 134

def validate_command_namespace(command_namespace:)
  return {} if command_namespace.blank?

  unless valid_command_namespace_type?(command_namespace:)
    raise ArgumentError,
      'Argument command_namespace is not a String, Hash or Array.'
  end

  command_namespace
end