Class: NullChainer

Inherits:
BasicObject
Defined in:
lib/nullobject_chain.rb

Class Attribute Summary collapse

Instance Method Summary collapse

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_nullobjectObject

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

#getObject



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