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



70
71
72
# File 'lib/philiprehberger/maybe.rb', line 70

def initialize(value)
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Returns the wrapped value.

Returns:

  • (Object)

    the wrapped value



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Returns equality check.

Returns:

  • (Boolean)

    equality check



170
171
172
# File 'lib/philiprehberger/maybe.rb', line 170

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



183
184
185
# File 'lib/philiprehberger/maybe.rb', line 183

def [](key)
  dig(key)
end

#contains?(value) ⇒ Boolean

Check whether the wrapped value equals the given value

Parameters:

  • value (Object)

    the value to compare against

Returns:

  • (Boolean)

    true if the wrapped value equals the argument



238
239
240
# File 'lib/philiprehberger/maybe.rb', line 238

def contains?(value)
  @value == value
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support

Parameters:

  • keys (Array<Symbol>, nil)

    the keys to deconstruct

Returns:

  • (Hash)

    the deconstructed hash



165
166
167
# File 'lib/philiprehberger/maybe.rb', line 165

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/philiprehberger/maybe.rb', line 147

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)


81
82
83
84
85
86
# File 'lib/philiprehberger/maybe.rb', line 81

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



121
122
123
124
125
126
127
# File 'lib/philiprehberger/maybe.rb', line 121

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:



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

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

#flattenSome, None

Flatten a single level of Maybe nesting

Some(Some(x)) becomes Some(x); Some(None) becomes None; Some(x) is unchanged.

Returns:



227
228
229
230
231
232
# File 'lib/philiprehberger/maybe.rb', line 227

def flatten
  case @value
  when Some, None then @value
  else self
  end
end

#inspectString Also known as: to_s

Returns string representation.

Returns:

  • (String)

    string representation



175
176
177
# File 'lib/philiprehberger/maybe.rb', line 175

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



102
103
104
# File 'lib/philiprehberger/maybe.rb', line 102

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

#none?Boolean

Returns false.

Returns:

  • (Boolean)

    false



94
95
96
# File 'lib/philiprehberger/maybe.rb', line 94

def none?
  false
end

#or_else(_default = nil) ⇒ Some

Return self since value is present

Returns:



132
133
134
# File 'lib/philiprehberger/maybe.rb', line 132

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



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

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

#present?Boolean

Alias for #some? — matches Rails-style naming

Returns:

  • (Boolean)

    true



245
246
247
# File 'lib/philiprehberger/maybe.rb', line 245

def present?
  true
end

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

Inverse of #filter — return None when the predicate is truthy

Yields:

  • (value)

    the predicate block

Returns:

  • (Some, None)

    None if predicate is true, self otherwise



214
215
216
217
218
219
220
# File 'lib/philiprehberger/maybe.rb', line 214

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

#some?Boolean

Returns true.

Returns:

  • (Boolean)

    true



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

def some?
  true
end

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

Execute a block for side effects, returning self unchanged

Yields:

  • (value)

    the block to execute

Returns:



205
206
207
208
# File 'lib/philiprehberger/maybe.rb', line 205

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



191
192
193
194
195
196
197
198
199
# File 'lib/philiprehberger/maybe.rb', line 191

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

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