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)
-
- (String) last_error
readonly
The last string error message.
-
- (Integer) result_code
readonly
The current result code.
Instance Method Summary (collapse)
-
- (Object) [](key)
Return a value return by the command.
-
- (Object) []=(key, val)
Assign some kind of a return value for use by the caller.
-
- (Object) configure_logger(logger, format)
set the logger for this result.
-
- (Object) current
returns the current hash of values we are operating on.
-
- (Object) debug(msg = nil) { ... }
display an debugging message to the logger, if there is one.
-
- (Object) error(msg)
display an error message to the logger, if there is one.
-
- (Object) failed(msg)
Call this if your command implementation fails.
-
- (Object) failed_with_code(msg, result_code)
Call this if your command implementation fails.
-
- (Object) info(msg = nil) { ... }
display an informational message to the logger, if there is one.
-
- (Result) initialize
constructor
By default, a command is ok?.
-
- (Boolean) key?(key)
determine if a key exists in the result.
-
- (Object) log_input(params)
Used internally to log the input parameters to a command.
-
- (Object) log_output
Used internally to log the output parameters for a command.
-
- (Boolean) logging?
True if logging is enabled.
-
- (Boolean) ok?
True, up until failed has been called at least once.
-
- (Object) pop(key)
removes the current set of results from the stack.
-
- (Object) push(key, cmd)
adds results under the subkey until pop is called.
-
- (Object) root_command(cls)
Used internally to establish the class of the root command.
-
- (Object) sequence
return the unique sequence id for the commands under this result.
Constructor Details
- (Result) initialize
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
- (String) last_error (readonly)
Returns the last string error message
83 84 85 |
# File 'lib/action_command/result.rb', line 83 def last_error @last_error end |
- (Integer) result_code (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
- (Object) [](key)
Return a value return by the command
126 127 128 |
# File 'lib/action_command/result.rb', line 126 def [](key) return current[key] end |
- (Object) []=(key, val)
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 |
- (Object) configure_logger(logger, format)
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 |
- (Object) current
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 |
- (Object) debug(msg = nil) { ... }
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 |
- (Object) error(msg)
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 |
- (Object) failed(msg)
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 |
- (Object) failed_with_code(msg, result_code)
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 |
- (Object) info(msg = nil) { ... }
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 |
- (Boolean) key?(key)
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 |
- (Object) log_input(params)
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 |
- (Object) log_output
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 |
- (Boolean) logging?
Returns true if logging is enabled.
25 26 27 |
# File 'lib/action_command/result.rb', line 25 def logging? return !@logger.nil? end |
- (Boolean) ok?
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 |
- (Object) pop(key)
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 |
- (Object) push(key, cmd)
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 |
- (Object) root_command(cls)
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 |
- (Object) sequence
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 |