Class: SimpleCommandDispatcher::Services::CommandService
- Inherits:
-
Object
- Object
- SimpleCommandDispatcher::Services::CommandService
- 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
-
#initialize(command:, command_namespace: {}, options: {}) ⇒ CommandService
constructor
A new instance of CommandService.
-
#to_class ⇒ Class
Returns a constantized class (as a Class constant), given the command and command_namespace that were provided during initialization.
-
#validate_command(command:) ⇒ String
Validates command and returns command as a string after leading and trailing whitespace is stripped.
-
#validate_command_namespace(command_namespace:) ⇒ Hash, ...
Validates and returns command_namespace.
Methods included from Helpers::Camelize
Methods included from Helpers::TrimAll
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 = @command = validate_command(command:) @command_namespace = validate_command_namespace(command_namespace:) end |
Instance Method Details
#to_class ⇒ Class
Returns a constantized class (as a Class constant), given the command and command_namespace that were provided during initialization.
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 .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.) end end |
#validate_command(command:) ⇒ String
Validates command and returns command as a string after leading and trailing whitespace is stripped.
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.
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 |