Class: Monads::Optional

Inherits:
Object
  • Object
show all
Includes:
Monad
Defined in:
lib/monads/optional.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.



9
10
11
# File 'lib/monads/optional.rb', line 9

def initialize(value)
  @value = value
end

Dynamic Method Handling

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

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/monads/optional.rb', line 7

def value
  @value
end

Class Method Details

.from_value(value) ⇒ Object



27
28
29
# File 'lib/monads/optional.rb', line 27

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

Instance Method Details

#and_then(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/monads/optional.rb', line 13

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

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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/monads/optional.rb', line 23

def respond_to_missing?(method_name, include_private = false)
  super || value.nil? || value.respond_to?(method_name, include_private)
end