Class: Opted::Result::Err

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

Overview

Value object that represents an error result

Instance Method Summary collapse

Constructor Details

#initialize(error) ⇒ Err

Returns a new instance of Err.

Parameters:

  • error (Object)

    a non-nil error to wrap

Raises:

  • (ArgumentError)

    if provided error is nil



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

def initialize(error)
  if error.nil?
    fail ArgumentError.new("can't wrap nil")
  else
    @error = error
  end
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

If other object is also Opted::Result::Err and wraps equivalent error

Parameters:

  • other (Object)

    any object

Returns:

  • (Boolean)


18
19
20
# File 'lib/opted/result/err.rb', line 18

def ==(other)
  other.is_a?(Err) && unwrap_err! == other.unwrap_err!
end

#and(_other) ⇒ Ok, Err

Note:

Arguments provided will be eagerly evaluated. Use #and_then for lazy evaluation.

Returns other result if Ok, self if Opted::Result::Err

Examples:

Ok.new(1).and(Ok.new(2)) # => Ok.new(2)
Ok.new(1).and(Err.new(:err)) # => Err.new(:err)
Err.new(:e1).and(Ok.new(1)) # => Err.new(:e1)
Err.new(:e1).and(Err.new(:e2)) # => Err.new(:e1)

Parameters:

  • other (Ok, Err)

    other result

Returns:

  • (Ok, Err)

    self or other result

See Also:



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

def and(_other)
  self
end

#and_then {|value| ... } ⇒ Ok, Err

Note:

This differs from #map in that it allows you to turn Ok into Opted::Result::Err

Returns the result of calling the block with the inner value if Ok, self if Opted::Result::Err

Examples:

Ok.new(:foo)
  .and_then { |value| Err.new(value.upcase) }
  # => Err.new(:FOO)
Err.new(:err)
  .and_then { |value| Err.new(value.upcase) }
  # => Err.new(:err)

Yield Parameters:

  • value (Object)

    inner value

Yield Returns:

  • (Ok, Err)

    mapped result

Returns:

  • (Ok, Err)

    self or mapped result

See Also:



73
74
75
# File 'lib/opted/result/err.rb', line 73

def and_then
  self
end

#err?Boolean

If self is Opted::Result::Err

Examples:

Ok.new(1).err? # => false
Err.new(:err).err? # => true

Returns:

  • (Boolean)

See Also:



31
32
33
# File 'lib/opted/result/err.rb', line 31

def err?
  true
end

#map {|value| ... } ⇒ Ok, Err

Returns a result of the same type wrapping the result of applying the block to the original inner value, leaving errors untouched

Examples:

Ok.new(1)
  .map { |value| value + 1 }
  .unwrap! # => 2
Err.new(:err)
  .map { |value| value + 1 }
  .unwrap_err! # => :err

Yield Parameters:

  • value (Object)

Returns:

  • (Ok, Err)

    mapped result

See Also:



49
50
51
# File 'lib/opted/result/err.rb', line 49

def map
  self
end

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

Returns a result of the same type wrapping the result of applying the block to the original inner error, leaving values untouched

Examples:

Ok.new(1)
  .map_err { |error| error.upcase }
  .unwrap! # => 1
Err.new(:err)
  .map_err { |error| error.upcase }
  .unwrap_err! # => :ERR

Yield Parameters:

  • error (Object)

Returns:

  • (Ok, Err)

    mapped result

See Also:



55
56
57
# File 'lib/opted/result/err.rb', line 55

def map_err
  Err.new(yield unwrap_err!)
end

#match(&block) ⇒ Object

Returns the result of running either the ok or err branch provided to the block based on if the result is Ok or Opted::Result::Err

Examples:

Ok.new(1).match do
  ok { |value| value + 1 }
  err { |error| fail "unreachable" }
end # => 2

Err.new(:err).match do
  ok { |value| fail "unreachable" }
  err { |error| error.upcase }
end # => :ERR

Ok.new(1).match do
  ok { |value| value + 1 }
end # => RuntimeError: Must match on both ok and err results

Yield Returns:

  • (Object)

    mapped value

Returns:

  • (Object)

    mapped value

Raises:

  • (RuntimeError)

    unless both ok and error branches are defined

See Also:



91
92
93
# File 'lib/opted/result/err.rb', line 91

def match(&block)
  Match.match_error(unwrap_err!, &block)
end

#ok?Boolean

If self is Ok

Examples:

Ok.new(1).ok? # => true
Err.new(:err).ok? # => false

Returns:

  • (Boolean)

See Also:



25
26
27
# File 'lib/opted/result/err.rb', line 25

def ok?
  false
end

#or(other) ⇒ Ok, Err

Note:

Arguments provided will be eagerly evaluated. Use #or_else for lazy evaluation.

Returns self if Ok, other result if Opted::Result::Err

Examples:

Ok.new(1).or(Ok.new(2)) # => Ok.new(1)
Ok.new(1).or(Err.new(:err)) # => Ok.new(1)
Err.new(:e1).or(Ok.new(1)) # => Ok.new(:1)
Err.new(:e1).or(Err.new(:e2)) # => Err.new(:e2)

Parameters:

  • other (Ok, Err)

    other result

Returns:

  • (Ok, Err)

    self or other result

See Also:



79
80
81
# File 'lib/opted/result/err.rb', line 79

def or(other)
  other
end

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

Note:

This differs from #map_err in that it allows you to turn Opted::Result::Err into Ok

Returns self if Ok, the result of calling the block with the inner error if Opted::Result::Err

Examples:

Ok.new(:foo)
  .or_else { |error| Ok.new(error.upcase) }
  # => Ok.new(:foo)
Err.new(:err)
  .or_else { |error| Ok.new(error.upcase) }
  # => Ok.new(:ERR)

Yield Parameters:

  • value (Object)

    inner error

Yield Returns:

  • (Ok, Err)

    mapped result

Returns:

  • (Ok, Err)

    self or mapped result

See Also:



85
86
87
# File 'lib/opted/result/err.rb', line 85

def or_else
  yield unwrap_err!
end

#unwrap!Object

Returns the inner value if Ok

Examples:

Ok.new(1).unwrap! # => 1
Err.new(:err).unwrap! # => UnwrapError: Called #unwrap! on #<Opted::Result::Err:0x00007fbec7032798 @error=:err>

Returns:

  • (Object)

    value

Raises:

See Also:



37
38
39
# File 'lib/opted/result/err.rb', line 37

def unwrap!
  fail UnwrapError.new(__method__, inspect)
end

#unwrap_err!Object

Returns the inner error if Opted::Result::Err

Examples:

Ok.new(1).unwrap_err! # => UnwrapError: Called #unwrap_err! on #<Opted::Result::Ok:0x00007fbec7901c20 @value=1>
Err.new(:err).unwrap_err! # => :err

Returns:

  • (Object)

    error

Raises:

See Also:



43
44
45
# File 'lib/opted/result/err.rb', line 43

def unwrap_err!
  @error
end

#unwrap_or(other_value) ⇒ Object

Returns inner value if Ok, other value if Opted::Result::Err

Examples:

Ok.new(1).unwrap_or(2) # => 1
Err.new(:err).unwrap_or(2) # => 2

Returns:

  • (Object)

    inner value or other value

See Also:



61
62
63
# File 'lib/opted/result/err.rb', line 61

def unwrap_or(other_value)
  other_value
end