Module: ActionCommand
- Defined in:
- lib/action_command.rb,
lib/action_command/utils.rb,
lib/action_command/result.rb,
lib/action_command/version.rb,
lib/action_command/executable.rb,
lib/action_command/log_parser.rb,
lib/action_command/input_output.rb,
lib/action_command/executable_transaction.rb,
lib/action_command/pretty_print_log_action.rb
Overview
Unnecessary comment for rubocop
Defined Under Namespace
Classes: Executable, ExecutableTransaction, InputOutput, LogMessage, LogParser, PrettyPrintLogAction, Result, Utils
Constant Summary collapse
- CONTEXT_TEST =
Used as root parent of command if we are in a testing context.
:test- CONTEXT_RAKE =
Used as root parent of command if we are in a rake task
:rake- CONTEXT_RAILS =
Used as root parent of command if we are executing it from rails (a controller, etc)
:rails- CONTEXT_API =
Used as a root element when the command is executed from an API context
:api- RESULT_CODE_OK =
Used if a result has had no failures
0- RESULT_CODE_FAILED =
Used as a generic result code for failure, if you do not provide a more specific one through ActionCommand::Result#failed_with_code
1- LOG_KIND_COMMAND_INPUT =
log entry for the input to a commmand
:command_input- LOG_KIND_COMMAND_OUTPUT =
log entry for the output from a command
:command_output- LOG_KIND_INFO =
info message from within a command
:info- LOG_KIND_DEBUG =
debug message from within a command
:debug- LOG_KIND_ERROR =
error message from within a command
:error- OPTIONAL =
Used to create an optional parameter in describe_io
{ optional: true }.freeze
- VERSION =
Version of this Gem
'0.1.5'.freeze
- @@logger =
rubocop:disable Style/ClassVars
nil- @@params =
rubocop:disable Style/ClassVars
{}
Class Method Summary collapse
- .check_params(cls, params) ⇒ Object
-
.create_and_execute(cls, params, parent, result) ⇒ Object
Used internally, not for general purpose use.
-
.create_result ⇒ Object
A new, valid, empty result.
-
.describe_io(cmd_cls, desc) ⇒ Object
Create a global description of the inputs and outputs of a command.
-
.execute_api(cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of an api context.
-
.execute_child(parent, cls, result, result_key, params = {}) ⇒ ActionCommand::Result
Execute a child command, placing its results under the specified subkey.
-
.execute_rails(cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of a rails context.
-
.execute_rake(cls, args) ⇒ ActionCommand::Result
Execute a command at the root level of a rake task context.
-
.execute_test(rspec, cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of a testing context.
-
.install_rake(rake, sym, cls, deps) ⇒ Object
Install a command as a rake task in a.
-
.logger=(logger) ⇒ Object
Set a logger to be used when creating commands.
Class Method Details
.check_params(cls, params) ⇒ Object
168 169 170 171 172 173 174 |
# File 'lib/action_command.rb', line 168 def self.check_params(cls, params) raise ArgumentError, 'Expected params to be a Hash' unless params.is_a? Hash unless cls.is_a?(Class) && cls.ancestors.include?(ActionCommand::Executable) raise ArgumentError, 'Expected an ActionCommand::Executable as class' end end |
.create_and_execute(cls, params, parent, result) ⇒ Object
Used internally, not for general purpose use.
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/action_command.rb', line 154 def self.create_and_execute(cls, params, parent, result) check_params(cls, params) params[:parent] = parent result.logger = params[:logger] result.logger = @@logger unless params[:logger] result.root_command(cls) if parent.is_a? Symbol action = cls.new(params) result.log_input(params) action.execute(result) result.log_output return result end |
.create_result ⇒ Object
62 63 64 |
# File 'lib/action_command.rb', line 62 def self.create_result return ActionCommand::Result.new end |
.describe_io(cmd_cls, desc) ⇒ Object
Create a global description of the inputs and outputs of a command. Should usually be called within an ActionCommand::Executable subclass in its self.describe_io method
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/action_command.rb', line 141 def self.describe_io(cmd_cls, desc) name = cmd_cls.name params = @@params[name] unless params params = InputOutput.new(cmd_cls, desc) @@params[name] = params yield params params.input(:help, 'Help for this command', OPTIONAL) if params.input_count == 0 end return params end |
.execute_api(cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of an api context
118 119 120 121 |
# File 'lib/action_command.rb', line 118 def self.execute_api(cls, params = {}) result = create_result return ActionCommand.create_and_execute(cls, params, CONTEXT_API, result) end |
.execute_child(parent, cls, result, result_key, params = {}) ⇒ ActionCommand::Result
Execute a child command, placing its results under the specified subkey
131 132 133 134 135 136 |
# File 'lib/action_command.rb', line 131 def self.execute_child(parent, cls, result, result_key, params = {}) result.push(result_key, cls) ActionCommand.create_and_execute(cls, params, parent, result) result.pop(result_key) return result end |
.execute_rails(cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of a rails context
109 110 111 112 |
# File 'lib/action_command.rb', line 109 def self.execute_rails(cls, params = {}) result = create_result return ActionCommand.create_and_execute(cls, params, CONTEXT_RAILS, result) end |
.execute_rake(cls, args) ⇒ ActionCommand::Result
Execute a command at the root level of a rake task context.
84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/action_command.rb', line 84 def self.execute_rake(cls, args) io = cls.describe_io if io.help? args io.show_help return end result = create_result ActionCommand.create_and_execute(cls, io.rake_input(args), CONTEXT_RAKE, result) io.print_output(result) return result end |
.execute_test(rspec, cls, params = {}) ⇒ ActionCommand::Result
Execute a command at the root level of a testing context
74 75 76 77 78 |
# File 'lib/action_command.rb', line 74 def self.execute_test(rspec, cls, params = {}) params[:test] = rspec result = create_result return ActionCommand.create_and_execute(cls, params, CONTEXT_TEST, result) end |
.install_rake(rake, sym, cls, deps) ⇒ Object
Install a command as a rake task in a
98 99 100 101 102 103 |
# File 'lib/action_command.rb', line 98 def self.install_rake(rake, sym, cls, deps) rake.send(:desc, cls.describe_io.desc) rake.send(:task, sym, cls.describe_io.keys => deps) do |_t, args| ActionCommand.execute_rake(cls, args) end end |
.logger=(logger) ⇒ Object
Set a logger to be used when creating commands.
57 58 59 |
# File 'lib/action_command.rb', line 57 def self.logger=(logger) @@logger = logger # rubocop:disable Style/ClassVars end |