Module: Naught::Conversions
- Defined in:
- lib/naught/conversions.rb
Overview
Helper conversion API available on generated null classes
This module is designed to be configured per null class via Conversions.configure. Each generated null class gets its own configured version of these conversion functions.
Class Method Summary collapse
-
.configure(mod, null_class:, null_equivs:) ⇒ void
private
Configure a Conversions module for a specific null class.
Instance Method Summary collapse
-
#Actual(object = nil) ⇒ Object?
Return
nilfor null objects, otherwise return the value. -
#Just(object = nil) ⇒ Object
Return the value if not null-equivalent, otherwise raise.
-
#Maybe(object = nil) ⇒ Object
Return a null object for null-equivalent values, otherwise the value.
-
#Null(object = NOTHING_PASSED) ⇒ Object
Return a null object for
objectif it is null-equivalent.
Class Method Details
.configure(mod, null_class:, null_equivs:) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Configure a Conversions module for a specific null class
22 23 24 25 26 |
# File 'lib/naught/conversions.rb', line 22 def configure(mod, null_class:, null_equivs:) mod.define_method(:__null_class__) { null_class } mod.define_method(:__null_equivs__) { null_equivs } mod.send(:private, :__null_class__, :__null_equivs__) end |
Instance Method Details
#Actual(object = nil) ⇒ Object?
Return nil for null objects, otherwise return the value
92 93 94 95 |
# File 'lib/naught/conversions.rb', line 92 def Actual(object = nil) object = yield if block_given? null_object?(object) ? nil : object end |
#Just(object = nil) ⇒ Object
Return the value if not null-equivalent, otherwise raise
74 75 76 77 78 79 80 81 |
# File 'lib/naught/conversions.rb', line 74 def Just(object = nil) object = yield if block_given? if null_object?(object) || null_equivalent?(object) raise ArgumentError, "Just() requires a non-null value, got: #{object.inspect}" end object end |
#Maybe(object = nil) ⇒ Object
Return a null object for null-equivalent values, otherwise the value
56 57 58 59 60 61 62 |
# File 'lib/naught/conversions.rb', line 56 def Maybe(object = nil) object = yield if block_given? return object if null_object?(object) return make_null(1) if null_equivalent?(object) object end |
#Null(object = NOTHING_PASSED) ⇒ Object
Return a null object for object if it is null-equivalent
39 40 41 42 43 44 45 |
# File 'lib/naught/conversions.rb', line 39 def Null(object = NOTHING_PASSED) return object if null_object?(object) return make_null(1) if null_equivalent?(object, include_nothing: true) raise ArgumentError, "Null() requires a null-equivalent value, " \ "got #{object.class}: #{object.inspect}" end |