Module: SimpleCommandDispatcher

Defined in:
lib/simple_command_dispatcher/configuration.rb,
lib/simple_command_dispatcher.rb,
lib/simple_command_dispatcher/version.rb,
lib/simple_command_dispatcher/commands/utils.rb,
lib/simple_command_dispatcher/commands/errors.rb,
lib/simple_command_dispatcher/helpers/camelize.rb,
lib/simple_command_dispatcher/helpers/trim_all.rb,
lib/simple_command_dispatcher/services/command_service.rb,
lib/simple_command_dispatcher/commands/command_callable.rb,
lib/simple_command_dispatcher/services/command_namespace_service.rb,
lib/simple_command_dispatcher/errors/invalid_class_constant_error.rb,
lib/simple_command_dispatcher/errors/required_class_method_missing_error.rb

Overview

This is the configuration for SimpleCommandDispatcher.

Defined Under Namespace

Modules: Commands, Errors, Helpers, Services Classes: Configuration

Constant Summary collapse

VERSION =
'4.1.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject

Returns the value of attribute configuration.



6
7
8
# File 'lib/simple_command_dispatcher/configuration.rb', line 6

def configuration
  @configuration
end

Class Method Details

.call(command:, command_namespace: {}, request_params: nil) ⇒ Object

Calls a Command given the command name, the namespace (modules) the command belongs to and the (request) parameters to pass to the command.

Examples:


# Below call equates to the following:
# Api::Carz4Rent::V1::Authenticate.call({ email: '[email protected]', password: 'AskM3!' })
SimpleCommandDispatcher.call(command: :Authenticate,
   command_namespace: { api: :Api, app_name: :Carz4Rent, api_version: :V1 },
   request_params: { email: '[email protected]', password: 'AskM3!' } ) # => Command result

# Below equates to the following: Api::Carz4Rent::V2::Authenticate.call('[email protected]', 'AskM3!')
SimpleCommandDispatcher.call(command: :Authenticate,
   command_namespace: ['Api', 'Carz4Rent', 'V2'],
   request_params: ['[email protected]', 'AskM3!']) # => Command result

# Below equates to the following:
# Api::Auth::JazzMeUp::V1::Authenticate.call('[email protected]', 'JazzM3!')
SimpleCommandDispatcher.call(command: :Authenticate,
   command_namespace: ['Api::Auth::JazzMeUp', :V1],
   request_params: ['[email protected]', 'JazzM3!']) # => Command result

Parameters:

  • command (Symbol, String)

    the name of the Command to call.

  • command_namespace (Hash, Array) (defaults to: {})

    the ruby modules that qualify the Command to call. When passing a Hash, the Hash keys serve as documentation only. For example, [‘Api’, ‘AppName’, ‘V1’] and { :api :Api, app_name: :AppName, api_version: :V1 } will both produce ‘Api::AppName::V1’, this string will be prepended to the command to form the Command to call (e.g. ‘Api::AppName::V1::MySimpleCommand’ = Api::AppName::V1::MySimpleCommand.call(*request_params)).

  • request_params (Hash, Array, Object) (defaults to: nil)

    the parameters to pass to the call method of the Command. This parameter is simply passed through to the call method of the Command. Hash parameters are passed as keyword arguments, Array parameters are passed as positional arguments, and other objects are passed as a single argument.

Returns:

  • (Object)

    the Object returned as a result of calling the Command#call method.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/simple_command_dispatcher.rb', line 53

def call(command:, command_namespace: {}, request_params: nil)
  # Create a constantized class from our command and command_namespace...
  constantized_class_object = Services::CommandService.new(command:, command_namespace:).to_class
  validate_command!(constantized_class_object)

  # We know we have a valid command class object if we get here. All we need to do is call the .call
  # class method, pass the request_params arguments depending on the request_params data type, and
  # return the results.

  call_command(constantized_class_object:, request_params:)
end

.configure {|Configuration| ... } ⇒ Configuration

Configures SimpleCommandDispatcher by yielding the configuration object to the block.

SimpleCommandDispatcher.configure do |config|

config.some_option = 'some value'

end

Yields:

  • (Configuration)

    yields the configuration object to the block

Returns:



18
19
20
21
22
23
24
# File 'lib/simple_command_dispatcher/configuration.rb', line 18

def configure
  self.configuration ||= Configuration.new

  yield(configuration) if block_given?

  configuration
end