Class: TheHelp::Service::Result

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeResult

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

#statusObject

Returns the value of attribute status.



196
197
198
# File 'lib/the_help/service.rb', line 196

def status
  @status
end

#valueObject

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

Returns:

  • (Boolean)


211
212
213
# File 'lib/the_help/service.rb', line 211

def error?
  status == :error
end

#pending?Boolean

Returns:

  • (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

Returns:

  • (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