Class: Philiprehberger::Maybe::Some

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/philiprehberger/maybe.rb

Overview

Container for a present value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value) ⇒ Some

Returns a new instance of Some.

Parameters:

  • value (Object)

    the wrapped value



43
44
45
# File 'lib/philiprehberger/maybe.rb', line 43

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the wrapped value.

Returns:

  • (Object)

    the wrapped value



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns equality check.

Returns:

  • (Boolean)

    equality check



143
144
145
# File 'lib/philiprehberger/maybe.rb', line 143

def ==(other)
  other.is_a?(Some) && other.value == @value
end

#[](key) ⇒ Some, None

Access a single key (shorthand for dig)

Parameters:

  • key (Object)

    the key to access

Returns:

  • (Some, None)

    the result of digging with a single key



156
157
158
# File 'lib/philiprehberger/maybe.rb', line 156

def [](key)
  dig(key)
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support

Parameters:

  • keys (Array<Symbol>, nil)

    the keys to deconstruct

Returns:

  • (Hash)

    the deconstructed hash



138
139
140
# File 'lib/philiprehberger/maybe.rb', line 138

def deconstruct_keys(_keys)
  { value: @value, some: true, none: false }
end

#dig(*keys) ⇒ Some, None

Dig into nested structures

Parameters:

  • keys (Array<Object>)

    keys to dig through

Returns:

  • (Some, None)

    the result of digging



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/philiprehberger/maybe.rb', line 120

def dig(*keys)
  result = @value
  keys.each do |key|
    result = case result
             when Hash then result[key]
             when Array then result[key]
             else
               return None.instance
             end
    return None.instance if result.nil?
  end
  Maybe.wrap(result)
end

#each {|value| ... } ⇒ Enumerator, self

Yield the wrapped value for Enumerable support

Yields:

  • (value)

    the wrapped value

Returns:

  • (Enumerator, self)


54
55
56
57
58
59
# File 'lib/philiprehberger/maybe.rb', line 54

def each(&block)
  return to_enum(:each) unless block

  block.call(@value)
  self
end

#filter {|value| ... } ⇒ Some, None

Filter the value based on a predicate

Yields:

  • (value)

    the predicate block

Returns:

  • (Some, None)

    Some if predicate is true, None otherwise



94
95
96
97
98
99
100
# File 'lib/philiprehberger/maybe.rb', line 94

def filter(&block)
  if block.call(@value)
    self
  else
    None.instance
  end
end

#flat_map {|value| ... } ⇒ Some, None

Transform the wrapped value, expecting a Maybe return

Yields:

  • (value)

    the transformation block returning a Maybe

Returns:

Raises:



83
84
85
86
87
88
# File 'lib/philiprehberger/maybe.rb', line 83

def flat_map(&block)
  result = block.call(@value)
  raise Error, 'flat_map block must return a Maybe' unless result.is_a?(Some) || result.is_a?(None)

  result
end

#inspectString Also known as: to_s

Returns string representation.

Returns:

  • (String)

    string representation



148
149
150
# File 'lib/philiprehberger/maybe.rb', line 148

def inspect
  "Some(#{@value.inspect})"
end

#map {|value| ... } ⇒ Some, None

Transform the wrapped value

Yields:

  • (value)

    the transformation block

Returns:

  • (Some, None)

    the transformed Maybe



75
76
77
# File 'lib/philiprehberger/maybe.rb', line 75

def map(&block)
  Maybe.wrap(block.call(@value))
end

#none?Boolean

Returns false.

Returns:

  • (Boolean)

    false



67
68
69
# File 'lib/philiprehberger/maybe.rb', line 67

def none?
  false
end

#or_else(_default = nil) ⇒ Some

Return self since value is present

Returns:



105
106
107
# File 'lib/philiprehberger/maybe.rb', line 105

def or_else(_default = nil)
  self
end

#or_raise(_error_class = Error, _message = 'value is absent') ⇒ Object

Return the wrapped value since it is present

Returns:

  • (Object)

    the wrapped value



112
113
114
# File 'lib/philiprehberger/maybe.rb', line 112

def or_raise(_error_class = Error, _message = 'value is absent')
  @value
end

#some?Boolean

Returns true.

Returns:

  • (Boolean)

    true



62
63
64
# File 'lib/philiprehberger/maybe.rb', line 62

def some?
  true
end

#tap {|value| ... } ⇒ Some

Execute a block for side effects, returning self unchanged

Yields:

  • (value)

    the block to execute

Returns:



178
179
180
181
# File 'lib/philiprehberger/maybe.rb', line 178

def tap(&block)
  block.call(@value)
  self
end

#zip(*others) ⇒ Some, None

Combine multiple Maybes into a single Maybe of an array

Parameters:

  • others (Array<Some, None>)

    other Maybe values

Returns:

  • (Some, None)

    Some with array of values if all are Some, None otherwise



164
165
166
167
168
169
170
171
172
# File 'lib/philiprehberger/maybe.rb', line 164

def zip(*others)
  values = [@value]
  others.each do |other|
    return None.instance if other.none?

    values << other.value
  end
  Some.new(values)
end