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 334
def Delegator.delegating_block(mid)
  lambda do |*args, &block|
    target = self.__getobj__
    begin
      target.__send__(mid, *args, &block)
    ensure
      $@.delete_if {|t| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/ =~ t} if $@
    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 136
def initialize(obj)
  __setobj__(obj)
end
            

Public Instance Methods

!() click to toggle source
 
               # File delegate.rb, line 207
def !
  !__getobj__
end
            
!=(obj) click to toggle source

Returns true if two objects are not considered of equal value.

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

Returns true if two objects are considered of equal value.

 
               # File delegate.rb, line 194
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 215
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 223
def __setobj__(obj)
  raise NotImplementedError, "need to define `__setobj__'"
end
            
freeze() click to toggle source

Freeze self and target at once.

 
               # File delegate.rb, line 290
def freeze
  __getobj__.freeze
  super
end
            
marshal_dump() click to toggle source

Serialization support for the object returned by __getobj__.

 
               # File delegate.rb, line 230
def marshal_dump
  ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
  [
    :__v2__,
    ivars, ivars.map{|var| instance_variable_get(var)},
    __getobj__
  ]
end
            
marshal_load(data) click to toggle source

Reinitializes delegation from a serialized object.

 
               # File delegate.rb, line 242
def marshal_load(data)
  version, vars, values, obj = data
  if version == :__v2__
    vars.each_with_index{|var, i| instance_variable_set(var, values[i])}
    __setobj__(obj)
  else
    __setobj__(data)
  end
end
            
method_missing(m, *args, &block) click to toggle source

Handles the magic of delegation through __getobj__.

 
               # File delegate.rb, line 143
def method_missing(m, *args, &block)
  target = self.__getobj__
  begin
    target.respond_to?(m) ? target.__send__(m, *args, &block) : super(m, *args, &block)
  ensure
    $@.delete_if {|t| %r\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:" =~ t} if $@
  end
end
            
methods() click to toggle source

Returns the methods available to this delegate object as the union of this object's and __getobj__ methods.

 
               # File delegate.rb, line 169
def methods
  __getobj__.methods | super
end
            
protected_methods(all=true) click to toggle source

Returns the methods available to this delegate object as the union of this object's and __getobj__ protected methods.

 
               # File delegate.rb, line 185
def protected_methods(all=true)
  __getobj__.protected_methods(all) | super
end
            
public_methods(all=true) click to toggle source

Returns the methods available to this delegate object as the union of this object's and __getobj__ public methods.

 
               # File delegate.rb, line 177
def public_methods(all=true)
  __getobj__.public_methods(all) | super
end
            
respond_to_missing?(m, include_private) click to toggle source

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

 
               # File delegate.rb, line 156
def respond_to_missing?(m, include_private)
  r = self.__getobj__.respond_to?(m, include_private)
  if r && include_private && !self.__getobj__.respond_to?(m, false)
    warn "#{caller(3)[0]}: delegator does not forward private method \##{m}"
    return false
  end
  r
end
            
taint click to toggle source

Taint both the object returned by __getobj__ and self.

 
               # File delegate.rb, line 275
  
            
trust click to toggle source

Trust both the object returned by __getobj__ and self.

 
               # File delegate.rb, line 265
  
            
untaint click to toggle source

Untaint both the object returned by __getobj__ and self.

 
               # File delegate.rb, line 280
  
            
untrust click to toggle source

Untrust both the object returned by __getobj__ and self.

 
               # File delegate.rb, line 270