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 =
'5.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configuration ⇒ Configuration

Returns the configuration object, initializing it if necessary



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


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



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