class Cuprum::Result

Data object that encapsulates the result of calling a Cuprum command.

Constants

STATUSES

Enumerates the default permitted values for a Result#status.

Attributes

error[R]

@return [Object] the error (if any) generated when the command was

called.
status[R]

@return [Symbol] the status of the result, either :success or :failure.

value[R]

@return [Object] the value returned by calling the command.

Public Class Methods

new(value: nil, error: nil, status: nil) click to toggle source

@param value [Object] The value returned by calling the command. @param error [Object] The error (if any) generated when the command was

called. Can be a Cuprum::Error, a model errors object, etc.

@param status [String, Symbol] The status of the result. Must be :success,

:failure, or nil.
# File lib/cuprum/result.rb, line 16
def initialize(value: nil, error: nil, status: nil)
  @value  = value
  @error  = error
  @status = resolve_status(status)
end

Public Instance Methods

==(other) click to toggle source

Compares the other object to the result.

@param other [#value, success?] An object responding to, at minimum,

#value and #success?. If present, the #failure? and #error values
will also be compared.

@return [Boolean] True if all present values match the result, otherwise

false.
# File lib/cuprum/result.rb, line 40
def ==(other)
  return false unless other.respond_to?(:value)  && other.value  == value
  return false unless other.respond_to?(:status) && other.status == status
  return false unless other.respond_to?(:error)  && other.error  == error

  true
end
failure?() click to toggle source

@return [Boolean] true if the result status is :failure, otherwise false.

# File lib/cuprum/result.rb, line 49
def failure?
  @status == :failure
end
success?() click to toggle source

@return [Boolean] true if the result status is :success, otherwise false.

# File lib/cuprum/result.rb, line 54
def success?
  @status == :success
end
to_cuprum_result() click to toggle source

@return [Cuprum::Result] The result.

# File lib/cuprum/result.rb, line 59
def to_cuprum_result
  self
end

Private Instance Methods

defined_statuses() click to toggle source
# File lib/cuprum/result.rb, line 65
def defined_statuses
  self.class::STATUSES
end
normalize_status(status) click to toggle source
# File lib/cuprum/result.rb, line 69
def normalize_status(status)
  return status unless status.is_a?(String) || status.is_a?(Symbol)

  tools.string_tools.underscore(status).intern
end
resolve_status(status) click to toggle source
# File lib/cuprum/result.rb, line 75
def resolve_status(status)
  return error.nil? ? :success : :failure if status.nil?

  normalized = normalize_status(status)

  return normalized if defined_statuses.include?(normalized)

  raise ArgumentError, "invalid status #{status.inspect}"
end
tools() click to toggle source
# File lib/cuprum/result.rb, line 85
def tools
  SleepingKingStudios::Tools::Toolbelt.instance
end