BasicObject
Delegator is an abstract class used to build delegator pattern objects from subclasses. Subclasses should redefine __getobj__. For a concrete implementation, see SimpleDelegator.
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
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
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
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 self and target at once.
# File delegate.rb, line 290
def freeze
__getobj__.freeze
super
end
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
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
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| %\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:" =~ t} if $@
end
end
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
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
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
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 both the object returned by __getobj__ and self.
# File delegate.rb, line 275
Trust both the object returned by __getobj__ and self.
# File delegate.rb, line 265