Class Delegator
In: lib/delegate.rb
Parent: Object

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

Methods

External Aliases

initialize -> initialize_methods

Public Class methods

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

[Source]

     # File lib/delegate.rb, line 123
123:   def initialize(obj)
124:     preserved = ::Kernel.public_instance_methods(false)
125:     preserved -= ["to_s","to_a","inspect","==","=~","==="]
126:     for t in self.class.ancestors
127:       preserved |= t.public_instance_methods(false)
128:       preserved |= t.private_instance_methods(false)
129:       preserved |= t.protected_instance_methods(false)
130:       break if t == Delegator
131:     end
132:     preserved << "singleton_method_added"
133:     for method in obj.methods
134:       next if preserved.include? method
135:       begin
136:         eval "def self.\#{method}(*args, &block)\nbegin\n__getobj__.__send__(:\#{method}, *args, &block)\nrescue Exception\n$@.delete_if{|s| /:in `__getobj__'$/ =~ s} #`\n$@.delete_if{|s| /^\\\\(eval\\\\):/ =~ s}\nKernel::raise\nend\nend\n"
137:       rescue SyntaxError
138:         raise NameError, "invalid identifier %s" % method, caller(4)
139:       end
140:     end
141:   end

Public Instance methods

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

[Source]

     # File lib/delegate.rb, line 177
177:   def __getobj__
178:     raise NotImplementedError, "need to define `__getobj__'"
179:   end

Serialization support for the object returned by __getobj__.

[Source]

     # File lib/delegate.rb, line 182
182:   def marshal_dump
183:     __getobj__
184:   end

Reinitializes delegation from a serialized object.

[Source]

     # File lib/delegate.rb, line 186
186:   def marshal_load(obj)
187:     initialize_methods(obj)
188:     __setobj__(obj)
189:   end

Handles the magic of delegation through __getobj__.

[Source]

     # File lib/delegate.rb, line 156
156:   def method_missing(m, *args)
157:     target = self.__getobj__
158:     unless target.respond_to?(m)
159:       super(m, *args)
160:     end
161:     target.__send__(m, *args)
162:   end

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

[Source]

     # File lib/delegate.rb, line 168
168:   def respond_to?(m)
169:     return true if super
170:     return self.__getobj__.respond_to?(m)
171:   end

[Validate]