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.8'.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
173 174 175 176 177 178 179 |
# File 'lib/action_command.rb', line 173 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/action_command.rb', line 156 def self.create_and_execute(cls, params, parent, result) check_params(cls, params) params[:parent] = parent logger = params[:logger] logger = @@logger unless logger log_format = :json log_format = params[:log_format] if params.key?(:log_format) result.configure_logger(logger, log_format) 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
Returns a new, valid, empty result.
63 64 65 |
# File 'lib/action_command.rb', line 63 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
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/action_command.rb', line 143 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
120 121 122 123 |
# File 'lib/action_command.rb', line 120 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
133 134 135 136 137 138 |
# File 'lib/action_command.rb', line 133 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
111 112 113 114 |
# File 'lib/action_command.rb', line 111 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.
85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/action_command.rb', line 85 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
75 76 77 78 79 |
# File 'lib/action_command.rb', line 75 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
100 101 102 103 104 105 |
# File 'lib/action_command.rb', line 100 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.
58 59 60 |
# File 'lib/action_command.rb', line 58 def self.logger=(logger) @@logger = logger # rubocop:disable Style/ClassVars end |