Class: Delegator

Inherits:
Object
  • Object
show all
Defined in:
lib/delegate.rb

Overview

Delegator is an abstract class used to build delegator pattern objects from subclasses. Subclasses should redefine _getobj_. For a concrete implementation, see SimpleDelegator.

Direct Known Subclasses

SimpleDelegator

Constant Summary collapse

IgnoreBacktracePat =
%r"\A#{Regexp.quote(__FILE__)}:\d+:in `"

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Delegator Also known as: initialize_methods

Pass in the obj to delegate method calls to. All methods supported by obj will be delegated to.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/delegate.rb', line 124

def initialize(obj)
  preserved = ::Kernel.public_instance_methods(false)
  preserved -= ["to_s","to_a","inspect","==","=~","==="]
  for t in self.class.ancestors
    preserved |= t.public_instance_methods(false)
    preserved |= t.private_instance_methods(false)
    preserved |= t.protected_instance_methods(false)
    break if t == Delegator
  end
  preserved << "singleton_method_added"
  for method in obj.methods
    next if preserved.include? method
    begin
	eval <<-EOS, nil, __FILE__, __LINE__+1
 def self.#{method}(*args, &block)
   begin
     __getobj__.__send__(:#{method}, *args, &block)
   ensure
     [email protected]_if{|s|IgnoreBacktracePat=~s} if $@
   end
 end
	EOS
    rescue SyntaxError
      raise NameError, "invalid identifier %s" % method, caller(4)
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Handles the magic of delegation through _getobj_.



154
155
156
157
158
159
160
# File 'lib/delegate.rb', line 154

def method_missing(m, *args, &block)
  target = self.__getobj__
  unless target.respond_to?(m)
    super(m, *args, &block)
  end
  target.__send__(m, *args, &block)
end

Instance Method Details

#__getobj__Object

This method must be overridden by subclasses and should return the object method calls are being delegated to.

Raises:

  • (NotImplementedError)


175
176
177
# File 'lib/delegate.rb', line 175

def __getobj__
  raise NotImplementedError, "need to define `__getobj__'"
end

#marshal_dumpObject

Serialization support for the object returned by _getobj_.



180
181
182
# File 'lib/delegate.rb', line 180

def marshal_dump
  __getobj__
end

#marshal_load(obj) ⇒ Object

Reinitializes delegation from a serialized object.



184
185
186
187
# File 'lib/delegate.rb', line 184

def marshal_load(obj)
  initialize_methods(obj)
  __setobj__(obj)
end

#respond_to?(m, include_private = false) ⇒ Boolean

Checks for a method provided by this the delegate object by fowarding the call through _getobj_.

Returns:

  • (Boolean)


166
167
168
169
# File 'lib/delegate.rb', line 166

def respond_to?(m, include_private = false)
  return true if super
  return self.__getobj__.respond_to?(m, include_private)
end