Class: Danom::Monad Abstract
- Inherits:
-
Object
- Object
- Danom::Monad
- Defined in:
- lib/danom/monad.rb
Overview
This class is abstract.
This class provides a common interface.
Instance Method Summary collapse
-
#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) ⇒ Danom::Monad (private)
Provides convinience for chaining methods together while maintaining a monad.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/danom/monad.rb', line 79 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) } rescue TypeError self.class.new(nil) end |
Instance Method Details
#monad_value ⇒ Object
Note:
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.
38 39 40 41 42 43 44 |
# File 'lib/danom/monad.rb', line 38 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.
19 20 21 22 23 24 25 |
# File 'lib/danom/monad.rb', line 19 def value if !(Monad === @value) && @value.respond_to?(:value) and_then{ |v| v.value } else monad_value end end |
#~@ ⇒ Object
Alias for #monad_value
49 50 51 |
# File 'lib/danom/monad.rb', line 49 def ~@ monad_value end |