Class: TheHelp::Service::Result
- Inherits:
-
Object
- Object
- TheHelp::Service::Result
- Defined in:
- lib/the_help/service.rb
Overview
Holds the result of running a service as well as the execution status
An instance of this class will be returned from any service call and will have a status of either :success or :error along with a value that is set by the service.
Instance Attribute Summary collapse
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #error(value = nil, &block) ⇒ Object
- #error? ⇒ Boolean
-
#initialize ⇒ Result
constructor
A new instance of Result.
- #pending? ⇒ Boolean
- #success(value = nil) ⇒ Object
- #success? ⇒ Boolean
- #value! ⇒ Object
Constructor Details
#initialize ⇒ Result
Returns a new instance of Result.
198 199 200 201 |
# File 'lib/the_help/service.rb', line 198 def initialize self.status = :pending self.value = nil end |
Instance Attribute Details
#status ⇒ Object
Returns the value of attribute status.
196 197 198 |
# File 'lib/the_help/service.rb', line 196 def status @status end |
#value ⇒ Object
Returns the value of attribute value.
196 197 198 |
# File 'lib/the_help/service.rb', line 196 def value @value end |
Instance Method Details
#error(value = nil, &block) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/the_help/service.rb', line 221 def error(value = nil, &block) self.value = if block_given? begin self.value = block.call rescue StandardError => e e end else value end self.status = :error freeze end |
#error? ⇒ Boolean
211 212 213 |
# File 'lib/the_help/service.rb', line 211 def error? status == :error end |
#pending? ⇒ Boolean
203 204 205 |
# File 'lib/the_help/service.rb', line 203 def pending? status == :pending end |
#success(value = nil) ⇒ Object
215 216 217 218 219 |
# File 'lib/the_help/service.rb', line 215 def success(value = nil) self.value = value self.status = :success freeze end |
#success? ⇒ Boolean
207 208 209 |
# File 'lib/the_help/service.rb', line 207 def success? status == :success end |
#value! ⇒ Object
235 236 237 238 239 240 241 242 243 |
# File 'lib/the_help/service.rb', line 235 def value! raise TheHelp::NoResultError if pending? raise value if error? && value.is_a?(Exception) raise TheHelp::ResultError.new(value) if error? value end |