Top Level Namespace

Defined Under Namespace

Classes: Delegator, ExtArray, SimpleDelegator

Instance Method Summary collapse

Instance Method Details

#DelegateClass(superclass) ⇒ Object

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


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/delegate.rb', line 356

def DelegateClass(superclass)
  klass = Class.new(Delegator)
  methods = superclass.instance_methods
  methods -= ::Delegator.public_api
  methods -= [:to_s,:inspect,:=~,:!~,:===]
  klass.module_eval do
    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
    methods.each do |method|
      define_method(method, Delegator.delegating_block(method))
    end
  end
  klass.define_singleton_method :public_instance_methods do |all=true|
    super(all) - superclass.protected_instance_methods
  end
  klass.define_singleton_method :protected_instance_methods do |all=true|
    super(all) | superclass.protected_instance_methods
  end
  return klass
end