Class: SimpleCommandDispatcher::Services::CommandService

Inherits:
Object
  • Object
show all
Includes:
Helpers::Camelize, Logger
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: {}, options: {}) ⇒ CommandService

Returns a new instance of CommandService.



15
16
17
18
19
# File 'lib/simple_command_dispatcher/services/command_service.rb', line 15

def initialize(command:, command_namespace: {}, options: {})
  @options = options
  @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:


CommandService.new(command: "Authenticate", command_namespace: "Api").to_class
  # => Api::Authenticate
CommandService.new(command: :Authenticate, command_namespace: [:Api, :AppName, :V1]).to_class
  # => Api::AppName::V1::Authenticate
CommandService.new(command: :Authenticate,
                   command_namespace: { api: :Api, app_name: :AppName, api_version: :V2 }).to_class
  # => Api::AppName::V2::Authenticate
CommandService.new(command: "authenticate", command_namespace: "api::app_name::v1").to_class
  # => Api::AppName::V1::Authenticate

Returns:

  • (Class)

    the class constant.

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/simple_command_dispatcher/services/command_service.rb', line 41

def to_class
  qualified_class_string = to_qualified_class_string

  if options.debug?
    log_debug <<~DEBUG
      Command to execute: #{qualified_class_string.inspect}
    DEBUG
  end

  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 leading and trailing whitespace is stripped.

Examples:


validate_command(command: " MyClass ") # => "MyClass"
validate_command(command: :MyClass) # => "MyClass"

Parameters:

  • command (Symbol, String)

    the class name to be validated. command cannot be empty after stripping.

Returns:

  • (String)

    the validated class as a string with leading/trailing whitespace removed.

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, ...

Validates and returns command_namespace.

Examples:


validate_command_namespace(command_namespace: "Api::V1") # => "Api::V1"
validate_command_namespace(command_namespace: [:Api, :V1]) # => [:Api, :V1]
validate_command_namespace(command_namespace: { api: :Api, version: :V1 }) # => { api: :Api, version: :V1 }

Parameters:

  • command_namespace (Hash, Array, String)

    the module(s) to be validated.

Returns:

  • (Hash, Array, String)

    the validated module(s), or {} if blank.

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