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).
-
#contains?(value) ⇒ Boolean
Check whether the wrapped value equals the given value.
-
#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.
-
#flatten ⇒ Some, None
Flatten a single level of Maybe nesting.
-
#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.
-
#present? ⇒ Boolean
Alias for #some? — matches Rails-style naming.
-
#reject {|value| ... } ⇒ Some, None
Inverse of #filter — return None when the predicate is truthy.
-
#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.
70 71 72 |
# File 'lib/philiprehberger/maybe.rb', line 70 def initialize(value) @value = value end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the wrapped value.
75 76 77 |
# File 'lib/philiprehberger/maybe.rb', line 75 def value @value end |
Instance Method Details
#==(other) ⇒ Boolean
Returns equality check.
170 171 172 |
# File 'lib/philiprehberger/maybe.rb', line 170 def ==(other) other.is_a?(Some) && other.value == @value end |
#[](key) ⇒ Some, None
Access a single key (shorthand for dig)
183 184 185 |
# File 'lib/philiprehberger/maybe.rb', line 183 def [](key) dig(key) end |
#contains?(value) ⇒ Boolean
Check whether the wrapped value equals the given value
238 239 240 |
# File 'lib/philiprehberger/maybe.rb', line 238 def contains?(value) @value == value end |
#deconstruct_keys(_keys) ⇒ Hash
Pattern matching support
165 166 167 |
# File 'lib/philiprehberger/maybe.rb', line 165 def deconstruct_keys(_keys) { value: @value, some: true, none: false } end |
#dig(*keys) ⇒ Some, None
Dig into nested structures
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/philiprehberger/maybe.rb', line 147 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
81 82 83 84 85 86 |
# File 'lib/philiprehberger/maybe.rb', line 81 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
121 122 123 124 125 126 127 |
# File 'lib/philiprehberger/maybe.rb', line 121 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
110 111 112 113 114 115 |
# File 'lib/philiprehberger/maybe.rb', line 110 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 |
#flatten ⇒ Some, None
Flatten a single level of Maybe nesting
Some(Some(x)) becomes Some(x); Some(None) becomes None; Some(x) is unchanged.
227 228 229 230 231 232 |
# File 'lib/philiprehberger/maybe.rb', line 227 def flatten case @value when Some, None then @value else self end end |
#inspect ⇒ String Also known as: to_s
Returns string representation.
175 176 177 |
# File 'lib/philiprehberger/maybe.rb', line 175 def inspect "Some(#{@value.inspect})" end |
#map {|value| ... } ⇒ Some, None
Transform the wrapped value
102 103 104 |
# File 'lib/philiprehberger/maybe.rb', line 102 def map(&block) Maybe.wrap(block.call(@value)) end |
#none? ⇒ Boolean
Returns false.
94 95 96 |
# File 'lib/philiprehberger/maybe.rb', line 94 def none? false end |
#or_else(_default = nil) ⇒ Some
Return self since value is present
132 133 134 |
# File 'lib/philiprehberger/maybe.rb', line 132 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
139 140 141 |
# File 'lib/philiprehberger/maybe.rb', line 139 def or_raise(_error_class = Error, = 'value is absent') @value end |
#present? ⇒ Boolean
Alias for #some? — matches Rails-style naming
245 246 247 |
# File 'lib/philiprehberger/maybe.rb', line 245 def present? true end |
#reject {|value| ... } ⇒ Some, None
Inverse of #filter — return None when the predicate is truthy
214 215 216 217 218 219 220 |
# File 'lib/philiprehberger/maybe.rb', line 214 def reject(&block) if block.call(@value) None.instance else self end end |
#some? ⇒ Boolean
Returns true.
89 90 91 |
# File 'lib/philiprehberger/maybe.rb', line 89 def some? true end |
#tap {|value| ... } ⇒ Some
Execute a block for side effects, returning self unchanged
205 206 207 208 |
# File 'lib/philiprehberger/maybe.rb', line 205 def tap(&block) block.call(@value) self end |
#zip(*others) ⇒ Some, None
Combine multiple Maybes into a single Maybe of an array
191 192 193 194 195 196 197 198 199 |
# File 'lib/philiprehberger/maybe.rb', line 191 def zip(*others) values = [@value] others.each do |other| return None.instance if other.none? values << other.value end Some.new(values) end |