Class: Opted::Result::AbstractResult Abstract

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

Overview

This class is abstract.

This class exists purely to document the interface of both Ok and Err result types.

Instance Method Summary collapse

Instance Method Details

#and(other) ⇒ Ok, Err

Note:

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

Returns other result if Ok, self if 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



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

def and(other)
end

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

Note:

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

Returns the result of calling the block with the inner value if Ok, self if 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



103
104
# File 'lib/opted/result/abstract_result.rb', line 103

def and_then(&block)
end

#err?Boolean

If self is Err

Examples:

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

Returns:

  • (Boolean)


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

def err?
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



51
52
# File 'lib/opted/result/abstract_result.rb', line 51

def map(&block)
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



65
66
# File 'lib/opted/result/abstract_result.rb', line 65

def map_err(&block)
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 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



155
156
# File 'lib/opted/result/abstract_result.rb', line 155

def match(&block)
end

#ok?Boolean

If self is Ok

Examples:

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

Returns:

  • (Boolean)


11
12
# File 'lib/opted/result/abstract_result.rb', line 11

def ok?
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 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



116
117
# File 'lib/opted/result/abstract_result.rb', line 116

def or(other)
end

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

Note:

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

Returns self if Ok, the result of calling the block with the inner error if 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



133
134
# File 'lib/opted/result/abstract_result.rb', line 133

def or_else(&block)
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:



28
29
# File 'lib/opted/result/abstract_result.rb', line 28

def unwrap!
end

#unwrap_err!Object

Returns the inner error if 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:



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

def unwrap_err!
end

#unwrap_or(other_value) ⇒ Object

Returns inner value if Ok, other value if Err

Examples:

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

Returns:

  • (Object)

    inner value or other value



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

def unwrap_or(other_value)
end