Class: Mona::Result::Err
- Inherits:
-
Object
- Object
- Mona::Result::Err
- Defined in:
- lib/mona/result/err.rb
Overview
A Error or failure result, with optional reason, and metadata
Direct Known Subclasses
Instance Attribute Summary collapse
- #failure ⇒ Object readonly
- #meta ⇒ Object readonly
- #reason ⇒ Object readonly
Instance Method Summary collapse
- #and_tap ⇒ Object
- #and_then ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys = nil) ⇒ Object
- #either(_ok, err) ⇒ Object
- #err {|@failure, @reason, @meta| ... } ⇒ Object
- #err? ⇒ Boolean
-
#initialize(failure, reason = nil, **meta) ⇒ Err
constructor
A new instance of Err.
- #ok ⇒ Object
- #ok? ⇒ Boolean
- #or_else ⇒ Object
- #to_result ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #value_or ⇒ Object
Constructor Details
#initialize(failure, reason = nil, **meta) ⇒ Err
Returns a new instance of Err.
7 8 9 10 11 12 13 |
# File 'lib/mona/result/err.rb', line 7 def initialize(failure, reason = nil, **) raise ArgumentError, "meta can't contain :reason key" if .key?(:reason) @failure = failure.freeze @reason = reason = end |
Instance Attribute Details
#failure ⇒ Object (readonly)
15 16 17 |
# File 'lib/mona/result/err.rb', line 15 def failure @failure end |
#meta ⇒ Object (readonly)
15 16 17 |
# File 'lib/mona/result/err.rb', line 15 def end |
#reason ⇒ Object (readonly)
15 16 17 |
# File 'lib/mona/result/err.rb', line 15 def reason @reason end |
Instance Method Details
#and_tap ⇒ Object
31 |
# File 'lib/mona/result/err.rb', line 31 def and_tap(&) = self |
#and_then ⇒ Object
29 |
# File 'lib/mona/result/err.rb', line 29 def and_then(&) = self |
#deconstruct ⇒ Object
35 |
# File 'lib/mona/result/err.rb', line 35 def deconstruct = [:err, @failure, @reason, ] |
#deconstruct_keys(_keys = nil) ⇒ Object
37 |
# File 'lib/mona/result/err.rb', line 37 def deconstruct_keys(_keys = nil) = { err: @failure, reason: @reason, ** } |
#either(_ok, err) ⇒ Object
27 |
# File 'lib/mona/result/err.rb', line 27 def either(_ok, err) = err.call(@failure, @reason, **) |
#err {|@failure, @reason, @meta| ... } ⇒ Object
25 |
# File 'lib/mona/result/err.rb', line 25 def err(&) = yield @failure, @reason, ** |
#err? ⇒ Boolean
19 |
# File 'lib/mona/result/err.rb', line 19 def err? = true |
#ok ⇒ Object
23 |
# File 'lib/mona/result/err.rb', line 23 def ok(&) = nil |
#ok? ⇒ Boolean
17 |
# File 'lib/mona/result/err.rb', line 17 def ok? = false |
#or_else ⇒ Object
33 |
# File 'lib/mona/result/err.rb', line 33 def or_else(&) = Result[yield @failure, @reason, **] |
#to_result ⇒ Object
39 |
# File 'lib/mona/result/err.rb', line 39 def to_result = self |
#to_s ⇒ Object Also known as: inspect
41 42 43 |
# File 'lib/mona/result/err.rb', line 41 def to_s "#<Err #{@failure.inspect} #{{ reason: @reason, **@meta }.map{ "#{_1}: #{_2.inspect}" }.join(", ")}>" end |
#value_or ⇒ Object
21 |
# File 'lib/mona/result/err.rb', line 21 def value_or(&) = yield |