Class: MonadOxide::Result
- Inherits:
-
Object
- Object
- MonadOxide::Result
- Defined in:
- lib/result.rb
Overview
A Result is a chainable series of sequential transformations. The Result structure contains an ‘Ok<A> | Err<Exception>`. This is the central location for documentation between both `Ok` and `Err`. It is best to think of any given `Ok` or `Err` as a `Result` instead. All methods on `Ok` are present on `Err` and vice versa. This way we can interchange one for the other during virtually any call.
This is a base-class only, and you should never see instances of these in the wild.
Instance Method Summary collapse
-
#and_then(f = nil) { ... } ⇒ Ok<B> | Err<C>
For ‘Ok’, invokes ‘f’ or the block with the data and returns the Result returned from that.
-
#err? ⇒ Boolean
Determine if this is a MonadOxide::Err.
-
#flatten ⇒ Result
Un-nest this ‘Result’.
-
#initialize(data) ⇒ Result
constructor
None of these work.
-
#inspect_err(f = nil) { ... } ⇒ Result<A>
In the case of ‘Err’, applies ‘f’ or the block over the ‘Exception’ and returns the same ‘Err’.
-
#inspect_ok(f = nil) { ... } ⇒ Result<A>
In the case of ‘Ok’, applies ‘f’ or the block over the data and returns the same ‘Ok’.
-
#map(f = nil) { ... } ⇒ Result<B>
In the case of ‘Ok’, applies ‘f’ or the block over the data and returns a new new ‘Ok’ with the returned value.
-
#map_err(f = nil) { ... } ⇒ Result<B>
In the case of ‘Err’, applies ‘f’ or the block over the ‘Exception’ and returns a new new ‘Err’ with the returned value.
-
#match(matcher) ⇒ R
Use pattern matching to work with both Ok and Err variants.
-
#ok? ⇒ Boolean
Determine if this is a MonadOxide::Ok.
-
#or_else(f = nil) { ... } ⇒ Ok<B> | Err<C>
For ‘Err’, invokes ‘f’ or the block with the data and returns the Result returned from that.
-
#unwrap ⇒ A
Dangerously access the ‘Result’ data.
-
#unwrap_err ⇒ E
Dangerously access the ‘Result’ data.
-
#unwrap_err_or_else(_f) { ... } ⇒ T|B
Safely unwrap the ‘Result` but with lazy evaluation.
-
#unwrap_or(_) ⇒ T|B
Attempt to safely access the ‘Result` data.
-
#unwrap_or_else(_f) { ... } ⇒ T|B
Safely unwrap the ‘Result` but with lazy evaluation.
Constructor Details
#initialize(data) ⇒ Result
None of these work. Option 1: private_class_method :new Option 2: class << self
protected :new
end
55 56 57 |
# File 'lib/result.rb', line 55 def initialize(data) raise NoMethodError.new('Do not use Result directly. See Ok and Err.') end |
Instance Method Details
#and_then(f = nil) { ... } ⇒ Ok<B> | Err<C>
For ‘Ok’, invokes ‘f’ or the block with the data and returns the Result returned from that. Exceptions raised during ‘f’ or the block will return an ‘Err<Exception>’.
For ‘Err’, returns itself and the function/block are ignored.
This method is used for control flow based on ‘Result’ values.
‘and_then’ is desirable for chaining together other Result based operations, or doing transformations where flipping from an ‘Ok’ to an ‘Err’ is desired. In cases where there is little/no risk of an ‘Err’ state, @see Result#map.
The ‘Err’ equivalent operation is @see Result#or_else.
The return type is enforced.
91 92 93 |
# File 'lib/result.rb', line 91 def and_then(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#err? ⇒ Boolean
Determine if this is a MonadOxide::Err.
62 63 64 |
# File 'lib/result.rb', line 62 def err?() false end |
#flatten ⇒ Result
Un-nest this ‘Result’. This implementation is shared between ‘Ok’ and ‘Err’. In both cases, the structure’s data is operated upon. return the inner-most ‘Result’, regardless of the depth of nesting. Otherwise return ‘self’.
115 116 117 118 119 120 121 |
# File 'lib/result.rb', line 115 def flatten() if @data.kind_of?(Result) return @data.flatten() else self end end |
#inspect_err(f = nil) { ... } ⇒ Result<A>
In the case of ‘Err’, applies ‘f’ or the block over the ‘Exception’ and returns the same ‘Err’. No changes are applied. This is ideal for logging. For ‘Ok’, this method falls through. Exceptions raised during these transformations will return an ‘Err’ with the Exception.
105 106 107 |
# File 'lib/result.rb', line 105 def inspect_err(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#inspect_ok(f = nil) { ... } ⇒ Result<A>
In the case of ‘Ok’, applies ‘f’ or the block over the data and returns the same ‘Ok’. No changes are applied. This is ideal for logging. For ‘Err’, this method falls through. Exceptions raised during these transformations will return an ‘Err’ with the Exception.
133 134 135 |
# File 'lib/result.rb', line 133 def inspect_ok(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#map(f = nil) { ... } ⇒ Result<B>
In the case of ‘Ok’, applies ‘f’ or the block over the data and returns a new new ‘Ok’ with the returned value. For ‘Err’, this method falls through. Exceptions raised during these transformations will return an ‘Err’ with the Exception.
149 150 151 |
# File 'lib/result.rb', line 149 def map(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#map_err(f = nil) { ... } ⇒ Result<B>
In the case of ‘Err’, applies ‘f’ or the block over the ‘Exception’ and returns a new new ‘Err’ with the returned value. For ‘Ok’, this method falls through. Exceptions raised during these transformations will return an ‘Err’ with the Exception.
165 166 167 |
# File 'lib/result.rb', line 165 def map_err(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#match(matcher) ⇒ R
Use pattern matching to work with both Ok and Err variants. This is useful when it is desirable to have both variants handled in the same location. It can also be useful when either variant can coerced into a non-Result type.
Ruby has no built-in pattern matching, but the next best thing is a Hash using the Result classes themselves as the keys.
Tests for this are found in Ok and Err’s tests.
upon.
185 186 187 |
# File 'lib/result.rb', line 185 def match(matcher) matcher[self.class].call(@data) end |
#ok? ⇒ Boolean
Determine if this is a MonadOxide::Ok.
192 193 194 |
# File 'lib/result.rb', line 192 def ok?() false end |
#or_else(f = nil) { ... } ⇒ Ok<B> | Err<C>
For ‘Err’, invokes ‘f’ or the block with the data and returns the Result returned from that. Exceptions raised during ‘f’ or the block will return an ‘Err<Exception>’.
For ‘Ok’, returns itself and the function/block are ignored.
This method is used for control flow based on ‘Result’ values.
‘or_else’ is desirable for chaining together other Result based operations, or doing transformations where flipping from an ‘Ok’ to an ‘Err’ is desired. In cases where there is little/no risk of an ‘Err’ state, @see Result#map.
The ‘Ok’ equivalent operation is @see Result#and_then.
The return type is enforced.
221 222 223 |
# File 'lib/result.rb', line 221 def or_else(f=nil, &block) Err.new(ResultMethodNotImplementedError.new()) end |
#unwrap ⇒ A
Dangerously access the ‘Result’ data. If this is an ‘Err’, an exception will be raised. It is recommended to use this for tests only.
230 231 232 |
# File 'lib/result.rb', line 230 def unwrap() Err.new(ResultMethodNotImplementedError.new()) end |
#unwrap_err ⇒ E
Dangerously access the ‘Result’ data. If this is an ‘Ok’, an exception will be raised. It is recommended to use this for tests only.
239 240 241 |
# File 'lib/result.rb', line 239 def unwrap_err() Err.new(ResultMethodNotImplementedError.new()) end |
#unwrap_err_or_else(_f) { ... } ⇒ T|B
Safely unwrap the ‘Result` but with lazy evaluation. In the case of `Err`, this returns the wrapped value. For `Ok` the function provided is evaluated and its returned value is what is returned.
254 255 256 |
# File 'lib/result.rb', line 254 def unwrap_err_or_else(_f) raise ResultMethodNotImplementedError.new() end |
#unwrap_or(_) ⇒ T|B
Attempt to safely access the ‘Result` data. This always returns a value instead of raising an Exception. In the case of `Ok`, the data is returned. In the case of `Err`, the value provided is returned.
264 265 266 |
# File 'lib/result.rb', line 264 def unwrap_or(_) Err.new(ResultMethodNotImplementedError.new()) end |
#unwrap_or_else(_f) { ... } ⇒ T|B
Safely unwrap the ‘Result` but with lazy evaluation. In the case of `Ok`, this returns the wrapped value. For `Err` the function provided is evaluated and its returned value is what is returned.
279 280 281 |
# File 'lib/result.rb', line 279 def unwrap_or_else(_f) raise ResultMethodNotImplementedError.new() end |