Module: SimpleCommandDispatcher

Extended by:
Logger
Defined in:
lib/simple_command_dispatcher/configuration.rb,
lib/simple_command_dispatcher.rb,
lib/simple_command_dispatcher/logger.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/services/options_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, Logger, Services Classes: Configuration

Constant Summary collapse

VERSION =
'4.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationConfiguration

Returns the configuration object, initializing it if necessary

Returns:



27
28
29
# File 'lib/simple_command_dispatcher/configuration.rb', line 27

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.call(command:, command_namespace: {}, request_params: nil, options: {}) ⇒ 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, String) (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’], ‘Api::AppName::V1’, and { :api :Api, app_name: :AppName, api_version: :V1 } will all 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.

  • options (Hash) (defaults to: {})

    optional configuration for command execution. Supported options:

    • :debug [Boolean] when true, enables debug logging of command execution flow

Returns:

  • (Object)

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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/simple_command_dispatcher.rb', line 61

def call(command:, command_namespace: {}, request_params: nil, options: {})
  @options = Services::OptionsService.new(options:)

  if @options.debug?
    log_debug <<~DEBUG
      Begin dispatching command
        command: #{command.inspect}
        command_namespace: #{command_namespace.inspect}
    DEBUG
  end

  # Create a constantized class from our command and command_namespace...
  constantized_class_object = Services::CommandService.new(command:, command_namespace:, options: @options).to_class

  if @options.debug?
    log_debug <<~DEBUG
      Constantized command: #{constantized_class_object.inspect}
    DEBUG
  end

  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.

  command_object = call_command(constantized_class_object:, request_params:)

  log_debug 'End dispatching command' if @options.debug?

  command_object
end

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

Configures SimpleCommandDispatcher by yielding the configuration object to the block.

SimpleCommandDispatcher.configure do |config|

config.logger = Rails.logger

end

Yields:

  • (Configuration)

    yields the configuration object to the block

Returns:



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

def configure
  self.configuration ||= Configuration.new

  yield(configuration) if block_given?

  configuration
end