Class: Ronad::Monad Abstract
- Inherits:
-
Object
- Object
- Ronad::Monad
- Defined in:
- lib/ronad/monad.rb
Overview
This class provides a common interface.
Direct Known Subclasses
Default, Eventually, Just, Many, Maybe
Instance Method Summary collapse
-
#continue ⇒ Monad
“Fails” a maybe given a certain condition.
-
#initialize(value) ⇒ Monad
constructor
A new instance of Monad.
-
#monad_value ⇒ Object
Extract the value from the monad.
-
#value ⇒ Object
Extract the value from the monad.
-
#~@ ⇒ Object
Alias for #monad_value.
Constructor Details
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Ronad::Monad (private)
Provides convinience for chaining methods together while maintaining a monad.
102 103 104 105 106 107 108 109 110 |
# File 'lib/ronad/monad.rb', line 102 def method_missing(method, *args, &block) unwrapped_args = args.map do |arg| case arg when Monad then arg.value || arg else arg end end and_then { |value| value.public_send(method, *unwrapped_args, &block) } end |
Instance Method Details
#continue ⇒ Monad
“Fails” a maybe given a certain condition. If the condition is ‘false` then Maybe(nil) will be returned. Useful for performing side-effects given a certain condition
Maybe([]).and_then do |value|
value.each(&:some_operation)
OtherModule.finalize! # Side Effect should only happen if there
end # are any values
The following ‘and_then` only executes if `value` responds truthy to any?
Maybe([]).continue(&:any?).and_then do |value|
value.each(&:some_operation)
OtherModule.finalize!
end
79 80 81 82 83 |
# File 'lib/ronad/monad.rb', line 79 def continue and_then do |value| value if yield value end end |
#monad_value ⇒ Object
Useful when you want to extract the underlying value and it also responds to #value
Extract the value from the monad. Can also be extracted via ‘~` unary operator.
46 47 48 49 50 51 52 |
# File 'lib/ronad/monad.rb', line 46 def monad_value if Monad === @value @value.value else @value end end |
#value ⇒ Object
Extract the value from the monad. If the underlying value responds to #value then this will be bypassed instead.
27 28 29 30 31 32 33 |
# File 'lib/ronad/monad.rb', line 27 def value if !(Monad === @value) && @value.respond_to?(:value) and_then{ |v| v.value } else monad_value end end |
#~@ ⇒ Object
Alias for #monad_value
58 59 60 |
# File 'lib/ronad/monad.rb', line 58 def ~@ monad_value end |