Class: Decoding::Result Abstract

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

Overview

This class is abstract.

A result represent the outcome of some computation that can succeed or fail. The results are represented with two subclasses of Result: Ok and Err. Each hold a single result value.

The use of a result is the common interface provided to callers for transforming or chaining result values.

Direct Known Subclasses

Err, Ok

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Result

Returns a new instance of Result.



54
55
56
57
58
# File 'lib/decoding/result.rb', line 54

def initialize(value)
  @value = value
  @hash = [self.class, value].hash
  freeze
end

Instance Attribute Details

#hash ⇒ Object (readonly)



52
53
54
# File 'lib/decoding/result.rb', line 52

def hash
  @hash
end

Class Method Details

.all(results) ⇒ Result<a>

Collapse array of result values into a single result, or return the first Err value.

Examples:

all(Ok(1), Ok(2)) # => Ok([1, 2])
all(Ok(1), Err("error")) # => Err("error")

Parameters:

Returns:



38
39
40
41
42
# File 'lib/decoding/result.rb', line 38

def self.all(results)
  results.reduce(ok([])) do |acc, el|
    acc.and(el.to_result) { [*_1, _2] }
  end
end

.err(value) ⇒ Decoding::Err<a>

Construct a new Err value with the given value.

Parameters:

  • value (a)

Returns:



28
# File 'lib/decoding/result.rb', line 28

def self.err(value) = Err.new(value)

.ok(value) ⇒ Decoding::Ok<a>

Construct a new Ok value with the given value.

Parameters:

  • value (a)

Returns:



22
# File 'lib/decoding/result.rb', line 22

def self.ok(value) = Ok.new(value)

Instance Method Details

#and(other) {|left, right| ... } ⇒ Decoding::Result<c>

Combine two Result values if they are both Ok using the given block, or return the first Err value.

Parameters:

Yield Parameters:

  • left (Object)
  • right (Object)

Yield Returns:

  • (c)

Returns:



138
# File 'lib/decoding/result.rb', line 138

def and(_) = self

#and_then {|value| ... } ⇒ Decoding::Result<a>

Transform a result using a block that will also return a result.

Yield Parameters:

  • value (Object)

Yield Returns:

Returns:



145
# File 'lib/decoding/result.rb', line 145

def and_then = self

#deconstruct ⇒ Object



60
61
62
# File 'lib/decoding/result.rb', line 60

def deconstruct
  [value]
end

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

Returns:

  • (Boolean)


64
65
66
# File 'lib/decoding/result.rb', line 64

def eql?(other)
  other.is_a?(self.class) && value == other.value
end

#err? ⇒ Boolean

Whether this value is an Err value.

Returns:

  • (Boolean)


81
# File 'lib/decoding/result.rb', line 81

def err? = false

#inspect ⇒ Object



69
70
71
# File 'lib/decoding/result.rb', line 69

def inspect
  "#<#{self.class} #{value.inspect}>"
end

#map {|value| ... } ⇒ Decoding::Result<b>

Create a new Result value for the result of the block applied to this result's value. Err values are returned as-is.

Examples:

Result.ok(123).map { |i| i * 2 } # => Ok(246)
Result.err("error").map { |i| i * 2 } # => Err("error")

Yield Parameters:

  • value (a)

Yield Returns:

  • (b)

Returns:



116
# File 'lib/decoding/result.rb', line 116

def map = self

#map_err {|value| ... } ⇒ Decoding::Result<b>

Create a new Result value for the result of the block applied to this result's value. Ok values are returned as-is.

Examples:

Result.ok(123).map { |s| s.upcase } # => Ok(123)
Result.err("error").map { |s| s.upcase } # => Err("ERROR")

Yield Parameters:

  • value (a)

Yield Returns:

  • (b)

Returns:



127
# File 'lib/decoding/result.rb', line 127

def map_err = self

#ok? ⇒ Boolean

Whether this value is an Ok value.

Returns:

  • (Boolean)


76
# File 'lib/decoding/result.rb', line 76

def ok? = false

#to_result ⇒ Object



147
# File 'lib/decoding/result.rb', line 147

def to_result = self

#unwrap(default_value) ⇒ Object

Extract the value out of a Result value. In case of an Ok, this returns the result's value. In case of an Err, the given default_value is returned.

Parameters:

  • default_value (Object)

Returns:

  • (Object)


89
# File 'lib/decoding/result.rb', line 89

def unwrap(default_value) = default_value

#unwrap! ⇒ Object

Extract the value out of a Result value. In case of an Ok, this returns the result's value. In case of an Err, a UnwrapError is raised with the error value as its message.

Returns:

  • (Object)

Raises:



97
# File 'lib/decoding/result.rb', line 97

def unwrap! = raise(UnwrapError, value.to_s)

#unwrap_err(default_value) ⇒ Object

Extract the error value out of a Result value. In case of an Err, this returns the result's value. In case of an Ok, the given default_value is returned.

Parameters:

  • default_value (Object)

Returns:

  • (Object)


105
# File 'lib/decoding/result.rb', line 105

def unwrap_err(default_value) = default_value