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



423
424
425
# File 'lib/philiprehberger/maybe.rb', line 423

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

#[](_key) ⇒ None

Access a single key (shorthand for dig)

Parameters:

  • _key (Object)

    the key to access

Returns:



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

def [](_key)
  self
end

#contains?(_value) ⇒ Boolean

None never contains anything

Parameters:

  • _value (Object)

    ignored

Returns:

  • (Boolean)

    false



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

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



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

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

#digNone

Dig always returns None

Returns:



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

def dig(*)
  self
end

#each(&block) ⇒ Enumerator, self

Yield nothing for Enumerable support

Returns:

  • (Enumerator, self)


275
276
277
278
279
# File 'lib/philiprehberger/maybe.rb', line 275

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

  self
end

#filterNone

No-op filter

Returns:



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

def filter
  self
end

#flat_mapNone

No-op transformation

Returns:



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

def flat_map
  self
end

#flattenNone

Flatten a None — returns self

Returns:



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

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



416
417
418
419
420
# File 'lib/philiprehberger/maybe.rb', line 416

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



428
429
430
# File 'lib/philiprehberger/maybe.rb', line 428

def inspect
  'None'
end

#mapNone

No-op transformation

Returns:



299
300
301
# File 'lib/philiprehberger/maybe.rb', line 299

def map
  self
end

#none?Boolean

Returns true.

Returns:

  • (Boolean)

    true



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

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



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

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



332
333
334
# File 'lib/philiprehberger/maybe.rb', line 332

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



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

def present?
  false
end

#recover { ... } ⇒ Some, None

Convert None to Some via a block

Yields:

  • the block providing a recovery value

Returns:



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

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

#rejectNone

No-op reject, returns self

Returns:



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

def reject
  self
end

#some?Boolean

Returns false.

Returns:

  • (Boolean)

    false



287
288
289
# File 'lib/philiprehberger/maybe.rb', line 287

def some?
  false
end

#tapNone

No-op tap, returns self

Returns:



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

def tap
  self
end

#valuenil

Returns always nil.

Returns:

  • (nil)

    always nil



282
283
284
# File 'lib/philiprehberger/maybe.rb', line 282

def value
  nil
end

#zipNone

Combine with other Maybes — always returns None

Returns:



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

def zip(*)
  self
end