Class: Mona::Result::Err

Inherits:
Object
  • Object
show all
Defined in:
lib/mona/result/err.rb

Overview

A Error or failure result, with optional reason, and metadata

Since:

  • 0.1.0

Direct Known Subclasses

Dict::Err

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(failure, reason = nil, **meta) ⇒ Err

Returns a new instance of Err.

Raises:

  • (ArgumentError)

Since:

  • 0.1.0



7
8
9
10
11
12
13
# File 'lib/mona/result/err.rb', line 7

def initialize(failure, reason = nil, **meta)
  raise ArgumentError, "meta can't contain :reason key" if meta.key?(:reason)

  @failure = failure.freeze
  @reason = reason
  @meta = meta
end

Instance Attribute Details

#failureObject (readonly)

Since:

  • 0.1.0



15
16
17
# File 'lib/mona/result/err.rb', line 15

def failure
  @failure
end

#metaObject (readonly)

Since:

  • 0.1.0



15
16
17
# File 'lib/mona/result/err.rb', line 15

def meta
  @meta
end

#reasonObject (readonly)

Since:

  • 0.1.0



15
16
17
# File 'lib/mona/result/err.rb', line 15

def reason
  @reason
end

Instance Method Details

#and_tapObject

Since:

  • 0.1.0



31
# File 'lib/mona/result/err.rb', line 31

def and_tap(&) = self

#and_thenObject

Since:

  • 0.1.0



29
# File 'lib/mona/result/err.rb', line 29

def and_then(&) = self

#deconstructObject

Since:

  • 0.1.0



35
# File 'lib/mona/result/err.rb', line 35

def deconstruct = [:err, @failure, @reason, @meta]

#deconstruct_keys(_keys = nil) ⇒ Object

Since:

  • 0.1.0



37
# File 'lib/mona/result/err.rb', line 37

def deconstruct_keys(_keys = nil) = { err: @failure, reason: @reason, **@meta }

#either(_ok, err) ⇒ Object

Since:

  • 0.1.0



27
# File 'lib/mona/result/err.rb', line 27

def either(_ok, err) = err.call(@failure, @reason, **@meta)

#err {|@failure, @reason, @meta| ... } ⇒ Object

Yields:

Since:

  • 0.1.0



25
# File 'lib/mona/result/err.rb', line 25

def err(&) = yield @failure, @reason, **@meta

#err?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



19
# File 'lib/mona/result/err.rb', line 19

def err? = true

#okObject

Since:

  • 0.1.0



23
# File 'lib/mona/result/err.rb', line 23

def ok(&) = nil

#ok?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



17
# File 'lib/mona/result/err.rb', line 17

def ok? = false

#or_elseObject

Since:

  • 0.1.0



33
# File 'lib/mona/result/err.rb', line 33

def or_else(&) = Result[yield @failure, @reason, **@meta]

#to_resultObject

Since:

  • 0.1.0



39
# File 'lib/mona/result/err.rb', line 39

def to_result = self

#to_sObject Also known as: inspect

Since:

  • 0.1.0



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_orObject

Since:

  • 0.1.0



21
# File 'lib/mona/result/err.rb', line 21

def value_or(&) = yield