Class: NilCheckDelegate

Inherits:
Delegator
  • Object
show all
Defined in:
lib/gorillib/nil_check_delegate.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ NilCheckDelegate

Returns a new instance of NilCheckDelegate.



4
5
6
7
8
# File 'lib/gorillib/nil_check_delegate.rb', line 4

def initialize(obj)
  super
  @delegate_sd_obj = obj
  @depth = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &blk) ⇒ Object



19
20
21
# File 'lib/gorillib/nil_check_delegate.rb', line 19

def method_missing m, *args, &blk
  _wrap(__getobj__.nil? ? nil : super(m, *args, &blk))
end

Instance Method Details

#__getobj__Object



23
24
25
# File 'lib/gorillib/nil_check_delegate.rb', line 23

def __getobj__
  @delegate_sd_obj
end

#__setobj__(obj) ⇒ Object



27
28
29
# File 'lib/gorillib/nil_check_delegate.rb', line 27

def __setobj__(obj)
  @delegate_sd_obj = obj
end

#_wrap(obj) ⇒ Object



15
16
17
# File 'lib/gorillib/nil_check_delegate.rb', line 15

def _wrap(obj)
  ((@depth -= 1) >= 0) ? self.class.new(obj) : obj
end

#nilcheck_depth(depth = 0) ⇒ Object



10
11
12
13
# File 'lib/gorillib/nil_check_delegate.rb', line 10

def nilcheck_depth(depth = 0)
  @depth = depth
  self
end