Class: Decoding::Result Abstract
- Inherits:
-
Object
- Object
- Decoding::Result
- Defined in:
- lib/decoding/result.rb
Overview
A result represent the outcome of some computation that can succeed or fail.
The results are represented with two subclasses of Result: Ok and Err.
Each hold a single result value.
The use of a result is the common interface provided to callers for transforming or chaining result values.
Instance Attribute Summary collapse
- #hash ⇒ Object readonly
Class Method Summary collapse
-
.all(results) ⇒ Result<a>
Collapse array of result values into a single result, or return the first
Errvalue. -
.err(value) ⇒ Decoding::Err<a>
Construct a new
Errvalue with the givenvalue. -
.ok(value) ⇒ Decoding::Ok<a>
Construct a new
Okvalue with the givenvalue.
Instance Method Summary collapse
-
#and(other) {|left, right| ... } ⇒ Decoding::Result<c>
Combine two
Resultvalues if they are bothOkusing the given block, or return the firstErrvalue. -
#and_then {|value| ... } ⇒ Decoding::Result<a>
Transform a result using a block that will also return a result.
- #deconstruct ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#err? ⇒ Boolean
Whether this value is an
Errvalue. -
#initialize(value) ⇒ Result
constructor
A new instance of Result.
- #inspect ⇒ Object
-
#map {|value| ... } ⇒ Decoding::Result<b>
Create a new
Resultvalue for the result of the block applied to this result'svalue. -
#map_err {|value| ... } ⇒ Decoding::Result<b>
Create a new
Resultvalue for the result of the block applied to this result'svalue. -
#ok? ⇒ Boolean
Whether this value is an
Okvalue. - #to_result ⇒ Object
-
#unwrap(default_value) ⇒ Object
Extract the value out of a
Resultvalue. -
#unwrap! ⇒ Object
Extract the value out of a
Resultvalue. -
#unwrap_err(default_value) ⇒ Object
Extract the error value out of a
Resultvalue.
Constructor Details
#initialize(value) ⇒ Result
Returns a new instance of Result.
52 53 54 55 56 |
# File 'lib/decoding/result.rb', line 52 def initialize(value) @value = value @hash = [self.class, value].hash freeze end |
Instance Attribute Details
#hash ⇒ Object (readonly)
50 51 52 |
# File 'lib/decoding/result.rb', line 50 def hash @hash end |
Class Method Details
.all(results) ⇒ Result<a>
Collapse array of result values into a single result, or return the
first Err value.
36 37 38 39 40 |
# File 'lib/decoding/result.rb', line 36 def self.all(results) results.reduce(ok([])) do |acc, el| acc.and(el.to_result) { [*_1, _2] } end end |
.err(value) ⇒ Decoding::Err<a>
Construct a new Err value with the given value.
26 |
# File 'lib/decoding/result.rb', line 26 def self.err(value) = Err.new(value) |
.ok(value) ⇒ Decoding::Ok<a>
Construct a new Ok value with the given value.
20 |
# File 'lib/decoding/result.rb', line 20 def self.ok(value) = Ok.new(value) |
Instance Method Details
#and(other) {|left, right| ... } ⇒ Decoding::Result<c>
Combine two Result values if they are both Ok using the given block, or
return the first Err value.
136 |
# File 'lib/decoding/result.rb', line 136 def and(_) = self |
#and_then {|value| ... } ⇒ Decoding::Result<a>
Transform a result using a block that will also return a result.
143 |
# File 'lib/decoding/result.rb', line 143 def and_then = self |
#deconstruct ⇒ Object
58 59 60 |
# File 'lib/decoding/result.rb', line 58 def deconstruct [value] end |
#eql?(other) ⇒ Boolean Also known as: ==
62 63 64 |
# File 'lib/decoding/result.rb', line 62 def eql?(other) other.is_a?(self.class) && value == other.value end |
#err? ⇒ Boolean
Whether this value is an Err value.
79 |
# File 'lib/decoding/result.rb', line 79 def err? = false |
#inspect ⇒ Object
67 68 69 |
# File 'lib/decoding/result.rb', line 67 def inspect "#<#{self.class} #{value.inspect}>" end |
#map {|value| ... } ⇒ Decoding::Result<b>
Create a new Result value for the result of the block applied to this
result's value. Err values are returned as-is.
114 |
# File 'lib/decoding/result.rb', line 114 def map = self |
#map_err {|value| ... } ⇒ Decoding::Result<b>
Create a new Result value for the result of the block applied to this
result's value. Ok values are returned as-is.
125 |
# File 'lib/decoding/result.rb', line 125 def map_err = self |
#ok? ⇒ Boolean
Whether this value is an Ok value.
74 |
# File 'lib/decoding/result.rb', line 74 def ok? = false |
#to_result ⇒ Object
145 |
# File 'lib/decoding/result.rb', line 145 def to_result = self |
#unwrap(default_value) ⇒ Object
Extract the value out of a Result value. In case of an Ok, this
returns the result's value. In case of an Err, the given default_value
is returned.
87 |
# File 'lib/decoding/result.rb', line 87 def unwrap(default_value) = default_value |
#unwrap! ⇒ Object
Extract the value out of a Result value. In case of an Ok, this
returns the result's value. In case of an Err, a UnwrapError is
raised with the error value as its message.
95 |
# File 'lib/decoding/result.rb', line 95 def unwrap! = raise(UnwrapError, value.to_s) |
#unwrap_err(default_value) ⇒ Object
Extract the error value out of a Result value. In case of an Err, this
returns the result's value. In case of an Ok, the given default_value
is returned.
103 |
# File 'lib/decoding/result.rb', line 103 def unwrap_err(default_value) = default_value |