Class: SimpleCommandDispatcher::Services::CommandService
- Inherits:
-
Object
- Object
- SimpleCommandDispatcher::Services::CommandService
- 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
-
#initialize(command:, command_namespace: {}) ⇒ 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 all blanks have been removed using command.gsub(/s+/, “”).
-
#validate_command_namespace(command_namespace:) ⇒ Hash, Array or String
Validates and returns command_namespace.
Methods included from Helpers::Camelize
Methods included from Helpers::TrimAll
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_class ⇒ Class
Returns a constantized class (as a Class constant), given the command and command_namespace that were provided during initialization.
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.) end end |
#validate_command(command:) ⇒ String
Validates command and returns command as a string after all blanks have been removed using command.gsub(/s+/, “”).
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.
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 |