Class: DumbDelegator

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

Constant Summary collapse

VERSION =
'0.8.0'

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ DumbDelegator

Returns a new instance of DumbDelegator.



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

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



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

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



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

def __getobj__
  @__dumb_target__
end

#__setobj__(obj) ⇒ Object

Raises:

  • (::ArgumentError)


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

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

#marshal_dumpObject



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

def marshal_dump
  [
    :__v1__,
    __getobj__
  ]
end

#marshal_load(data) ⇒ Object



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

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

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

Returns:

  • (Boolean)


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

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