Module: SimpleCommand::Dispatcher

Extended by:
KlassTransform
Defined in:
lib/simple_command_dispatcher.rb,
lib/simple_command_dispatcher/version.rb,
lib/simple_command_dispatcher/configure.rb,
lib/simple_command_dispatcher/configuration.rb

Overview

Provides a way to call SimpleCommands or your own custom commands in a more dymanic manner.

For information about the simple_command gem, visit https://rubygems.org/gems/simple_command

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =
'1.2.7'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from KlassTransform

camelize, ensure_options, to_class_string, to_constantized_class, to_constantized_class_string, to_modules_string, validate_klass, validate_klass_modules

Class Attribute Details

.configurationConfiguration

Returns the application configuration object.



14
15
16
# File 'lib/simple_command_dispatcher/configure.rb', line 14

def self.configuration
  @configuration ||= Configuration.new
end

Class Method Details

.call(command = '', command_modules = {}, options = {}, *command_parameters) ⇒ SimpleCommand, Object

Calls a SimpleCommand or Command given the command name, the modules the command belongs to and the parameters to pass to the command.

Examples:


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

# Below equates to the following: Api::Carz4Rent::V2::Authenticate.call('[email protected]', 'AskM3!')
SimpleCommand::Dispatcher.call(:Authenticate,
   ['Api', 'Carz4Rent', 'V2'], '[email protected]', 'AskM3!') # => SimpleCommand result

# Below equates to the following:
# Api::Auth::JazzMeUp::V1::Authenticate.call('[email protected]', 'JazzM3!')
SimpleCommand::Dispatcher.call(:Authenticate, ['Api::Auth::JazzMeUp', :V1],
   '[email protected]', 'JazzM3!') # => SimpleCommand result

Options Hash (options):

  • :camelize (Boolean) — default: false

    determines whether or not both class and module names should be camelized.

  • :titleize (Boolean) — default: false

    determines whether or not both class and module names should be titleized.

  • :class_titleize (Boolean) — default: false

    determines whether or not class names should be titleized.

  • :class_camelized (Boolean) — default: false

    determines whether or not class names should be camelized.

  • :module_titleize (Boolean) — default: false

    determines whether or not module names should be titleized.

  • :module_camelized (Boolean) — default: false

    determines whether or not module names should be camelized.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/simple_command_dispatcher.rb', line 70

def call(command = '', command_modules = {}, options = {}, *command_parameters)
  # Create a constantized class from our command and command_modules...
  command_class_constant = to_constantized_class(command, command_modules, options)

  # If we're NOT allowing custom commands, make sure we're dealing with a a command class
  # that prepends the SimpleCommand module.
  if !SimpleCommand::Dispatcher.configuration.allow_custom_commands && !simple_command?(command_class_constant)
    raise ArgumentError,
      "Class \"#{command_class_constant}\" " \
      'must prepend module SimpleCommand if Configuration#allow_custom_commands is true.'
  end

  if valid_command?(command_class_constant)
    # We know we have a valid SimpleCommand; all we need to do is call #call,
    # pass the command_parameter variable arguments to the call, and return the results.
    run_command(command_class_constant, command_parameters)
  else
    raise NameError, "Class \"#{command_class_constant}\" does not respond_to? method ::call."
  end
end

.configure {|configuration| ... } ⇒ Object

Yields:



18
19
20
# File 'lib/simple_command_dispatcher/configure.rb', line 18

def self.configure
  yield(configuration)
end

.run_command(klass_constant, parameters) ⇒ Object

Runs the command given the parameters and returns the result.



129
130
131
132
133
# File 'lib/simple_command_dispatcher.rb', line 129

def run_command(klass_constant, parameters)
  klass_constant.call(*parameters)
  # rescue NameError
  #   raise NameError.new("Class \"#{klass_constant}\" does not respond_to? method ::call.")
end

.simple_command?(klass_constant) ⇒ Boolean

Returns true or false depending on whether or not the class constant prepends module

SimpleCommand::ClassMethods.


117
118
119
# File 'lib/simple_command_dispatcher.rb', line 117

def simple_command?(klass_constant)
  klass_constant.eigenclass.included_modules.include? SimpleCommand::ClassMethods
end

.valid_command?(klass_constant) ⇒ Boolean

Returns true or false depending on whether or not the class constant has a public class method named ::call defined. Commands that do not have a public class method named ::call, are considered invalid.



104
105
106
# File 'lib/simple_command_dispatcher.rb', line 104

def valid_command?(klass_constant)
  klass_constant.eigenclass.public_method_defined?(:call)
end