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.



174
175
176
177
# File 'lib/the_help/service.rb', line 174

def initialize
  self.status = :pending
  self.value = nil
end

Instance Attribute Details

#statusObject

Returns the value of attribute status.



172
173
174
# File 'lib/the_help/service.rb', line 172

def status
  @status
end

#valueObject

Returns the value of attribute value.



172
173
174
# File 'lib/the_help/service.rb', line 172

def value
  @value
end

Instance Method Details

#error(value) ⇒ Object



197
198
199
200
201
# File 'lib/the_help/service.rb', line 197

def error(value)
  self.value = value
  self.status = :error
  freeze
end

#error?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/the_help/service.rb', line 187

def error?
  status == :error
end

#pending?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/the_help/service.rb', line 179

def pending?
  status == :pending
end

#success(value = nil) ⇒ Object



191
192
193
194
195
# File 'lib/the_help/service.rb', line 191

def success(value = nil)
  self.value = value
  self.status = :success
  freeze
end

#success?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/the_help/service.rb', line 183

def success?
  status == :success
end

#value!Object



203
204
205
206
207
208
209
210
211
# File 'lib/the_help/service.rb', line 203

def value!
  raise TheHelp::NoResultError if pending?

  raise value if error? && value.is_a?(Exception)

  raise TheHelp::ResultError.new(value) if error?

  value
end