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

Constants

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

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 124
124:   def initialize(obj)
125:     preserved = ::Kernel.public_instance_methods(false)
126:     preserved -= ["to_s","to_a","inspect","==","=~","==="]
127:     for t in self.class.ancestors
128:       preserved |= t.public_instance_methods(false)
129:       preserved |= t.private_instance_methods(false)
130:       preserved |= t.protected_instance_methods(false)
131:       break if t == Delegator
132:     end
133:     preserved << "singleton_method_added"
134:     for method in obj.methods
135:       next if preserved.include? method
136:       begin
137:         eval "def self.\#{method}(*args, &block)\nbegin\n__getobj__.__send__(:\#{method}, *args, &block)\nensure\n$@.delete_if{|s|IgnoreBacktracePat=~s} if $@\nend\nend\n", nil, __FILE__, __LINE__+1
138:       rescue SyntaxError
139:         raise NameError, "invalid identifier %s" % method, caller(4)
140:       end
141:     end
142:   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 176
176:   def __getobj__
177:     raise NotImplementedError, "need to define `__getobj__'"
178:   end

Serialization support for the object returned by __getobj__.

[Source]

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

Reinitializes delegation from a serialized object.

[Source]

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

Handles the magic of delegation through __getobj__.

[Source]

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

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

[Source]

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

[Validate]