Class: Mooset::Optional

Inherits:
Object
  • Object
show all
Includes:
Monad
Defined in:
lib/mooset/monads.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monad

#method_missing, #within

Constructor Details

#initialize(value) ⇒ Optional

Returns a new instance of Optional.



38
39
40
# File 'lib/mooset/monads.rb', line 38

def initialize(value)
  @value = value
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Mooset::Monad

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



37
38
39
# File 'lib/mooset/monads.rb', line 37

def value
  @value
end

Class Method Details

.from_value(value) ⇒ Object



52
53
54
# File 'lib/mooset/monads.rb', line 52

def self.from_value(value)
  Optional.new(value)
end

Instance Method Details

#and_then(&block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/mooset/monads.rb', line 42

def and_then(&block)
  block = ensure_monadic_result(&block)

  if value.nil?
    self
  else
    block.call(value)
  end
end