Class: Nilable
- Inherits:
- BasicObject
- Defined in:
- lib/nilable.rb,
lib/nilable/version.rb
Overview
Nilable object is a tool to handle nil invocations.
Any Nilable object wraps a single value object and proxy method invocations to it. In turn, every method result is wrapped in an Nilable object.
That way, if somewhere along the call chain, method result is nil, no NoMethodError will be raised. It acts as a black hole object.
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Attribute Summary collapse
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(object) ⇒ Nilable
constructor
A new instance of Nilable.
- #method_missing(name, *args, &block) ⇒ Object
Constructor Details
#initialize(object) ⇒ Nilable
Returns a new instance of Nilable.
14 15 16 |
# File 'lib/nilable.rb', line 14 def initialize(object) @value = object.is_a?(::Nilable) ? object.value : object end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/nilable.rb', line 18 def method_missing(name, *args, &block) if value ::Nilable.new(value.public_send(name, *args, &block)) else self end end |
Instance Attribute Details
#value ⇒ Object (readonly)
Returns the value of attribute value.
12 13 14 |
# File 'lib/nilable.rb', line 12 def value @value end |