In Files

  • delegate.rb

Class/Module Index [+]

Quicksearch

Delegator

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

Public Class Methods

delegating_block(mid) click to toggle source
 
               # File delegate.rb, line 250
def Delegator.delegating_block(mid)
  lambda do |*args, &block|
    begin
      __getobj__.__send__(mid, *args, &block)
    rescue
      re = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o
      $!.backtrace.delete_if {|t| re =~ t}
      raise
    end
  end
end
            
new(obj) click to toggle source

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

 
               # File delegate.rb, line 126
def initialize(obj)
  __setobj__(obj)
end
            

Public Instance Methods

==(obj) click to toggle source

Returns true if two objects are considered same.

 
               # File delegate.rb, line 157
def ==(obj)
  return true if obj.equal?(self)
  self.__getobj__ == obj
end
            
__getobj__() click to toggle source

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

 
               # File delegate.rb, line 166
def __getobj__
  raise NotImplementedError, "need to define `__getobj__'"
end
            
__setobj__(obj) click to toggle source

This method must be overridden by subclasses and change the object delegate to obj.

 
               # File delegate.rb, line 174
def __setobj__(obj)
  raise NotImplementedError, "need to define `__setobj__'"
end
            
clone() click to toggle source

Clone support for the object returned by _getobj_.

 
               # File delegate.rb, line 188
def clone
  new = super
  new.__setobj__(__getobj__.clone)
  new
end
            
dup() click to toggle source

Duplication support for the object returned by _getobj_.

 
               # File delegate.rb, line 194
def dup
  new = super
  new.__setobj__(__getobj__.dup)
  new
end
            
marshal_dump() click to toggle source

Serialization support for the object returned by _getobj_.

 
               # File delegate.rb, line 179
def marshal_dump
  __getobj__
end
            
marshal_load(obj) click to toggle source

Reinitializes delegation from a serialized object.

 
               # File delegate.rb, line 183
def marshal_load(obj)
  __setobj__(obj)
end
            
method_missing(m, *args, &block) click to toggle source

Handles the magic of delegation through _getobj_.

 
               # File delegate.rb, line 131
def method_missing(m, *args, &block)
  begin
    target = self.__getobj__
    unless target.respond_to?(m)
      super(m, *args, &block)
    else
      target.__send__(m, *args, &block)
    end
  rescue Exception
    $@.delete_if{|s| %r\A#{Regexp.quote(__FILE__)}:\d+:in `method_missing'\z"o =~ s}
    ::Kernel::raise
  end
end
            
respond_to?(m, include_private = false) click to toggle source

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

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

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit Documenting-ruby.org.

blog comments powered by Disqus