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



143
144
145
# File 'lib/philiprehberger/result/err.rb', line 143

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



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

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



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

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

#deconstructArray

Pattern matching support via ‘in Err`.

Returns:

  • (Array)

    deconstructed error



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

def deconstruct
  [@error]
end

#deconstruct_keys(_keys) ⇒ Hash

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

Parameters:

  • keys (Array<Symbol>)

    requested keys

Returns:

  • (Hash)


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

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



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

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



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

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



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

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

#recover(error_class = nil) ⇒ Object

Recover from specific error types



89
90
91
92
93
94
95
96
# File 'lib/philiprehberger/result/err.rb', line 89

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)


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

def to_h
  { err: @error }
end

#to_maybenil

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

Returns:

  • (nil)

    always



101
102
103
# File 'lib/philiprehberger/result/err.rb', line 101

def to_maybe
  nil
end

#to_sObject Also known as: inspect



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

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

#unwrap!Object

Raise because there is no success value.

Raises:



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

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

#unwrap_err!Object

Return the error value.

Returns:

  • the error value



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

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

#zip(_other) ⇒ Object

Zip is a no-op on Err



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

def zip(_other)
  self
end