Class: Errgonomic::Option::Any
- Defined in:
- lib/errgonomic/option.rb,
lib/errgonomic/rails/active_record_optional.rb
Overview
The base class for all options. Some and None are subclasses.
Instance Method Summary collapse
-
#==(other) ⇒ Object
An option of the same type with an equal inner value is equal.
-
#and(other) ⇒ Object
If self is Some, return the provided other Option.
-
#and_then(&block) ⇒ Object
If self is Some, call the given block with the inner value and return its result.
- #deconstruct ⇒ Object
-
#expect!(msg) ⇒ Object
returns the inner value if pressent, else raises an error with the given message.
-
#map(&block) ⇒ Object
Maps the Option to another Option by applying a function to the contained value (if Some) or returns None.
-
#map_or(default, &block) ⇒ Object
Returns the provided default (if none), or applies a function to the contained value (if some).
-
#map_or_else(proc, &block) ⇒ Object
Computes a default from the given Proc if None, or applies the block to the contained value (if Some).
-
#none_or(&block) ⇒ Object
(also: #none_or?)
return true if the contained value is None or the block returns truthy.
-
#ok ⇒ Object
convert the option into a result where Some is Ok and None is Err.
-
#ok_or(err) ⇒ Object
Transforms the option into a result, mapping Some(v) to Ok(v) and None to Err(err).
-
#ok_or_else(&block) ⇒ Object
Transforms the option into a result, mapping Some(v) to Ok(v) and None to Err(err).
-
#or(other) ⇒ Object
Returns the option if it contains a value, otherwise returns the provided Option.
-
#or_else(&block) ⇒ Object
Returns the option if it contains a value, otherwise calls the block and returns the result.
-
#some_and(&block) ⇒ Object
(also: #some_and?)
return true if the contained value is Some and the block returns truthy.
-
#tap_some(&block) ⇒ Object
Calls a function with the inner value, if Some, but returns the original option.
-
#to_a ⇒ Object
return an Array with the contained value, if any.
-
#to_json(*_args) ⇒ Object
Refuse to serialize an unwrapped Option as JSON.
-
#to_s ⇒ Object
Refuse to serialize an unwrapped Option as a String.
-
#unwrap! ⇒ Object
returns the inner value if present, else raises an error.
-
#unwrap_or(default) ⇒ Object
returns the inner value if present, else returns the default value.
-
#unwrap_or_else(&block) ⇒ Object
returns the inner value if present, else returns the result of the provided block.
-
#zip(other) ⇒ Object
Zips self with another Option.
-
#zip_with(other, &block) ⇒ Object
Zip two options using the block passed.
Instance Method Details
#==(other) ⇒ Object
An option of the same type with an equal inner value is equal.
Because we’re going to monkey patch this into other libraries Rails, we allow some “pass through” functionality into the inner value of a Some, such as comparability here.
TODO: does None == null?
strict:
Some(1) == 1 # => raise Errgonomic::NotComparableError, "Cannot compare Errgonomic::Option::Some with Integer"
30 31 32 33 34 35 |
# File 'lib/errgonomic/option.rb', line 30 def ==(other) return false if self.class != other.class return true if none? value == other.value end |
#and(other) ⇒ Object
If self is Some, return the provided other Option.
272 273 274 275 276 |
# File 'lib/errgonomic/option.rb', line 272 def and(other) return self if none? other end |
#and_then(&block) ⇒ Object
If self is Some, call the given block with the inner value and return its result. Block must return an Option.
284 285 286 287 288 289 290 291 292 293 |
# File 'lib/errgonomic/option.rb', line 284 def and_then(&block) return self if none? val = block.call(value) if !Errgonomic.give_me_ambiguous_downstream_errors? && !val.is_a?(Errgonomic::Option::Any) raise Errgonomic::ArgumentError.new, "block must return an Option, was #{val.class.name}" end val end |
#deconstruct ⇒ Object
47 48 49 50 51 |
# File 'lib/errgonomic/option.rb', line 47 def deconstruct return [self, value] if some? [Errgonomic::Option::None] end |
#expect!(msg) ⇒ Object
returns the inner value if pressent, else raises an error with the given message
106 107 108 109 110 |
# File 'lib/errgonomic/option.rb', line 106 def expect!(msg) raise Errgonomic::ExpectError, msg if none? value end |
#map(&block) ⇒ Object
Maps the Option to another Option by applying a function to the contained value (if Some) or returns None. Raises a pedantic exception if the return value of the block is not an Option.
167 168 169 170 171 |
# File 'lib/errgonomic/option.rb', line 167 def map(&block) return self if none? Some(block.call(value)) end |
#map_or(default, &block) ⇒ Object
Returns the provided default (if none), or applies a function to the contained value (if some). If you want lazy evaluation for the provided value, use map_or_else.
181 182 183 184 185 |
# File 'lib/errgonomic/option.rb', line 181 def map_or(default, &block) return Some(default) if none? Some(block.call(value)) end |
#map_or_else(proc, &block) ⇒ Object
Computes a default from the given Proc if None, or applies the block to the contained value (if Some).
194 195 196 197 198 199 200 201 |
# File 'lib/errgonomic/option.rb', line 194 def map_or_else(proc, &block) if none? val = proc.call return val ? Some(val) : None() end Some(block.call(value)) end |
#none_or(&block) ⇒ Object Also known as: none_or?
return true if the contained value is None or the block returns truthy
73 74 75 76 77 |
# File 'lib/errgonomic/option.rb', line 73 def none_or(&block) return true if none? !!block.call(value) end |
#ok ⇒ Object
convert the option into a result where Some is Ok and None is Err
207 208 209 210 211 |
# File 'lib/errgonomic/option.rb', line 207 def ok return Errgonomic::Result::Ok.new(value) if some? Errgonomic::Result::Err.new end |
#ok_or(err) ⇒ Object
Transforms the option into a result, mapping Some(v) to Ok(v) and None to Err(err)
218 219 220 221 222 |
# File 'lib/errgonomic/option.rb', line 218 def ok_or(err) return Errgonomic::Result::Ok.new(value) if some? Errgonomic::Result::Err.new(err) end |
#ok_or_else(&block) ⇒ Object
Transforms the option into a result, mapping Some(v) to Ok(v) and None to Err(err). TODO: block or proc?
230 231 232 233 234 |
# File 'lib/errgonomic/option.rb', line 230 def ok_or_else(&block) return Errgonomic::Result::Ok.new(value) if some? Errgonomic::Result::Err.new(block.call) end |
#or(other) ⇒ Object
Returns the option if it contains a value, otherwise returns the provided Option. Returns an Option.
242 243 244 245 246 247 248 |
# File 'lib/errgonomic/option.rb', line 242 def or(other) raise ArgumentError, "other must be an Option, was #{other.class.name}" unless other.is_a?(Any) return self if some? other end |
#or_else(&block) ⇒ Object
Returns the option if it contains a value, otherwise calls the block and returns the result. Returns an Option.
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/errgonomic/option.rb', line 256 def or_else(&block) return self if some? val = block.call if !val.is_a?(Errgonomic::Option::Any) && !Errgonomic.give_me_ambiguous_downstream_errors? raise Errgonomic::ArgumentError.new, "block must return an Option, was #{val.class.name}" end val end |
#some_and(&block) ⇒ Object Also known as: some_and?
return true if the contained value is Some and the block returns truthy
59 60 61 62 63 |
# File 'lib/errgonomic/option.rb', line 59 def some_and(&block) return false if none? !!block.call(value) end |
#tap_some(&block) ⇒ Object
Calls a function with the inner value, if Some, but returns the original option. In Rust, this is “inspect” but that clashes with Ruby conventions. We call this “tap_some” to avoid further clashing with “tap.”
155 156 157 158 |
# File 'lib/errgonomic/option.rb', line 155 def tap_some(&block) block.call(value) if some? self end |
#to_a ⇒ Object
return an Array with the contained value, if any
85 86 87 88 89 |
# File 'lib/errgonomic/option.rb', line 85 def to_a return [] if none? [value] end |
#to_json(*_args) ⇒ Object
Refuse to serialize an unwrapped Option as JSON. Not only should we require that options be correctly handled to access their inner value, but without this we will get undefined structures from default Object#to_json implementations.
341 342 343 |
# File 'lib/errgonomic/option.rb', line 341 def to_json(*_args) raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Option' end |
#to_s ⇒ Object
Refuse to serialize an unwrapped Option as a String. Options must be correctly handled to access their inner value.
330 331 332 |
# File 'lib/errgonomic/option.rb', line 330 def to_s raise Errgonomic::SerializeError, 'cannot serialize an unwrapped Option' end |
#unwrap! ⇒ Object
returns the inner value if present, else raises an error
95 96 97 98 99 |
# File 'lib/errgonomic/option.rb', line 95 def unwrap! raise Errgonomic::UnwrapError, 'cannot unwrap None' if none? value end |
#unwrap_or(default) ⇒ Object
returns the inner value if present, else returns the default value
116 117 118 119 120 |
# File 'lib/errgonomic/option.rb', line 116 def unwrap_or(default) return default if none? value end |
#unwrap_or_else(&block) ⇒ Object
returns the inner value if present, else returns the result of the provided block
137 138 139 140 141 |
# File 'lib/errgonomic/option.rb', line 137 def unwrap_or_else(&block) return block.call if none? value end |
#zip(other) ⇒ Object
Zips self with another Option.
If self is Some(s) and other is Some(o), this method returns Some([s, o]). Otherwise, None is returned.
304 305 306 307 308 |
# File 'lib/errgonomic/option.rb', line 304 def zip(other) return None() unless some? && other.some? Some([value, other.value]) end |
#zip_with(other, &block) ⇒ Object
Zip two options using the block passed. If self is Some and Other is some, yield both of their values to the block and return its value as Some. Else return None.
318 319 320 321 322 323 |
# File 'lib/errgonomic/option.rb', line 318 def zip_with(other, &block) return None() unless some? && other.some? other = block.call(value, other.value) Some(other) end |