Class: DumbDelegator

Inherits:
BasicObject
Defined in:
lib/dumb_delegator.rb,
lib/dumb_delegator/version.rb

Constant Summary collapse

VERSION =
'0.7.0'

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ DumbDelegator

Returns a new instance of DumbDelegator.



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

def initialize(target)
  __setobj__(target)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/dumb_delegator.rb', line 23

def method_missing(method, *args, &block)
  if @__dumb_target__.respond_to?(method)
    @__dumb_target__.__send__(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#__getobj__Object



31
32
33
# File 'lib/dumb_delegator.rb', line 31

def __getobj__
  @__dumb_target__
end

#__setobj__(obj) ⇒ Object

Raises:

  • (::ArgumentError)


35
36
37
38
# File 'lib/dumb_delegator.rb', line 35

def __setobj__(obj)
  raise ::ArgumentError, 'Delegation to self is not allowed.' if obj.__id__ == __id__
  @__dumb_target__ = obj
end

#marshal_dumpObject



40
41
42
43
44
45
# File 'lib/dumb_delegator.rb', line 40

def marshal_dump
  [
    :__v1__,
    __getobj__
  ]
end

#marshal_load(data) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/dumb_delegator.rb', line 47

def marshal_load(data)
  version, obj = data
  case version
  when :__v1__
    __setobj__(obj)
  end
end

#respond_to?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method, include_all=false)
  __getobj__.respond_to?(method) || super
end