Class: Philiprehberger::Maybe::None

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

Overview

Represents an absent value

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Returns equality check.

Returns:

  • (Boolean)

    equality check



438
439
440
# File 'lib/philiprehberger/maybe.rb', line 438

def ==(other)
  other.is_a?(None)
end

#[](_key) ⇒ None

Access a single key (shorthand for dig)

Parameters:

  • _key (Object)

    the key to access

Returns:



362
363
364
# File 'lib/philiprehberger/maybe.rb', line 362

def [](_key)
  self
end

#contains?(_value) ⇒ Boolean

None never contains anything

Parameters:

  • _value (Object)

    ignored

Returns:

  • (Boolean)

    false



414
415
416
# File 'lib/philiprehberger/maybe.rb', line 414

def contains?(_value)
  false
end

#deconstruct_keys(_keys) ⇒ Hash

Pattern matching support

Parameters:

  • keys (Array<Symbol>, nil)

    the keys to deconstruct

Returns:

  • (Hash)

    the deconstructed hash



370
371
372
# File 'lib/philiprehberger/maybe.rb', line 370

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

#digNone

Dig always returns None

Returns:



354
355
356
# File 'lib/philiprehberger/maybe.rb', line 354

def dig(*)
  self
end

#each(&block) ⇒ Enumerator, self

Yield nothing for Enumerable support

Returns:

  • (Enumerator, self)


290
291
292
293
294
# File 'lib/philiprehberger/maybe.rb', line 290

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

  self
end

#filterNone

No-op filter

Returns:



328
329
330
# File 'lib/philiprehberger/maybe.rb', line 328

def filter
  self
end

#flat_mapNone

No-op transformation

Returns:



321
322
323
# File 'lib/philiprehberger/maybe.rb', line 321

def flat_map
  self
end

#flattenNone

Flatten a None — returns self

Returns:



406
407
408
# File 'lib/philiprehberger/maybe.rb', line 406

def flatten
  self
end

#fold(some:, none:) ⇒ Object

Explicit case handling: invokes the ‘none:` proc with no arguments.

Parameters:

  • some (#call)

    called with the wrapped value (ignored for None)

  • none (#call)

    called with no arguments

Returns:

  • (Object)

    the result of ‘none.call`

Raises:

  • (ArgumentError)

    if either keyword is missing



431
432
433
434
435
# File 'lib/philiprehberger/maybe.rb', line 431

def fold(some:, none:)
  raise ArgumentError, 'some: and none: are required' if some.nil? || none.nil?

  none.call
end

#inspectString Also known as: to_s

Returns string representation.

Returns:

  • (String)

    string representation



443
444
445
# File 'lib/philiprehberger/maybe.rb', line 443

def inspect
  'None'
end

#mapNone

No-op transformation

Returns:



314
315
316
# File 'lib/philiprehberger/maybe.rb', line 314

def map
  self
end

#none?Boolean

Returns true.

Returns:

  • (Boolean)

    true



307
308
309
# File 'lib/philiprehberger/maybe.rb', line 307

def none?
  true
end

#or_else(default = nil) { ... } ⇒ Some, None

Return the default value

Parameters:

  • default (Object) (defaults to: nil)

    the default value

Yields:

  • optional block providing default

Returns:

  • (Some, None)

    the default wrapped in Maybe



337
338
339
340
# File 'lib/philiprehberger/maybe.rb', line 337

def or_else(default = nil, &block)
  value = block ? block.call : default
  Maybe.wrap(value)
end

#or_raise(error_class = Error, message = 'value is absent') ⇒ Object

Raise an error since value is absent

Parameters:

  • error_class (Class) (defaults to: Error)

    the error class to raise

  • message (String) (defaults to: 'value is absent')

    the error message

Raises:

  • (Error)

    always raises



347
348
349
# File 'lib/philiprehberger/maybe.rb', line 347

def or_raise(error_class = Error, message = 'value is absent')
  raise error_class, message
end

#present?Boolean

Alias for #some? — matches Rails-style naming

Returns:

  • (Boolean)

    false



421
422
423
# File 'lib/philiprehberger/maybe.rb', line 421

def present?
  false
end

#recover { ... } ⇒ Some, None

Convert None to Some via a block

Yields:

  • the block providing a recovery value

Returns:



378
379
380
# File 'lib/philiprehberger/maybe.rb', line 378

def recover(&block)
  Maybe.wrap(block.call)
end

#rejectNone

No-op reject, returns self

Returns:



399
400
401
# File 'lib/philiprehberger/maybe.rb', line 399

def reject
  self
end

#some?Boolean

Returns false.

Returns:

  • (Boolean)

    false



302
303
304
# File 'lib/philiprehberger/maybe.rb', line 302

def some?
  false
end

#tapNone

No-op tap, returns self

Returns:



392
393
394
# File 'lib/philiprehberger/maybe.rb', line 392

def tap
  self
end

#valuenil

Returns always nil.

Returns:

  • (nil)

    always nil



297
298
299
# File 'lib/philiprehberger/maybe.rb', line 297

def value
  nil
end

#zipNone

Combine with other Maybes — always returns None

Returns:



385
386
387
# File 'lib/philiprehberger/maybe.rb', line 385

def zip(*)
  self
end