Class: Cuprum::Result
- Inherits:
-
Object
- Object
- Cuprum::Result
- 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
-
#error ⇒ Object
readonly
The error (if any) generated when the command was called.
-
#status ⇒ Symbol
readonly
The status of the result, either :success or :failure.
-
#value ⇒ Object
readonly
The value returned by calling the command.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Compares the other object to the result.
-
#failure? ⇒ Boolean
True if the result status is :failure, otherwise false.
-
#initialize(value: nil, error: nil, status: nil) ⇒ Result
constructor
A new instance of Result.
-
#properties ⇒ Hash{Symbol => Object}
(also: #to_h)
A Hash representation of the result.
-
#success? ⇒ Boolean
True if the result status is :success, otherwise false.
-
#to_cuprum_result ⇒ Cuprum::Result
The result.
Constructor Details
#initialize(value: nil, error: nil, status: nil) ⇒ Result
Returns a new instance of Result.
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
#error ⇒ Object (readonly)
Returns the error (if any) generated when the command was called.
27 28 29 |
# File 'lib/cuprum/result.rb', line 27 def error @error end |
#status ⇒ Symbol (readonly)
Returns the status of the result, either :success or :failure.
30 31 32 |
# File 'lib/cuprum/result.rb', line 30 def status @status end |
#value ⇒ Object (readonly)
Returns 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.
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.
50 51 52 |
# File 'lib/cuprum/result.rb', line 50 def failure? @status == :failure end |
#properties ⇒ Hash{Symbol => Object} Also known as: to_h
Returns 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.
65 66 67 |
# File 'lib/cuprum/result.rb', line 65 def success? @status == :success end |
#to_cuprum_result ⇒ Cuprum::Result
Returns The result.
70 71 72 |
# File 'lib/cuprum/result.rb', line 70 def to_cuprum_result self end |