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 =
%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.



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

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.



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

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.



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

def status
  @status
end

#valueObject (readonly)

Returns the value returned by calling the command.

Returns:

  • (Object)

    the value returned by calling the command.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compares the other object to the result.

Parameters:

  • other (#value, #success?)

    An object responding to, at minimum, #value and #success?. If present, the #failure? and #error values will also be compared.

Returns:

  • (Boolean)

    True if all present values match the result, otherwise false.



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

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?Boolean

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

Returns:

  • (Boolean)

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



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

def failure?
  @status == :failure
end

#success?Boolean

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

Returns:

  • (Boolean)

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



56
57
58
# File 'lib/cuprum/result.rb', line 56

def success?
  @status == :success
end

#to_cuprum_resultCuprum::Result

Returns The result.

Returns:



61
62
63
# File 'lib/cuprum/result.rb', line 61

def to_cuprum_result
  self
end