Class | Delegator
In: | 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

__getobj__ marshal_dump marshal_load method_missing new respond_to?

### External Aliases

initialize | -> | initialize_methods

### Public Class methods

new(obj)

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

[Source]

    # File delegate.rb, line 123
      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 "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"
          rescue SyntaxError
            raise NameError, "invalid identifier %s" % method, caller(4)
          end
        end
      end

### Public Instance methods

__getobj__()

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

[Source]

    # File delegate.rb, line 177
      def __getobj__
        raise NotImplementedError, "need to define `__getobj__'"
      end

marshal_dump()

Serialization support for the object returned by __getobj__.

[Source]

    # File delegate.rb, line 182
      def marshal_dump
        __getobj__
      end

marshal_load(obj)

Reinitializes delegation from a serialized object.

[Source]

    # File delegate.rb, line 186
      def marshal_load(obj)
        initialize_methods(obj)
      end

method_missing(m, *args)

Handles the magic of delegation through __getobj__.

[Source]

    # File delegate.rb, line 156
      def method_missing(m, *args)
        target = self.__getobj__
        unless target.respond_to?(m)
          super(m, *args)
        end
        target.__send__(m, *args)
      end

respond_to?(m)

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

[Source]

    # File delegate.rb, line 168
      def respond_to?(m)
        return true if super
        return self.__getobj__.respond_to?(m)
      end

[Validate]