Class: Muina::Maybe::Some

Inherits:
Muina::Maybe show all
Defined in:
lib/muina/maybe/some.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

Elem =
type_member { { upper: Object } }
ElemT =
type_template { { upper: Object } }

Constants inherited from Muina::Maybe

UnwrappingError

Instance Method Summary collapse

Methods inherited from Muina::Maybe

none, return

Constructor Details

#initialize(value) ⇒ Some

rubocop:disable Lint/MissingSuper



19
20
21
22
# File 'lib/muina/maybe/some.rb', line 19

def initialize(value) # rubocop:disable Lint/MissingSuper
  @value = value
  freeze
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if both instances are of the same variant, and the contained values are equal in the case of Muina::Maybe::Some.

Parameters:

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/muina/maybe/some.rb', line 127

def ==(other)
  self.class == other.class &&
    value! == other.value!
end

#and_then {|value| ... } ⇒ self

Runs the provided block only if instance is of the Muina::Maybe::Some variant, yielding the contained value. Always returns self.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Returns:

  • (self)


70
71
72
73
# File 'lib/muina/maybe/some.rb', line 70

def and_then(&_blk)
  yield(@value)
  self
end

#bind {|value| ... } ⇒ Maybe

If instance is of the Muina::Maybe::Some variant, it yields the contained value to the block and it returns the return value of the block; if it is of the None variant, it returns itself.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Yield Returns:

Returns:



111
112
113
# File 'lib/muina/maybe/some.rb', line 111

def bind(&_blk)
  yield(@value)
end

#bind_none { ... } ⇒ Maybe

If instance is of the None variant, it runs the provided block and it returns its return value; if it is of the Muina::Maybe::Some variant, it returns itself.

Yields:

Yield Returns:

Returns:



121
122
123
# File 'lib/muina/maybe/some.rb', line 121

def bind_none(&_blk)
  self
end

#map {|value| ... } ⇒ Maybe<yield>, None

If instance is of the Muina::Maybe::Some variant, it passes the contained value to the block and returns a new Muina::Maybe::Some instance containing the return value of the block; if instance is of the None variant, it returns itself.

Yield Parameters:

  • value (Elem)

    the contained value is passed to the block

Yield Returns:

  • (Object)

Returns:



89
90
91
# File 'lib/muina/maybe/some.rb', line 89

def map(&_blk)
  Maybe.return yield(@value)
end

#map_none { ... } ⇒ Maybe<yield>, self

If instance is of the Muina::Maybe::Some variant, it returns itself; if instance is of the None variant it runs the block and returns a new Muina::Maybe::Some instance containing the return value of the block.

Yields:

Yield Returns:

  • (Object)

Returns:



99
100
101
# File 'lib/muina/maybe/some.rb', line 99

def map_none(&_blk)
  self
end

#none?true, false

Returns true if instance is of the None variant, or false if it is of the Muina::Maybe::Some variant.

Returns:



32
33
34
# File 'lib/muina/maybe/some.rb', line 32

def none?
  false
end

#or_else { ... } ⇒ self

Runs the provided block only if instance is of the None variant, yielding no value to the block. Always returns self.

Yields:

Returns:

  • (self)


77
78
79
# File 'lib/muina/maybe/some.rb', line 77

def or_else(&_blk)
  self
end

#some?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/muina/maybe/some.rb', line 26

def some?
  true
end

#value!Elem

Returns the contained value if instance is of the Muina::Maybe::Some variant, or raises UnwrappingError if it is of the None variant.

Returns:

Raises:



38
39
40
# File 'lib/muina/maybe/some.rb', line 38

def value!
  @value
end

#value_or(_default) ⇒ Object



48
49
50
# File 'lib/muina/maybe/some.rb', line 48

def value_or(_default)
  @value
end

#value_or_nilElem?

Returns the contained value if instance is of the Muina::Maybe::Some variant, or nil if it is of the None variant.

Returns:



64
65
66
# File 'lib/muina/maybe/some.rb', line 64

def value_or_nil
  @value
end

#value_or_yield(&_blk) ⇒ Elem, yield

Returns the contained value if instance is of the Muina::Maybe::Some variant, or runs the provided block and returns its result if it is of the None variant.

Yield Returns:

  • (Object)

Returns:



58
59
60
# File 'lib/muina/maybe/some.rb', line 58

def value_or_yield(&_blk)
  @value
end