Class: Cuprum::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/result.rb

Overview

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

Constant Summary collapse

STATUSES =

Enumerates the default permitted values for a Result#status.

%i[success failure].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value: nil, error: nil, status: nil) ⇒ Result

Returns a new instance of Result.

Parameters:

  • value (Object) (defaults to: nil)

    the value returned by calling the command.

  • error (Object) (defaults to: nil)

    the error (if any) generated when the command was called. Can be a Cuprum::Error, a model errors object, etc.

  • status (String, Symbol) (defaults to: nil)

    the status of the result. Must be :success, :failure, or nil.



16
17
18
19
20
# File 'lib/cuprum/result.rb', line 16

def initialize(value: nil, error: nil, status: nil)
  @value  = value
  @error  = error
  @status = resolve_status(status)
end

Instance Attribute Details

#errorObject (readonly)

Returns the error (if any) generated when the command was called.

Returns:

  • (Object)

    the error (if any) generated when the command was called.



27
28
29
# File 'lib/cuprum/result.rb', line 27

def error
  @error
end

#statusSymbol (readonly)

Returns the status of the result, either :success or :failure.

Returns:

  • (Symbol)

    the status of the result, either :success or :failure.



30
31
32
# File 'lib/cuprum/result.rb', line 30

def status
  @status
end

#valueObject (readonly)

Returns the value returned by calling the command.

Returns:

  • (Object)

    the value returned by calling the command.



23
24
25
# File 'lib/cuprum/result.rb', line 23

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the other object to the result.

In order to match the result, the object must respond to the #to_h method, and the value of object.to_h must be equal to the value of result.properties.

Parameters:

  • other (#to_h)

    the result or object to compare.

Returns:

  • (Boolean)

    true if all values match the result, otherwise false.



41
42
43
44
45
46
47
# File 'lib/cuprum/result.rb', line 41

def ==(other)
  other = other.to_cuprum_result if other.respond_to?(:to_cuprum_result)

  return properties == other.to_h if other.respond_to?(:to_h)

  deprecated_compare(other)
end

#failure?Boolean

Returns true if the result status is :failure, otherwise false.

Returns:

  • (Boolean)

    true if the result status is :failure, otherwise false.



50
51
52
# File 'lib/cuprum/result.rb', line 50

def failure?
  @status == :failure
end

#propertiesHash{Symbol => Object} Also known as: to_h

Returns a Hash representation of the result.

Returns:

  • (Hash{Symbol => Object})

    a Hash representation of the result.



55
56
57
58
59
60
61
# File 'lib/cuprum/result.rb', line 55

def properties
  {
    error:  error,
    status: status,
    value:  value
  }
end

#success?Boolean

Returns true if the result status is :success, otherwise false.

Returns:

  • (Boolean)

    true if the result status is :success, otherwise false.



65
66
67
# File 'lib/cuprum/result.rb', line 65

def success?
  @status == :success
end

#to_cuprum_resultCuprum::Result

Returns The result.

Returns:



70
71
72
# File 'lib/cuprum/result.rb', line 70

def to_cuprum_result
  self
end