Module: Mona::Result
- Included in:
- DictResult, Err, OK
- Defined in:
- lib/mona/result.rb
Overview
Provides the result interface, including class must implement #either(on_ok, on_err)
Constant Summary collapse
- VERSION =
"0.3.0"
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #and_tap(&block) ⇒ Object
- #and_then(&block) ⇒ Object
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys = nil) ⇒ Object
- #either(_ok, _err) ⇒ Object
- #err(&block) ⇒ Object
- #err? ⇒ Boolean
- #ok(&block) ⇒ Object
- #ok? ⇒ Boolean
- #or_else(&block) ⇒ Object
- #value_or(&block) ⇒ Object
Class Method Details
Instance Method Details
#==(other) ⇒ Object
43 |
# File 'lib/mona/result.rb', line 43 def ==(other) = deconstruct == other.deconstruct |
#and_tap(&block) ⇒ Object
24 |
# File 'lib/mona/result.rb', line 24 def and_tap(&block) = tap { either block, ->(*_args) {} } |
#and_then(&block) ⇒ Object
26 |
# File 'lib/mona/result.rb', line 26 def and_then(&block) = either -> { Result[block.call _1] }, ->(*_args) { self } |
#deconstruct ⇒ Object
33 34 35 36 |
# File 'lib/mona/result.rb', line 33 def deconstruct # @type var meta: Hash[Symbol, untyped] either ->(value) { [:ok, value] }, ->(failure, reason, **) { [:err, failure, reason, ] } end |
#deconstruct_keys(_keys = nil) ⇒ Object
38 39 40 41 |
# File 'lib/mona/result.rb', line 38 def deconstruct_keys(_keys = nil) # @type var meta: Hash[Symbol, untyped] either ->(ok) { { ok: } }, ->(err, reason, **) { .merge(err:, reason:) } end |
#either(_ok, _err) ⇒ Object
12 |
# File 'lib/mona/result.rb', line 12 def either(_ok, _err) = raise(NotImplementedError, "implement #either") |
#err(&block) ⇒ Object
22 |
# File 'lib/mona/result.rb', line 22 def err(&block) = either ->(_) {}, block |
#err? ⇒ Boolean
18 |
# File 'lib/mona/result.rb', line 18 def err? = either ->(_) { false }, ->(*_args) { true } |
#ok(&block) ⇒ Object
20 |
# File 'lib/mona/result.rb', line 20 def ok(&block) = either block, ->(*_args) {} |
#ok? ⇒ Boolean
16 |
# File 'lib/mona/result.rb', line 16 def ok? = either ->(_) { true }, ->(*_args) { false } |
#or_else(&block) ⇒ Object
28 29 30 31 |
# File 'lib/mona/result.rb', line 28 def or_else(&block) # @type var meta: Hash[Symbol, untyped] either ->(_) { self }, ->(failure, reason, **) { Result[block.call failure, reason, **] } end |
#value_or(&block) ⇒ Object
14 |
# File 'lib/mona/result.rb', line 14 def value_or(&block) = either -> { _1 }, ->(*_args) { block.call } |