Class: Philiprehberger::Result::Err
- Inherits:
-
Object
- Object
- Philiprehberger::Result::Err
- Includes:
- Filterable, Tappable
- Defined in:
- lib/philiprehberger/result/err.rb
Overview
Represents a failed result containing an error.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#contains?(_other) ⇒ Boolean
Always false on Err (there is no value to compare).
-
#contains_err?(other) ⇒ Boolean
Check whether this Err contains the given error (using ==).
-
#deconstruct ⇒ Array
Pattern matching support via ‘in Err`.
-
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support via ‘in Err(error:)`.
-
#err? ⇒ Boolean
True.
-
#flat_map ⇒ Err
(also: #and_then)
Ignore the chained operation.
-
#initialize(error) ⇒ Err
constructor
A new instance of Err.
-
#map ⇒ Err
Ignore the value transformation.
-
#map_err {|error| ... } ⇒ Err
Transform the error value.
-
#map_or(default) ⇒ Object
Return the default since there is no value to map.
-
#ok? ⇒ Boolean
False.
-
#or_else {|error| ... } ⇒ Ok, Err
Recover from an error by calling the block.
-
#recover(error_class = nil) ⇒ Object
Recover from specific error types.
-
#to_h ⇒ Hash
Serialize to a hash.
-
#to_maybe ⇒ nil
Convert to a Maybe-like value: the success value, or nil for Err.
- #to_s ⇒ Object (also: #inspect)
-
#unwrap! ⇒ Object
Raise because there is no success value.
-
#unwrap_err! ⇒ Object
Return the error value.
-
#unwrap_or(default) ⇒ Object
Return the fallback value.
-
#zip(_other) ⇒ Object
Zip is a no-op on Err.
Methods included from Filterable
Methods included from Tappable
Constructor Details
#initialize(error) ⇒ Err
Returns a new instance of Err.
13 14 15 |
# File 'lib/philiprehberger/result/err.rb', line 13 def initialize(error) @error = error end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
10 11 12 |
# File 'lib/philiprehberger/result/err.rb', line 10 def error @error end |
Instance Method Details
#==(other) ⇒ Object
143 144 145 |
# File 'lib/philiprehberger/result/err.rb', line 143 def ==(other) other.is_a?(Err) && other.error == @error end |
#contains?(_other) ⇒ Boolean
Always false on Err (there is no value to compare).
109 110 111 |
# File 'lib/philiprehberger/result/err.rb', line 109 def contains?(_other) false end |
#contains_err?(other) ⇒ Boolean
Check whether this Err contains the given error (using ==).
117 118 119 |
# File 'lib/philiprehberger/result/err.rb', line 117 def contains_err?(other) @error == other end |
#deconstruct ⇒ Array
Pattern matching support via ‘in Err`.
131 132 133 |
# File 'lib/philiprehberger/result/err.rb', line 131 def deconstruct [@error] end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support via ‘in Err(error:)`.
139 140 141 |
# File 'lib/philiprehberger/result/err.rb', line 139 def deconstruct_keys(_keys) { error: @error } end |
#err? ⇒ Boolean
Returns true.
21 |
# File 'lib/philiprehberger/result/err.rb', line 21 def err? = true |
#flat_map ⇒ Err Also known as: and_then
Ignore the chained operation.
33 34 35 |
# File 'lib/philiprehberger/result/err.rb', line 33 def flat_map self end |
#map ⇒ Err
Ignore the value transformation.
26 27 28 |
# File 'lib/philiprehberger/result/err.rb', line 26 def map self end |
#map_err {|error| ... } ⇒ Err
Transform the error value.
59 60 61 |
# File 'lib/philiprehberger/result/err.rb', line 59 def map_err(&block) Err.new(block.call(@error)) end |
#map_or(default) ⇒ Object
Return the default since there is no value to map.
84 85 86 |
# File 'lib/philiprehberger/result/err.rb', line 84 def map_or(default, &) default end |
#ok? ⇒ Boolean
Returns false.
18 |
# File 'lib/philiprehberger/result/err.rb', line 18 def ok? = false |
#or_else {|error| ... } ⇒ Ok, Err
Recover from an error by calling the block. The block should return a Result.
68 69 70 |
# File 'lib/philiprehberger/result/err.rb', line 68 def or_else(&block) block.call(@error) end |
#recover(error_class = nil) ⇒ Object
Recover from specific error types
89 90 91 92 93 94 95 96 |
# File 'lib/philiprehberger/result/err.rb', line 89 def recover(error_class = nil) return self if error_class && !@error.is_a?(error_class) result = yield @error Result.ok(result) rescue StandardError => e Result.err(e) end |
#to_h ⇒ Hash
Serialize to a hash.
124 125 126 |
# File 'lib/philiprehberger/result/err.rb', line 124 def to_h { err: @error } end |
#to_maybe ⇒ nil
Convert to a Maybe-like value: the success value, or nil for Err.
101 102 103 |
# File 'lib/philiprehberger/result/err.rb', line 101 def to_maybe nil end |
#to_s ⇒ Object Also known as: inspect
147 148 149 |
# File 'lib/philiprehberger/result/err.rb', line 147 def to_s "Err(#{@error.inspect})" end |
#unwrap! ⇒ Object
Raise because there is no success value.
46 47 48 |
# File 'lib/philiprehberger/result/err.rb', line 46 def unwrap! raise UnwrapError, "Called unwrap! on Err(#{@error.inspect})" end |
#unwrap_err! ⇒ Object
Return the error value.
53 |
# File 'lib/philiprehberger/result/err.rb', line 53 def unwrap_err! = @error |
#unwrap_or(default) ⇒ Object
Return the fallback value.
41 |
# File 'lib/philiprehberger/result/err.rb', line 41 def unwrap_or(default) = default |
#zip(_other) ⇒ Object
Zip is a no-op on Err
76 77 78 |
# File 'lib/philiprehberger/result/err.rb', line 76 def zip(_other) self end |