Class: Hanami::Interactor::Result
- Inherits:
- Utils::BasicObject
- Defined in:
- lib/hanami/interactor.rb
Overview
Result of an operation
Constant Summary collapse
- METHODS =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Concrete methods
::Hash[initialize: true, success?: true, successful?: true, failure?: true, fail!: true, prepare!: true, errors: true, error: true].freeze
Instance Method Summary collapse
- #add_error(*errors) ⇒ Object private
-
#error ⇒ nil, String
Returns the first errors collected during an operation.
-
#errors ⇒ Array
Returns all the errors collected during an operation.
-
#fail! ⇒ Object
Forces the status to be a failure.
-
#failure? ⇒ TrueClass, FalseClass
Checks if the current status is not successful.
-
#initialize(payload = {}) ⇒ Hanami::Interactor::Result
constructor
private
Initialize a new result.
-
#prepare!(payload) ⇒ Object
private
Prepares the result before to be returned.
-
#successful? ⇒ TrueClass, FalseClass
(also: #success?)
Checks if the current status is successful.
Methods inherited from Utils::BasicObject
#class, const_missing, #inspect, #instance_of?, #is_a?, #kind_of?, #object_id, #pretty_print, #respond_to?
Constructor Details
#initialize(payload = {}) ⇒ Hanami::Interactor::Result
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Initialize a new result
40 41 42 43 44 |
# File 'lib/hanami/interactor.rb', line 40 def initialize(payload = {}) @payload = payload @errors = [] @success = true end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name) ⇒ Object (protected)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
125 126 127 |
# File 'lib/hanami/interactor.rb', line 125 def method_missing(method_name, *) @payload.fetch(method_name) { super } end |
Instance Method Details
#add_error(*errors) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
90 91 92 93 94 |
# File 'lib/hanami/interactor.rb', line 90 def add_error(*errors) @errors << errors @errors.flatten! nil end |
#error ⇒ nil, String
Returns the first errors collected during an operation
106 107 108 |
# File 'lib/hanami/interactor.rb', line 106 def error errors.first end |
#errors ⇒ Array
Returns all the errors collected during an operation
84 85 86 |
# File 'lib/hanami/interactor.rb', line 84 def errors @errors.dup end |
#fail! ⇒ Object
Forces the status to be a failure
70 71 72 |
# File 'lib/hanami/interactor.rb', line 70 def fail! @success = false end |
#failure? ⇒ TrueClass, FalseClass
Checks if the current status is not successful
63 64 65 |
# File 'lib/hanami/interactor.rb', line 63 def failure? !successful? end |
#prepare!(payload) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prepares the result before to be returned
116 117 118 119 |
# File 'lib/hanami/interactor.rb', line 116 def prepare!(payload) @payload.merge!(payload) self end |
#successful? ⇒ TrueClass, FalseClass Also known as: success?
Checks if the current status is successful
51 52 53 |
# File 'lib/hanami/interactor.rb', line 51 def successful? @success && errors.empty? end |