Class: Philiprehberger::Maybe::Some
- Inherits:
-
Object
- Object
- Philiprehberger::Maybe::Some
- Includes:
- Enumerable
- Defined in:
- lib/philiprehberger/maybe.rb
Overview
Container for a present value
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
The wrapped value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality check.
-
#[](key) ⇒ Some, None
Access a single key (shorthand for dig).
-
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support.
-
#dig(*keys) ⇒ Some, None
Dig into nested structures.
-
#each {|value| ... } ⇒ Enumerator, self
Yield the wrapped value for Enumerable support.
-
#filter {|value| ... } ⇒ Some, None
Filter the value based on a predicate.
-
#flat_map {|value| ... } ⇒ Some, None
Transform the wrapped value, expecting a Maybe return.
-
#initialize(value) ⇒ Some
constructor
A new instance of Some.
-
#inspect ⇒ String
(also: #to_s)
String representation.
-
#map {|value| ... } ⇒ Some, None
Transform the wrapped value.
-
#none? ⇒ Boolean
False.
-
#or_else(_default = nil) ⇒ Some
Return self since value is present.
-
#or_raise(_error_class = Error, _message = 'value is absent') ⇒ Object
Return the wrapped value since it is present.
-
#some? ⇒ Boolean
True.
-
#tap {|value| ... } ⇒ Some
Execute a block for side effects, returning self unchanged.
-
#zip(*others) ⇒ Some, None
Combine multiple Maybes into a single Maybe of an array.
Constructor Details
#initialize(value) ⇒ Some
Returns a new instance of Some.
43 44 45 |
# File 'lib/philiprehberger/maybe.rb', line 43 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the wrapped value.
48 49 50 |
# File 'lib/philiprehberger/maybe.rb', line 48 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Returns equality check.
143 144 145 |
# File 'lib/philiprehberger/maybe.rb', line 143 def ==(other) other.is_a?(Some) && other.value == @value end |
#[](key) ⇒ Some, None
Access a single key (shorthand for dig)
156 157 158 |
# File 'lib/philiprehberger/maybe.rb', line 156 def [](key) dig(key) end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support
138 139 140 |
# File 'lib/philiprehberger/maybe.rb', line 138 def deconstruct_keys(_keys) { value: @value, some: true, none: false } end |
#dig(*keys) ⇒ Some, None
Dig into nested structures
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/philiprehberger/maybe.rb', line 120 def dig(*keys) result = @value keys.each do |key| result = case result when Hash then result[key] when Array then result[key] else return None.instance end return None.instance if result.nil? end Maybe.wrap(result) end |
#each {|value| ... } ⇒ Enumerator, self
Yield the wrapped value for Enumerable support
54 55 56 57 58 59 |
# File 'lib/philiprehberger/maybe.rb', line 54 def each(&block) return to_enum(:each) unless block block.call(@value) self end |
#filter {|value| ... } ⇒ Some, None
Filter the value based on a predicate
94 95 96 97 98 99 100 |
# File 'lib/philiprehberger/maybe.rb', line 94 def filter(&block) if block.call(@value) self else None.instance end end |
#flat_map {|value| ... } ⇒ Some, None
Transform the wrapped value, expecting a Maybe return
83 84 85 86 87 88 |
# File 'lib/philiprehberger/maybe.rb', line 83 def flat_map(&block) result = block.call(@value) raise Error, 'flat_map block must return a Maybe' unless result.is_a?(Some) || result.is_a?(None) result end |
#inspect ⇒ String Also known as: to_s
Returns string representation.
148 149 150 |
# File 'lib/philiprehberger/maybe.rb', line 148 def inspect "Some(#{@value.inspect})" end |
#map {|value| ... } ⇒ Some, None
Transform the wrapped value
75 76 77 |
# File 'lib/philiprehberger/maybe.rb', line 75 def map(&block) Maybe.wrap(block.call(@value)) end |
#none? ⇒ Boolean
Returns false.
67 68 69 |
# File 'lib/philiprehberger/maybe.rb', line 67 def none? false end |
#or_else(_default = nil) ⇒ Some
Return self since value is present
105 106 107 |
# File 'lib/philiprehberger/maybe.rb', line 105 def or_else(_default = nil) self end |
#or_raise(_error_class = Error, _message = 'value is absent') ⇒ Object
Return the wrapped value since it is present
112 113 114 |
# File 'lib/philiprehberger/maybe.rb', line 112 def or_raise(_error_class = Error, = 'value is absent') @value end |
#some? ⇒ Boolean
Returns true.
62 63 64 |
# File 'lib/philiprehberger/maybe.rb', line 62 def some? true end |
#tap {|value| ... } ⇒ Some
Execute a block for side effects, returning self unchanged
178 179 180 181 |
# File 'lib/philiprehberger/maybe.rb', line 178 def tap(&block) block.call(@value) self end |
#zip(*others) ⇒ Some, None
Combine multiple Maybes into a single Maybe of an array
164 165 166 167 168 169 170 171 172 |
# File 'lib/philiprehberger/maybe.rb', line 164 def zip(*others) values = [@value] others.each do |other| return None.instance if other.none? values << other.value end Some.new(values) end |