Class: Opera::Operation::Result
- Inherits:
-
Object
- Object
- Opera::Operation::Result
- Defined in:
- lib/opera/operation/result.rb
Defined Under Namespace
Classes: OutputError
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Acumulator of errors in validation + steps.
-
#executions ⇒ Object
readonly
Acumulator of errors in validation + steps.
-
#information ⇒ Object
readonly
Acumulator of errors in validation + steps.
-
#output ⇒ Object
in case of success, it contains the resulting value.
Instance Method Summary collapse
-
#add_error(field, message) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#add_errors(errors) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #add_execution(step) ⇒ Object
- #add_information(hash) ⇒ Object
- #failure? ⇒ Boolean
- #failures ⇒ Object
-
#initialize(output: nil, errors: {}) ⇒ Result
constructor
A new instance of Result.
- #output! ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(output: nil, errors: {}) ⇒ Result
Returns a new instance of Result.
21 22 23 24 25 26 |
# File 'lib/opera/operation/result.rb', line 21 def initialize(output: nil, errors: {}) @errors = errors @information = {} @executions = [] @output = output end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Acumulator of errors in validation + steps
15 16 17 |
# File 'lib/opera/operation/result.rb', line 15 def errors @errors end |
#executions ⇒ Object (readonly)
Acumulator of errors in validation + steps
15 16 17 |
# File 'lib/opera/operation/result.rb', line 15 def executions @executions end |
#information ⇒ Object (readonly)
Acumulator of errors in validation + steps
15 16 17 |
# File 'lib/opera/operation/result.rb', line 15 def information @information end |
#output ⇒ Object
in case of success, it contains the resulting value
19 20 21 |
# File 'lib/opera/operation/result.rb', line 19 def output @output end |
Instance Method Details
#add_error(field, message) ⇒ Object
rubocop:disable Metrics/MethodLength
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/opera/operation/result.rb', line 47 def add_error(field, ) @errors[field] ||= [] if .is_a?(Hash) if @errors[field].first&.is_a?(Hash) @errors[field].first.merge!() else @errors[field].push() end else @errors[field].concat(Array()) end @errors[field].uniq! end |
#add_errors(errors) ⇒ Object
rubocop:enable Metrics/MethodLength
62 63 64 65 66 |
# File 'lib/opera/operation/result.rb', line 62 def add_errors(errors) errors.to_hash.each_pair do |key, value| add_error(key, value) end end |
#add_execution(step) ⇒ Object
72 73 74 |
# File 'lib/opera/operation/result.rb', line 72 def add_execution(step) @executions << step end |
#add_information(hash) ⇒ Object
68 69 70 |
# File 'lib/opera/operation/result.rb', line 68 def add_information(hash) @information.merge!(hash) end |
#failure? ⇒ Boolean
28 29 30 |
# File 'lib/opera/operation/result.rb', line 28 def failure? errors.any? end |
#failures ⇒ Object
36 37 38 |
# File 'lib/opera/operation/result.rb', line 36 def failures errors end |
#output! ⇒ Object
40 41 42 43 44 |
# File 'lib/opera/operation/result.rb', line 40 def output! raise OutputError.new('Cannot retrieve output from a Failure.', errors) if failure? output end |
#success? ⇒ Boolean
32 33 34 |
# File 'lib/opera/operation/result.rb', line 32 def success? !failure? end |