Class: ActionCommand::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/action_command/result.rb

Overview

The result of one or more commands being executed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

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_errorString (readonly)



79
80
81
# File 'lib/action_command/result.rb', line 79

def last_error
  @last_error
end

#result_codeInteger (readonly)



76
77
78
# File 'lib/action_command/result.rb', line 76

def result_code
  @result_code
end

Instance Method Details

#[](key) ⇒ Object

Return a value return by the command



122
123
124
# File 'lib/action_command/result.rb', line 122

def [](key)
  return current[key]
end

#[]=(key, val) ⇒ Object

Assign some kind of a return value for use by the caller.



107
108
109
# File 'lib/action_command/result.rb', line 107

def []=(key, val)
  current[key] = val
end

#currentObject

returns the current hash of values we are operating on.



102
103
104
# File 'lib/action_command/result.rb', line 102

def current
  return @values.last
end

#debug(msg = nil) { ... } ⇒ Object

display an debugging message to the logger, if there is one.

Yields:

  • return a message or hash



30
31
32
33
34
35
# File 'lib/action_command/result.rb', line 30

def debug(msg = nil)
  if @logger
    json = build_log(msg || yield, ActionCommand::LOG_KIND_DEBUG)
    @logger.info(json)
  end
end

#error(msg) ⇒ Object

display an error message to the logger, if there is one.



47
48
49
# File 'lib/action_command/result.rb', line 47

def error(msg)
  @logger.error(build_log(msg, ActionCommand::LOG_KIND_ERROR)) if @logger
end

#failed(msg) ⇒ Object

Call this if your command implementation fails. Sets ok? to false on the result.



54
55
56
57
58
# File 'lib/action_command/result.rb', line 54

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.



64
65
66
67
68
# File 'lib/action_command/result.rb', line 64

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.

Yields:

  • return a message or hash



39
40
41
42
43
44
# File 'lib/action_command/result.rb', line 39

def info(msg = nil)
  if @logger
    json = build_log(msg || yield, ActionCommand::LOG_KIND_INFO)
    @logger.info(json)
  end
end

#key?(key) ⇒ Boolean

determine if a key exists in the result.



117
118
119
# File 'lib/action_command/result.rb', line 117

def key?(key)
  return current.key?(key)
end

#log_input(params) ⇒ Object

Used internally to log the input parameters to a command



127
128
129
130
131
# File 'lib/action_command/result.rb', line 127

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_outputObject

Used internally to log the output parameters for a command.



134
135
136
137
138
139
140
# File 'lib/action_command/result.rb', line 134

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

#logger=(logger) ⇒ Object

set the logger for this result



16
17
18
19
20
21
# File 'lib/action_command/result.rb', line 16

def logger=(logger)
  return unless logger
  @sequence = SecureRandom.hex 
  @stack = [] 
  @logger = logger
end

#logging?Boolean



24
25
26
# File 'lib/action_command/result.rb', line 24

def logging?
  return !@logger.nil?
end

#ok?Boolean



71
72
73
# File 'lib/action_command/result.rb', line 71

def ok?
  return @result_code == RESULT_CODE_OK
end

#pop(key) ⇒ Object

removes the current set of results from the stack.



95
96
97
98
99
# File 'lib/action_command/result.rb', line 95

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



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/action_command/result.rb', line 82

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



143
144
145
# File 'lib/action_command/result.rb', line 143

def root_command(cls)
  @stack << { key: nil, cmd: cls }  if @logger
end

#sequenceObject

return the unique sequence id for the commands under this result



112
113
114
# File 'lib/action_command/result.rb', line 112

def sequence
  return @sequence
end