Class: Philiprehberger::Result::Err

Inherits:
Object
  • Object
show all
Includes:
Filterable, Tappable
Defined in:
lib/philiprehberger/result/err.rb

Overview

Represents a failed result containing an error.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filterable

#filter

Methods included from Tappable

#tap_err, #tap_ok

Constructor Details

#initialize(error) ⇒ Err

Returns a new instance of Err.

Parameters:

  • error

    the error value



13
14
15
# File 'lib/philiprehberger/result/err.rb', line 13

def initialize(error)
  @error = error
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



10
11
12
# File 'lib/philiprehberger/result/err.rb', line 10

def error
  @error
end

Instance Method Details

#==(other) ⇒ Object



151
152
153
# File 'lib/philiprehberger/result/err.rb', line 151

def ==(other)
  other.is_a?(Err) && other.error == @error
end

#contains?(_other) ⇒ Boolean

Always false on Err (there is no value to compare).

Parameters:

  • _other

    ignored

Returns:

  • (Boolean)

    false



117
118
119
# File 'lib/philiprehberger/result/err.rb', line 117

def contains?(_other)
  false
end

#contains_err?(other) ⇒ Boolean

Check whether this Err contains the given error (using ==).

Parameters:

  • other

    the error to compare against

Returns:

  • (Boolean)

    true if the error equals other



125
126
127
# File 'lib/philiprehberger/result/err.rb', line 125

def contains_err?(other)
  @error == other
end

#deconstructArray

Pattern matching support via ‘in Err`.

Returns:

  • (Array)

    deconstructed error



139
140
141
# File 'lib/philiprehberger/result/err.rb', line 139

def deconstruct
  [@error]
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support via ‘in Err(error:)`.

Parameters:

  • keys (Array<Symbol>)

    requested keys

Returns:

  • (Hash)


147
148
149
# File 'lib/philiprehberger/result/err.rb', line 147

def deconstruct_keys(_keys)
  { error: @error }
end

#err?Boolean

Returns true.

Returns:

  • (Boolean)

    true



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

def err? = true

#flat_mapErr Also known as: and_then

Ignore the chained operation.

Returns:

  • (Err)

    self



33
34
35
# File 'lib/philiprehberger/result/err.rb', line 33

def flat_map
  self
end

#mapErr

Ignore the value transformation.

Returns:

  • (Err)

    self



26
27
28
# File 'lib/philiprehberger/result/err.rb', line 26

def map
  self
end

#map_err {|error| ... } ⇒ Err

Transform the error value.

Yields:

  • (error)

    the current error

Returns:

  • (Err)

    a new Err with the transformed error



67
68
69
# File 'lib/philiprehberger/result/err.rb', line 67

def map_err(&block)
  Err.new(block.call(@error))
end

#map_or(default) ⇒ Object

Return the default since there is no value to map.

Parameters:

  • default

    the fallback value

Returns:

  • the default



92
93
94
# File 'lib/philiprehberger/result/err.rb', line 92

def map_or(default, &)
  default
end

#ok?Boolean

Returns false.

Returns:

  • (Boolean)

    false



18
# File 'lib/philiprehberger/result/err.rb', line 18

def ok? = false

#or_else {|error| ... } ⇒ Ok, Err

Recover from an error by calling the block. The block should return a Result.

Yields:

  • (error)

    the current error

Returns:

  • (Ok, Err)

    the result of the block



76
77
78
# File 'lib/philiprehberger/result/err.rb', line 76

def or_else(&block)
  block.call(@error)
end

#recover(error_class = nil) ⇒ Object

Recover from specific error types



97
98
99
100
101
102
103
104
# File 'lib/philiprehberger/result/err.rb', line 97

def recover(error_class = nil)
  return self if error_class && !@error.is_a?(error_class)

  result = yield @error
  Result.ok(result)
rescue StandardError => e
  Result.err(e)
end

#to_hHash

Serialize to a hash.

Returns:

  • (Hash)


132
133
134
# File 'lib/philiprehberger/result/err.rb', line 132

def to_h
  { err: @error }
end

#to_maybenil

Convert to a Maybe-like value: the success value, or nil for Err.

Returns:

  • (nil)

    always



109
110
111
# File 'lib/philiprehberger/result/err.rb', line 109

def to_maybe
  nil
end

#to_sObject Also known as: inspect



155
156
157
# File 'lib/philiprehberger/result/err.rb', line 155

def to_s
  "Err(#{@error.inspect})"
end

#unwrap!Object

Raise because there is no success value.

Raises:



54
55
56
# File 'lib/philiprehberger/result/err.rb', line 54

def unwrap!
  raise UnwrapError, "Called unwrap! on Err(#{@error.inspect})"
end

#unwrap_err!Object

Return the error value.

Returns:

  • the error value



61
# File 'lib/philiprehberger/result/err.rb', line 61

def unwrap_err! = @error

#unwrap_or(default) ⇒ Object

Return the fallback value.

Parameters:

  • default

    the fallback

Returns:

  • the default value



41
# File 'lib/philiprehberger/result/err.rb', line 41

def unwrap_or(default) = default

#unwrap_or_else {|error| ... } ⇒ Object

Compute a fallback value from the error.

Yields:

  • (error)

    the current error

Returns:

  • the block’s return value



47
48
49
# File 'lib/philiprehberger/result/err.rb', line 47

def unwrap_or_else(&block)
  block.call(@error)
end

#zip(_other) ⇒ Object

Zip is a no-op on Err



84
85
86
# File 'lib/philiprehberger/result/err.rb', line 84

def zip(_other)
  self
end