Module: SimpleCommand::Dispatcher

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

Overview

Provides a way to call SimpleCommand commands in a more dymanic manner.

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

Constant Summary collapse

VERSION =
"1.1.1"

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 Method Details

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

Calls a SimpleCommand 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

Parameters:

  • command (Symbol, String) (defaults to: "")

    the name of the SimpleCommand to call.

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

    the ruby modules that qualify the SimpleCommand 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 SimpleCommand to call (e.g. ‘Api::AppName::V1::MySimpleCommand’ = Api::AppName::V1::MySimpleCommand.call(*command_parameters)).

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

    the options that determine how command and command_module are transformed.

  • command_parameters (*)

    the parameters to pass to the call method of the SimpleCommand. This parameter is simply passed through to the call method of the SimpleCommand.

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.

Returns:

  • (SimpleCommand)

    the SimpleCommand returned as a result from calling the SimpleCommand#call method.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simple_command_dispatcher.rb', line 61

def call(command = "", command_modules = {}, options = {}, *command_parameters)

   # Create a constantized class from our command and command_modules...
   simple_command_class_constant = to_constantized_class(command, command_modules, options)

   # Calling is_simple_command? returns true if the class pointed to by
   # simple_command_class_constant is a valid SimpleCommand class; that is, 
   # if it prepends module SimpleCommand::ClassMethods.
   if !is_simple_command?(simple_command_class_constant) 
      raise ArgumentError.new('Class does not prepend module SimpleCommand.')
   end

   # 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.
   simple_command_class_constant.call(*command_parameters)
end

.is_simple_command?(klass_constant) ⇒ Boolean

Returns true or false depending on whether or not the class constant prepends module SimpleCommand::ClassMethods.

Parameters:

  • klass_constant (String)

    a class constant that will be validated to see whether or not the class prepends module SimpleCommand::ClassMethods.

Returns:

  • (Boolean)

    true if klass_constant prepends Module SimpleCommand::ClassMethods, false otherwise.



88
89
90
# File 'lib/simple_command_dispatcher.rb', line 88

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