Class: SeaFood::Service::ServiceResult
- Inherits:
-
Object
- Object
- SeaFood::Service::ServiceResult
- Defined in:
- lib/sea_food/service.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#success ⇒ Object
readonly
Returns the value of attribute success.
Instance Method Summary collapse
-
#failed? ⇒ Boolean
(also: #fail?)
Checks if the service call failed.
-
#initialize(success: true, data: nil, errors: nil) ⇒ ServiceResult
constructor
Initializes the ServiceResult.
-
#method_missing(key) ⇒ Object
:rubocop:disable Style/MissingRespondToMissing.
-
#succeeded? ⇒ Boolean
(also: #success?)
Checks if the service call was successful.
Constructor Details
#initialize(success: true, data: nil, errors: nil) ⇒ ServiceResult
Initializes the ServiceResult.
90 91 92 93 94 |
# File 'lib/sea_food/service.rb', line 90 def initialize(success: true, data: nil, errors: nil) @success = success @data = (data || {}).with_indifferent_access @errors = (errors.to_h || {}).with_indifferent_access end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key) ⇒ Object
:rubocop:disable Style/MissingRespondToMissing
113 114 115 116 117 118 119 |
# File 'lib/sea_food/service.rb', line 113 def method_missing(key) if succeeded? @data[key] else @errors[key] end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
85 86 87 |
# File 'lib/sea_food/service.rb', line 85 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
85 86 87 |
# File 'lib/sea_food/service.rb', line 85 def errors @errors end |
#success ⇒ Object (readonly)
Returns the value of attribute success.
85 86 87 |
# File 'lib/sea_food/service.rb', line 85 def success @success end |
Instance Method Details
#failed? ⇒ Boolean Also known as: fail?
Checks if the service call failed.
106 107 108 |
# File 'lib/sea_food/service.rb', line 106 def failed? !success end |
#succeeded? ⇒ Boolean Also known as: success?
Checks if the service call was successful.
98 99 100 |
# File 'lib/sea_food/service.rb', line 98 def succeeded? success end |