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.3"
Class Attribute Summary collapse
-
.configuration ⇒ Configuration
Returns the application configuration object.
Class Method Summary collapse
-
.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.
- .configure {|configuration| ... } ⇒ Object
-
.is_simple_command?(klass_constant) ⇒ Boolean
Returns true or false depending on whether or not the class constant prepends module SimpleCommand::ClassMethods.
-
.is_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.
-
.run_command(klass_constant, parameters) ⇒ Object
Runs the command given the parameters and returns the result.
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
.configuration ⇒ Configuration
Returns the application configuration object.
12 13 14 |
# File 'lib/simple_command_dispatcher/configure.rb', line 12 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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/simple_command_dispatcher.rb', line 62 def call(command = "", command_modules = {}, = {}, *command_parameters) # Create a constantized class from our command and command_modules... command_class_constant = to_constantized_class(command, command_modules, ) # 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 # Calling is_simple_command? returns true if the class pointed to by # command_class_constant is a valid SimpleCommand class; that is, # if it prepends module SimpleCommand::ClassMethods. if !is_simple_command?(command_class_constant) raise ArgumentError.new("Class \"#{command_class_constant}\" must prepend module SimpleCommand if Configuration#allow_custom_commands is true.") end end if is_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.new("Class \"#{command_class_constant}\" does not respond_to? method ::call.") end end |
.configure {|configuration| ... } ⇒ Object
16 17 18 |
# File 'lib/simple_command_dispatcher/configure.rb', line 16 def self.configure yield(configuration) end |
.is_simple_command?(klass_constant) ⇒ Boolean
Returns true or false depending on whether or not the class constant prepends module SimpleCommand::ClassMethods.
109 110 111 |
# File 'lib/simple_command_dispatcher.rb', line 109 def is_simple_command?(klass_constant) klass_constant.eigenclass.included_modules.include? SimpleCommand::ClassMethods end |
.is_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.
98 99 100 |
# File 'lib/simple_command_dispatcher.rb', line 98 def is_valid_command(klass_constant) klass_constant.eigenclass.public_method_defined?(:call) end |
.run_command(klass_constant, parameters) ⇒ Object
Runs the command given the parameters and returns the result.
121 122 123 124 125 |
# File 'lib/simple_command_dispatcher.rb', line 121 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 |