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.
-
#exceptions ⇒ 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_exception(method, message, classname: nil) ⇒ Object
- #add_exceptions(exceptions) ⇒ Object
- #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.
15 16 17 18 19 20 21 |
# File 'lib/opera/operation/result.rb', line 15 def initialize(output: nil, errors: {}) @errors = errors @exceptions = {} @information = {} @executions = [] @output = output end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Acumulator of errors in validation + steps
8 9 10 |
# File 'lib/opera/operation/result.rb', line 8 def errors @errors end |
#exceptions ⇒ Object (readonly)
Acumulator of errors in validation + steps
8 9 10 |
# File 'lib/opera/operation/result.rb', line 8 def exceptions @exceptions end |
#executions ⇒ Object (readonly)
Acumulator of errors in validation + steps
8 9 10 |
# File 'lib/opera/operation/result.rb', line 8 def executions @executions end |
#information ⇒ Object (readonly)
Acumulator of errors in validation + steps
8 9 10 |
# File 'lib/opera/operation/result.rb', line 8 def information @information end |
#output ⇒ Object
in case of success, it contains the resulting value
13 14 15 |
# File 'lib/opera/operation/result.rb', line 13 def output @output end |
Instance Method Details
#add_error(field, message) ⇒ Object
rubocop:disable Metrics/MethodLength
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/opera/operation/result.rb', line 42 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
57 58 59 60 61 |
# File 'lib/opera/operation/result.rb', line 57 def add_errors(errors) errors.to_hash.each_pair do |key, value| add_error(key, value) end end |
#add_exception(method, message, classname: nil) ⇒ Object
63 64 65 66 67 |
# File 'lib/opera/operation/result.rb', line 63 def add_exception(method, , classname: nil) key = [classname, Array(method).first].compact.join('#') @exceptions[key] ||= [] @exceptions[key].push() end |
#add_exceptions(exceptions) ⇒ Object
69 70 71 72 73 |
# File 'lib/opera/operation/result.rb', line 69 def add_exceptions(exceptions) exceptions.each_pair do |key, value| add_exception(key, value) end end |
#add_execution(step) ⇒ Object
79 80 81 |
# File 'lib/opera/operation/result.rb', line 79 def add_execution(step) @executions << step end |
#add_information(hash) ⇒ Object
75 76 77 |
# File 'lib/opera/operation/result.rb', line 75 def add_information(hash) @information.merge!(hash) end |
#failure? ⇒ Boolean
23 24 25 |
# File 'lib/opera/operation/result.rb', line 23 def failure? errors.any? || exceptions.any? end |
#failures ⇒ Object
31 32 33 |
# File 'lib/opera/operation/result.rb', line 31 def failures errors.merge(exceptions) end |
#output! ⇒ Object
35 36 37 38 39 |
# File 'lib/opera/operation/result.rb', line 35 def output! raise OutputError, 'Cannot retrieve output from a Failure.' if failure? output end |
#success? ⇒ Boolean
27 28 29 |
# File 'lib/opera/operation/result.rb', line 27 def success? !failure? end |