Class: Philiprehberger::Result::Ok

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

Overview

Represents a successful result containing a value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Filterable

#filter

Methods included from Tappable

#tap_err, #tap_ok

Constructor Details

#initialize(value) ⇒ Ok

Returns a new instance of Ok.

Parameters:

  • value

    the success value



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

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



141
142
143
# File 'lib/philiprehberger/result/ok.rb', line 141

def ==(other)
  other.is_a?(Ok) && other.value == @value
end

#contains?(other) ⇒ Boolean

Check whether this Ok contains the given value (using ==).

Parameters:

  • other

    the value to compare against

Returns:

  • (Boolean)

    true if the value equals other



107
108
109
# File 'lib/philiprehberger/result/ok.rb', line 107

def contains?(other)
  @value == other
end

#contains_err?(_other) ⇒ Boolean

Always false on Ok (there is no error to compare).

Parameters:

  • _other

    ignored

Returns:

  • (Boolean)

    false



115
116
117
# File 'lib/philiprehberger/result/ok.rb', line 115

def contains_err?(_other)
  false
end

#deconstructArray

Pattern matching support via ‘in Ok`.

Returns:

  • (Array)

    deconstructed value



129
130
131
# File 'lib/philiprehberger/result/ok.rb', line 129

def deconstruct
  [@value]
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support via ‘in Ok(value:)`.

Parameters:

  • keys (Array<Symbol>)

    requested keys

Returns:

  • (Hash)


137
138
139
# File 'lib/philiprehberger/result/ok.rb', line 137

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

#err?Boolean

Returns false.

Returns:

  • (Boolean)

    false



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

def err? = false

#flat_map {|value| ... } ⇒ Ok, Err Also known as: and_then

Chain a result-returning operation.

Yields:

  • (value)

    the current value

Returns:

  • (Ok, Err)

    the result of the block



35
36
37
# File 'lib/philiprehberger/result/ok.rb', line 35

def flat_map(&block)
  block.call(@value)
end

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

Transform the success value.

Yields:

  • (value)

    the current value

Returns:

  • (Ok)

    a new Ok with the transformed value



27
28
29
# File 'lib/philiprehberger/result/ok.rb', line 27

def map(&block)
  Ok.new(block.call(@value))
end

#map_errOk

Ignore the error transformation.

Returns:

  • (Ok)

    self



61
62
63
# File 'lib/philiprehberger/result/ok.rb', line 61

def map_err
  self
end

#map_or(_default) {|value| ... } ⇒ Object

Map the value with a fallback for Err.

Parameters:

  • _default

    ignored

Yields:

  • (value)

    the current value

Returns:

  • the result of the block



87
88
89
# File 'lib/philiprehberger/result/ok.rb', line 87

def map_or(_default, &block)
  block.call(@value)
end

#ok?Boolean

Returns true.

Returns:

  • (Boolean)

    true



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

def ok? = true

#or_elseOk

Return self (no error to recover from).

Returns:

  • (Ok)

    self



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

def or_else
  self
end

#recover(_error_class = nil) ⇒ Object

Recovery is a no-op on Ok



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

def recover(_error_class = nil)
  self
end

#to_hHash

Serialize to a hash.

Returns:

  • (Hash)


122
123
124
# File 'lib/philiprehberger/result/ok.rb', line 122

def to_h
  { ok: @value }
end

#to_maybeObject

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

Returns:

  • the success value



99
100
101
# File 'lib/philiprehberger/result/ok.rb', line 99

def to_maybe
  @value
end

#to_sObject Also known as: inspect



145
146
147
# File 'lib/philiprehberger/result/ok.rb', line 145

def to_s
  "Ok(#{@value.inspect})"
end

#unwrap!Object

Return the value.

Returns:

  • the success value

Raises:

  • never



49
# File 'lib/philiprehberger/result/ok.rb', line 49

def unwrap! = @value

#unwrap_err!Object

Raise because Ok has no error to unwrap.

Raises:



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

def unwrap_err!
  raise UnwrapError, "Called unwrap_err! on Ok(#{@value.inspect})"
end

#unwrap_or(_default) ⇒ Object

Return the value or ignore the fallback.

Parameters:

  • _default

    ignored

Returns:

  • the success value



43
# File 'lib/philiprehberger/result/ok.rb', line 43

def unwrap_or(_default) = @value

#zip(other) ⇒ Object

Combine two Ok results into Ok([a, b])



76
77
78
79
80
# File 'lib/philiprehberger/result/ok.rb', line 76

def zip(other)
  return other if other.err?

  Result.ok([@value, other.instance_variable_get(:@value)])
end