Class: ActionCommand::Result
- Inherits:
-
Object
- Object
- ActionCommand::Result
- Defined in:
- lib/action_command/result.rb
Overview
The result of one or more commands being executed.
Instance Attribute Summary collapse
-
#last_error ⇒ String
readonly
The last string error message.
-
#result_code ⇒ Integer
readonly
The current result code.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Return a value return by the command.
-
#[]=(key, val) ⇒ Object
Assign some kind of a return value for use by the caller.
-
#configure_logger(logger, format) ⇒ Object
set the logger for this result.
-
#current ⇒ Object
returns the current hash of values we are operating on.
-
#debug(msg = nil) { ... } ⇒ Object
display an debugging message to the logger, if there is one.
-
#error(msg) ⇒ Object
display an error message to the logger, if there is one.
-
#failed(msg) ⇒ Object
Call this if your command implementation fails.
-
#failed_with_code(msg, result_code) ⇒ Object
Call this if your command implementation fails.
-
#info(msg = nil) { ... } ⇒ Object
display an informational message to the logger, if there is one.
-
#initialize ⇒ Result
constructor
By default, a command is ok?.
-
#key?(key) ⇒ Boolean
determine if a key exists in the result.
-
#log_input(params) ⇒ Object
Used internally to log the input parameters to a command.
-
#log_output ⇒ Object
Used internally to log the output parameters for a command.
-
#logging? ⇒ Boolean
True if logging is enabled.
-
#ok? ⇒ Boolean
True, up until failed has been called at least once.
-
#pop(key) ⇒ Object
removes the current set of results from the stack.
-
#push(key, cmd) ⇒ Object
adds results under the subkey until pop is called.
-
#root_command(cls) ⇒ Object
Used internally to establish the class of the root command.
-
#sequence ⇒ Object
return the unique sequence id for the commands under this result.
Constructor Details
#initialize ⇒ Result
By default, a command is ok?
8 9 10 11 12 13 |
# File 'lib/action_command/result.rb', line 8 def initialize @result_code = RESULT_CODE_OK @last_error = nil @values = [{}] @logger = nil end |
Instance Attribute Details
#last_error ⇒ String (readonly)
Returns the last string error message.
83 84 85 |
# File 'lib/action_command/result.rb', line 83 def last_error @last_error end |
#result_code ⇒ Integer (readonly)
Returns the current result code.
80 81 82 |
# File 'lib/action_command/result.rb', line 80 def result_code @result_code end |
Instance Method Details
#[](key) ⇒ Object
Return a value return by the command
126 127 128 |
# File 'lib/action_command/result.rb', line 126 def [](key) return current[key] end |
#[]=(key, val) ⇒ Object
Assign some kind of a return value for use by the caller.
111 112 113 |
# File 'lib/action_command/result.rb', line 111 def []=(key, val) current[key] = val end |
#configure_logger(logger, format) ⇒ Object
set the logger for this result
16 17 18 19 20 21 22 |
# File 'lib/action_command/result.rb', line 16 def configure_logger(logger, format) return unless logger @sequence = SecureRandom.hex @stack = [] @logger = logger @log_format = format end |
#current ⇒ Object
returns the current hash of values we are operating on.
106 107 108 |
# File 'lib/action_command/result.rb', line 106 def current return @values.last end |
#debug(msg = nil) { ... } ⇒ Object
display an debugging message to the logger, if there is one.
31 32 33 34 35 36 |
# File 'lib/action_command/result.rb', line 31 def debug(msg = nil) if @logger msg = build_log(msg || yield, ActionCommand::LOG_KIND_DEBUG) @logger.info(format_log(msg)) end end |
#error(msg) ⇒ Object
display an error message to the logger, if there is one.
48 49 50 51 52 53 |
# File 'lib/action_command/result.rb', line 48 def error(msg) if @logger msg = build_log(msg, ActionCommand::LOG_KIND_ERROR) @logger.error(format_log(msg)) end end |
#failed(msg) ⇒ Object
Call this if your command implementation fails. Sets ok? to false on the result.
58 59 60 61 62 |
# File 'lib/action_command/result.rb', line 58 def failed(msg) @result_code = RESULT_CODE_FAILED @last_error = msg error(msg) end |
#failed_with_code(msg, result_code) ⇒ Object
Call this if your command implementation fails. Sets ok? to false on the result.
68 69 70 71 72 |
# File 'lib/action_command/result.rb', line 68 def failed_with_code(msg, result_code) @result_code = result_code @last_error = msg error(msg) end |
#info(msg = nil) { ... } ⇒ Object
display an informational message to the logger, if there is one.
40 41 42 43 44 45 |
# File 'lib/action_command/result.rb', line 40 def info(msg = nil) if @logger msg = build_log(msg || yield, ActionCommand::LOG_KIND_INFO) @logger.info(format_log(msg)) end end |
#key?(key) ⇒ Boolean
determine if a key exists in the result.
121 122 123 |
# File 'lib/action_command/result.rb', line 121 def key?(key) return current.key?(key) end |
#log_input(params) ⇒ Object
Used internally to log the input parameters to a command
131 132 133 134 135 |
# File 'lib/action_command/result.rb', line 131 def log_input(params) return unless @logger output = params.reject { |k, _v| internal_key?(k) } log_info_hash(output, ActionCommand::LOG_KIND_COMMAND_INPUT) end |
#log_output ⇒ Object
Used internally to log the output parameters for a command.
138 139 140 141 142 143 144 |
# File 'lib/action_command/result.rb', line 138 def log_output return unless @logger # only log the first level parameters, subcommands will log # their own output. output = current.reject { |k, v| v.is_a?(Hash) || internal_key?(k) } log_info_hash(output, ActionCommand::LOG_KIND_COMMAND_OUTPUT) end |
#logging? ⇒ Boolean
Returns true if logging is enabled.
25 26 27 |
# File 'lib/action_command/result.rb', line 25 def logging? return !@logger.nil? end |
#ok? ⇒ Boolean
Returns true, up until failed has been called at least once.
75 76 77 |
# File 'lib/action_command/result.rb', line 75 def ok? return @result_code == RESULT_CODE_OK end |
#pop(key) ⇒ Object
removes the current set of results from the stack.
99 100 101 102 103 |
# File 'lib/action_command/result.rb', line 99 def pop(key) return unless key @values.pop @stack.pop if @logger end |
#push(key, cmd) ⇒ Object
adds results under the subkey until pop is called
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/action_command/result.rb', line 86 def push(key, cmd) return unless key old_cur = current if old_cur.key?(key) @values << old_cur[key] else @values << {} old_cur[key] = @values.last end @stack << { key: key, cmd: cmd } if @logger end |
#root_command(cls) ⇒ Object
Used internally to establish the class of the root command
147 148 149 |
# File 'lib/action_command/result.rb', line 147 def root_command(cls) @stack << { key: nil, cmd: cls } if @logger end |
#sequence ⇒ Object
return the unique sequence id for the commands under this result
116 117 118 |
# File 'lib/action_command/result.rb', line 116 def sequence return @sequence end |