Class: Opted::Result::Err
- Inherits:
-
Object
- Object
- Opted::Result::Err
- Defined in:
- lib/opted/result/err.rb
Overview
Value object that represents an error result
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
If other object is also Err and wraps equivalent error.
- #and(_other) ⇒ Ok, Err
- #and_then {|value| ... } ⇒ Ok, Err
-
#err? ⇒ Boolean
If
self
is Err. -
#initialize(error) ⇒ Err
constructor
A new instance of Err.
-
#map {|value| ... } ⇒ Ok, Err
Returns a result of the same type wrapping the result of applying the block to the original inner value, leaving errors untouched.
-
#map_err {|error| ... } ⇒ Ok, Err
Returns a result of the same type wrapping the result of applying the block to the original inner error, leaving values untouched.
- #match(&block) ⇒ Object
-
#ok? ⇒ Boolean
If
self
is Ok. - #or(other) ⇒ Ok, Err
- #or_else {|value| ... } ⇒ Ok, Err
-
#unwrap! ⇒ Object
Returns the inner value if Ok.
-
#unwrap_err! ⇒ Object
Returns the inner error if Err.
- #unwrap_or(other_value) ⇒ Object
Constructor Details
#initialize(error) ⇒ Err
Returns a new instance of Err.
7 8 9 10 11 12 13 |
# File 'lib/opted/result/err.rb', line 7 def initialize(error) if error.nil? fail ArgumentError.new("can't wrap nil") else @error = error end end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
If other object is also Opted::Result::Err and wraps equivalent error
18 19 20 |
# File 'lib/opted/result/err.rb', line 18 def ==(other) other.is_a?(Err) && unwrap_err! == other.unwrap_err! end |
#and(_other) ⇒ Ok, Err
Arguments provided will be eagerly evaluated. Use #and_then for lazy evaluation.
Returns other result if Ok, self
if Opted::Result::Err
67 68 69 |
# File 'lib/opted/result/err.rb', line 67 def and(_other) self end |
#and_then {|value| ... } ⇒ Ok, Err
This differs from #map in that it allows you to turn Ok into Opted::Result::Err
Returns the result of calling the block with the inner value if Ok, self if Opted::Result::Err
73 74 75 |
# File 'lib/opted/result/err.rb', line 73 def and_then self end |
#err? ⇒ Boolean
If self
is Opted::Result::Err
31 32 33 |
# File 'lib/opted/result/err.rb', line 31 def err? true end |
#map {|value| ... } ⇒ Ok, Err
Returns a result of the same type wrapping the result of applying the block to the original inner value, leaving errors untouched
49 50 51 |
# File 'lib/opted/result/err.rb', line 49 def map self end |
#map_err {|error| ... } ⇒ Ok, Err
Returns a result of the same type wrapping the result of applying the block to the original inner error, leaving values untouched
55 56 57 |
# File 'lib/opted/result/err.rb', line 55 def map_err Err.new(yield unwrap_err!) end |
#match(&block) ⇒ Object
Returns the result of running either the ok
or err
branch provided to the block based on if the result is Ok or Opted::Result::Err
91 92 93 |
# File 'lib/opted/result/err.rb', line 91 def match(&block) Match.match_error(unwrap_err!, &block) end |
#or(other) ⇒ Ok, Err
Arguments provided will be eagerly evaluated. Use #or_else for lazy evaluation.
Returns self if Ok, other result if Opted::Result::Err
79 80 81 |
# File 'lib/opted/result/err.rb', line 79 def or(other) other end |
#or_else {|value| ... } ⇒ Ok, Err
This differs from #map_err in that it allows you to turn Opted::Result::Err into Ok
Returns self if Ok, the result of calling the block with the inner error if Opted::Result::Err
85 86 87 |
# File 'lib/opted/result/err.rb', line 85 def or_else yield unwrap_err! end |
#unwrap! ⇒ Object
Returns the inner value if Ok
37 38 39 |
# File 'lib/opted/result/err.rb', line 37 def unwrap! fail UnwrapError.new(__method__, inspect) end |
#unwrap_err! ⇒ Object
Returns the inner error if Opted::Result::Err
43 44 45 |
# File 'lib/opted/result/err.rb', line 43 def unwrap_err! @error end |
#unwrap_or(other_value) ⇒ Object
Returns inner value if Ok, other value if Opted::Result::Err
61 62 63 |
# File 'lib/opted/result/err.rb', line 61 def unwrap_or(other_value) other_value end |