Class: Monads::Many

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Monad

#method_missing, #within

Constructor Details

#initialize(values) ⇒ Many

Returns a new instance of Many.



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

def initialize(values)
  @values = values
end

Dynamic Method Handling

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

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



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

def values
  @values
end

Class Method Details

.from_value(value) ⇒ Object



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

def self.from_value(value)
  Many.new([value])
end

Instance Method Details

#and_then(&block) ⇒ Object



13
14
15
16
17
# File 'lib/monads/many.rb', line 13

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

  Many.new(values.map(&block).flat_map(&:values))
end

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

Returns:

  • (Boolean)


19
20
21
# File 'lib/monads/many.rb', line 19

def respond_to_missing?(method_name, include_private = false)
  super || values.all? { |value| value.respond_to?(method_name, include_private) }
end