Class: NullChainer
- Inherits:
- BasicObject
- Defined in:
- lib/nullobject_chain.rb
Class Attribute Summary collapse
-
.use_nullobject ⇒ Object
Returns the value of attribute use_nullobject.
Instance Method Summary collapse
- #get ⇒ Object
-
#initialize(obj) ⇒ NullChainer
constructor
A new instance of NullChainer.
- #method_missing(name, *args, &block) ⇒ Object
Constructor Details
#initialize(obj) ⇒ NullChainer
Returns a new instance of NullChainer.
7 8 9 |
# File 'lib/nullobject_chain.rb', line 7 def initialize(obj) @obj = obj end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/nullobject_chain.rb', line 19 def method_missing(name, *args, &block) begin ::NullChainer.new(@obj.send(name, *args, &block)) rescue ::NoMethodError if @obj.nil? ::NullChainer.new(nil) else raise $! end end end |
Class Attribute Details
.use_nullobject ⇒ Object
Returns the value of attribute use_nullobject.
3 4 5 |
# File 'lib/nullobject_chain.rb', line 3 def use_nullobject @use_nullobject end |
Instance Method Details
#get ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/nullobject_chain.rb', line 11 def get if @obj.is_a?(::NilClass) && ::NullChainer.use_nullobject ::Null::Object.instance else @obj end end |