In Files

  • delegate.rb

Parent

Methods

Class/Module Index [+]

Quicksearch

Object

Public Instance Methods

DelegateClass(superclass) click to toggle source

The primary interface to this library. Use to setup delegation when defining your class.

class MyClass < DelegateClass( ClassToDelegateTo )    # Step 1
  def initialize
    super(obj_of_ClassToDelegateTo)                   # Step 2
  end
end
 
               # File delegate.rb, line 273
def DelegateClass(superclass)
  klass = Class.new(Delegator)
  methods = superclass.public_instance_methods(true)
  methods -= ::Delegator.public_api
  methods -= [:to_s,:inspect,:=~,:!~,:===]
  klass.module_eval {
    def __getobj__  # :nodoc:
      @delegate_dc_obj
    end
    def __setobj__(obj)  # :nodoc:
      raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
      @delegate_dc_obj = obj
    end
  }
  klass.module_eval do
    methods.each do |method|
      define_method(method, Delegator.delegating_block(method))
    end
  end
  return klass
end