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

Class Method Details

.[](obj) ⇒ Object



10
# File 'lib/mona/result.rb', line 10

def self.[](obj) = obj.is_a?(Result) ? obj : OK[obj]

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 }

#deconstructObject



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, **meta) { [:err, failure, reason, meta] }
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, **meta) { meta.merge(err:, reason:) }
end

#either(_ok, _err) ⇒ Object

Raises:

  • (NotImplementedError)


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

Returns:

  • (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

Returns:

  • (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, **meta) { Result[block.call failure, reason, **meta] }
end

#value_or(&block) ⇒ Object



14
# File 'lib/mona/result.rb', line 14

def value_or(&block) = either -> { _1 }, ->(*_args) { block.call }