Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby18/lib64/ruby/1.8
File: delegate.rb
# = delegate -- Support for the Delegation Pattern
[0] Fix | Delete
#
[1] Fix | Delete
# Documentation by James Edward Gray II and Gavin Sinclair
[2] Fix | Delete
#
[3] Fix | Delete
# == Introduction
[4] Fix | Delete
#
[5] Fix | Delete
# This library provides three different ways to delegate method calls to an
[6] Fix | Delete
# object. The easiest to use is SimpleDelegator. Pass an object to the
[7] Fix | Delete
# constructor and all methods supported by the object will be delegated. This
[8] Fix | Delete
# object can be changed later.
[9] Fix | Delete
#
[10] Fix | Delete
# Going a step further, the top level DelegateClass method allows you to easily
[11] Fix | Delete
# setup delegation through class inheritance. This is considerably more
[12] Fix | Delete
# flexible and thus probably the most common use for this library.
[13] Fix | Delete
#
[14] Fix | Delete
# Finally, if you need full control over the delegation scheme, you can inherit
[15] Fix | Delete
# from the abstract class Delegator and customize as needed. (If you find
[16] Fix | Delete
# yourself needing this control, have a look at _forwardable_, also in the
[17] Fix | Delete
# standard library. It may suit your needs better.)
[18] Fix | Delete
#
[19] Fix | Delete
# == Notes
[20] Fix | Delete
#
[21] Fix | Delete
# Be advised, RDoc will not detect delegated methods.
[22] Fix | Delete
#
[23] Fix | Delete
# <b>delegate.rb provides full-class delegation via the
[24] Fix | Delete
# DelegateClass() method. For single-method delegation via
[25] Fix | Delete
# def_delegator(), see forwardable.rb.</b>
[26] Fix | Delete
#
[27] Fix | Delete
# == Examples
[28] Fix | Delete
#
[29] Fix | Delete
# === SimpleDelegator
[30] Fix | Delete
#
[31] Fix | Delete
# Here's a simple example that takes advantage of the fact that
[32] Fix | Delete
# SimpleDelegator's delegation object can be changed at any time.
[33] Fix | Delete
#
[34] Fix | Delete
# class Stats
[35] Fix | Delete
# def initialize
[36] Fix | Delete
# @source = SimpleDelegator.new([])
[37] Fix | Delete
# end
[38] Fix | Delete
#
[39] Fix | Delete
# def stats( records )
[40] Fix | Delete
# @source.__setobj__(records)
[41] Fix | Delete
#
[42] Fix | Delete
# "Elements: #{@source.size}\n" +
[43] Fix | Delete
# " Non-Nil: #{@source.compact.size}\n" +
[44] Fix | Delete
# " Unique: #{@source.uniq.size}\n"
[45] Fix | Delete
# end
[46] Fix | Delete
# end
[47] Fix | Delete
#
[48] Fix | Delete
# s = Stats.new
[49] Fix | Delete
# puts s.stats(%w{James Edward Gray II})
[50] Fix | Delete
# puts
[51] Fix | Delete
# puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
[52] Fix | Delete
#
[53] Fix | Delete
# <i>Prints:</i>
[54] Fix | Delete
#
[55] Fix | Delete
# Elements: 4
[56] Fix | Delete
# Non-Nil: 4
[57] Fix | Delete
# Unique: 4
[58] Fix | Delete
#
[59] Fix | Delete
# Elements: 8
[60] Fix | Delete
# Non-Nil: 7
[61] Fix | Delete
# Unique: 6
[62] Fix | Delete
#
[63] Fix | Delete
# === DelegateClass()
[64] Fix | Delete
#
[65] Fix | Delete
# Here's a sample of use from <i>tempfile.rb</i>.
[66] Fix | Delete
#
[67] Fix | Delete
# A _Tempfile_ object is really just a _File_ object with a few special rules
[68] Fix | Delete
# about storage location and/or when the File should be deleted. That makes for
[69] Fix | Delete
# an almost textbook perfect example of how to use delegation.
[70] Fix | Delete
#
[71] Fix | Delete
# class Tempfile < DelegateClass(File)
[72] Fix | Delete
# # constant and class member data initialization...
[73] Fix | Delete
#
[74] Fix | Delete
# def initialize(basename, tmpdir=Dir::tmpdir)
[75] Fix | Delete
# # build up file path/name in var tmpname...
[76] Fix | Delete
#
[77] Fix | Delete
# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
[78] Fix | Delete
#
[79] Fix | Delete
# # ...
[80] Fix | Delete
#
[81] Fix | Delete
# super(@tmpfile)
[82] Fix | Delete
#
[83] Fix | Delete
# # below this point, all methods of File are supported...
[84] Fix | Delete
# end
[85] Fix | Delete
#
[86] Fix | Delete
# # ...
[87] Fix | Delete
# end
[88] Fix | Delete
#
[89] Fix | Delete
# === Delegator
[90] Fix | Delete
#
[91] Fix | Delete
# SimpleDelegator's implementation serves as a nice example here.
[92] Fix | Delete
#
[93] Fix | Delete
# class SimpleDelegator < Delegator
[94] Fix | Delete
# def initialize(obj)
[95] Fix | Delete
# super # pass obj to Delegator constructor, required
[96] Fix | Delete
# @_sd_obj = obj # store obj for future use
[97] Fix | Delete
# end
[98] Fix | Delete
#
[99] Fix | Delete
# def __getobj__
[100] Fix | Delete
# @_sd_obj # return object we are delegating to, required
[101] Fix | Delete
# end
[102] Fix | Delete
#
[103] Fix | Delete
# def __setobj__(obj)
[104] Fix | Delete
# @_sd_obj = obj # change delegation object, a feature we're providing
[105] Fix | Delete
# end
[106] Fix | Delete
#
[107] Fix | Delete
# # ...
[108] Fix | Delete
# end
[109] Fix | Delete
[110] Fix | Delete
#
[111] Fix | Delete
# Delegator is an abstract class used to build delegator pattern objects from
[112] Fix | Delete
# subclasses. Subclasses should redefine \_\_getobj\_\_. For a concrete
[113] Fix | Delete
# implementation, see SimpleDelegator.
[114] Fix | Delete
#
[115] Fix | Delete
class Delegator
[116] Fix | Delete
IgnoreBacktracePat = %r"\A#{Regexp.quote(__FILE__)}:\d+:in `"
[117] Fix | Delete
[118] Fix | Delete
#
[119] Fix | Delete
# Pass in the _obj_ to delegate method calls to. All methods supported by
[120] Fix | Delete
# _obj_ will be delegated to.
[121] Fix | Delete
#
[122] Fix | Delete
def initialize(obj)
[123] Fix | Delete
preserved = ::Kernel.public_instance_methods(false)
[124] Fix | Delete
preserved -= ["to_s","to_a","inspect","==","=~","==="]
[125] Fix | Delete
for t in self.class.ancestors
[126] Fix | Delete
preserved |= t.public_instance_methods(false)
[127] Fix | Delete
preserved |= t.private_instance_methods(false)
[128] Fix | Delete
preserved |= t.protected_instance_methods(false)
[129] Fix | Delete
break if t == Delegator
[130] Fix | Delete
end
[131] Fix | Delete
preserved << "singleton_method_added"
[132] Fix | Delete
for method in obj.methods
[133] Fix | Delete
next if preserved.include? method
[134] Fix | Delete
begin
[135] Fix | Delete
eval <<-EOS, nil, __FILE__, __LINE__+1
[136] Fix | Delete
def self.#{method}(*args, &block)
[137] Fix | Delete
begin
[138] Fix | Delete
__getobj__.__send__(:#{method}, *args, &block)
[139] Fix | Delete
ensure
[140] Fix | Delete
$@.delete_if{|s|IgnoreBacktracePat=~s} if $@
[141] Fix | Delete
end
[142] Fix | Delete
end
[143] Fix | Delete
EOS
[144] Fix | Delete
rescue SyntaxError
[145] Fix | Delete
raise NameError, "invalid identifier %s" % method, caller(4)
[146] Fix | Delete
end
[147] Fix | Delete
end
[148] Fix | Delete
end
[149] Fix | Delete
alias initialize_methods initialize
[150] Fix | Delete
[151] Fix | Delete
# Handles the magic of delegation through \_\_getobj\_\_.
[152] Fix | Delete
def method_missing(m, *args, &block)
[153] Fix | Delete
target = self.__getobj__
[154] Fix | Delete
unless target.respond_to?(m)
[155] Fix | Delete
super(m, *args, &block)
[156] Fix | Delete
end
[157] Fix | Delete
target.__send__(m, *args, &block)
[158] Fix | Delete
end
[159] Fix | Delete
[160] Fix | Delete
#
[161] Fix | Delete
# Checks for a method provided by this the delegate object by fowarding the
[162] Fix | Delete
# call through \_\_getobj\_\_.
[163] Fix | Delete
#
[164] Fix | Delete
def respond_to?(m, include_private = false)
[165] Fix | Delete
return true if super
[166] Fix | Delete
return self.__getobj__.respond_to?(m, include_private)
[167] Fix | Delete
end
[168] Fix | Delete
[169] Fix | Delete
#
[170] Fix | Delete
# This method must be overridden by subclasses and should return the object
[171] Fix | Delete
# method calls are being delegated to.
[172] Fix | Delete
#
[173] Fix | Delete
def __getobj__
[174] Fix | Delete
raise NotImplementedError, "need to define `__getobj__'"
[175] Fix | Delete
end
[176] Fix | Delete
[177] Fix | Delete
# Serialization support for the object returned by \_\_getobj\_\_.
[178] Fix | Delete
def marshal_dump
[179] Fix | Delete
__getobj__
[180] Fix | Delete
end
[181] Fix | Delete
# Reinitializes delegation from a serialized object.
[182] Fix | Delete
def marshal_load(obj)
[183] Fix | Delete
initialize_methods(obj)
[184] Fix | Delete
__setobj__(obj)
[185] Fix | Delete
end
[186] Fix | Delete
end
[187] Fix | Delete
[188] Fix | Delete
#
[189] Fix | Delete
# A concrete implementation of Delegator, this class provides the means to
[190] Fix | Delete
# delegate all supported method calls to the object passed into the constructor
[191] Fix | Delete
# and even to change the object being delegated to at a later time with
[192] Fix | Delete
# \_\_setobj\_\_ .
[193] Fix | Delete
#
[194] Fix | Delete
class SimpleDelegator<Delegator
[195] Fix | Delete
[196] Fix | Delete
# Pass in the _obj_ you would like to delegate method calls to.
[197] Fix | Delete
def initialize(obj)
[198] Fix | Delete
super
[199] Fix | Delete
@_sd_obj = obj
[200] Fix | Delete
end
[201] Fix | Delete
[202] Fix | Delete
# Returns the current object method calls are being delegated to.
[203] Fix | Delete
def __getobj__
[204] Fix | Delete
@_sd_obj
[205] Fix | Delete
end
[206] Fix | Delete
[207] Fix | Delete
#
[208] Fix | Delete
# Changes the delegate object to _obj_.
[209] Fix | Delete
#
[210] Fix | Delete
# It's important to note that this does *not* cause SimpleDelegator's methods
[211] Fix | Delete
# to change. Because of this, you probably only want to change delegation
[212] Fix | Delete
# to objects of the same type as the original delegate.
[213] Fix | Delete
#
[214] Fix | Delete
# Here's an example of changing the delegation object.
[215] Fix | Delete
#
[216] Fix | Delete
# names = SimpleDelegator.new(%w{James Edward Gray II})
[217] Fix | Delete
# puts names[1] # => Edward
[218] Fix | Delete
# names.__setobj__(%w{Gavin Sinclair})
[219] Fix | Delete
# puts names[1] # => Sinclair
[220] Fix | Delete
#
[221] Fix | Delete
def __setobj__(obj)
[222] Fix | Delete
raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
[223] Fix | Delete
@_sd_obj = obj
[224] Fix | Delete
end
[225] Fix | Delete
[226] Fix | Delete
# Clone support for the object returned by \_\_getobj\_\_.
[227] Fix | Delete
def clone
[228] Fix | Delete
new = super
[229] Fix | Delete
new.__setobj__(__getobj__.clone)
[230] Fix | Delete
new
[231] Fix | Delete
end
[232] Fix | Delete
# Duplication support for the object returned by \_\_getobj\_\_.
[233] Fix | Delete
def dup
[234] Fix | Delete
new = super
[235] Fix | Delete
new.__setobj__(__getobj__.clone)
[236] Fix | Delete
new
[237] Fix | Delete
end
[238] Fix | Delete
end
[239] Fix | Delete
[240] Fix | Delete
# :stopdoc:
[241] Fix | Delete
# backward compatibility ^_^;;;
[242] Fix | Delete
Delegater = Delegator
[243] Fix | Delete
SimpleDelegater = SimpleDelegator
[244] Fix | Delete
# :startdoc:
[245] Fix | Delete
[246] Fix | Delete
#
[247] Fix | Delete
# The primary interface to this library. Use to setup delegation when defining
[248] Fix | Delete
# your class.
[249] Fix | Delete
#
[250] Fix | Delete
# class MyClass < DelegateClass( ClassToDelegateTo ) # Step 1
[251] Fix | Delete
# def initialize
[252] Fix | Delete
# super(obj_of_ClassToDelegateTo) # Step 2
[253] Fix | Delete
# end
[254] Fix | Delete
# end
[255] Fix | Delete
#
[256] Fix | Delete
def DelegateClass(superclass)
[257] Fix | Delete
klass = Class.new
[258] Fix | Delete
methods = superclass.public_instance_methods(true)
[259] Fix | Delete
methods -= ::Kernel.public_instance_methods(false)
[260] Fix | Delete
methods |= ["to_s","to_a","inspect","==","=~","==="]
[261] Fix | Delete
klass.module_eval {
[262] Fix | Delete
def initialize(obj) # :nodoc:
[263] Fix | Delete
@_dc_obj = obj
[264] Fix | Delete
end
[265] Fix | Delete
def method_missing(m, *args, &block) # :nodoc:
[266] Fix | Delete
unless @_dc_obj.respond_to?(m)
[267] Fix | Delete
super(m, *args, &block)
[268] Fix | Delete
end
[269] Fix | Delete
@_dc_obj.__send__(m, *args, &block)
[270] Fix | Delete
end
[271] Fix | Delete
def respond_to?(m, include_private = false) # :nodoc:
[272] Fix | Delete
return true if super
[273] Fix | Delete
return @_dc_obj.respond_to?(m, include_private)
[274] Fix | Delete
end
[275] Fix | Delete
def __getobj__ # :nodoc:
[276] Fix | Delete
@_dc_obj
[277] Fix | Delete
end
[278] Fix | Delete
def __setobj__(obj) # :nodoc:
[279] Fix | Delete
raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
[280] Fix | Delete
@_dc_obj = obj
[281] Fix | Delete
end
[282] Fix | Delete
def clone # :nodoc:
[283] Fix | Delete
new = super
[284] Fix | Delete
new.__setobj__(__getobj__.clone)
[285] Fix | Delete
new
[286] Fix | Delete
end
[287] Fix | Delete
def dup # :nodoc:
[288] Fix | Delete
new = super
[289] Fix | Delete
new.__setobj__(__getobj__.clone)
[290] Fix | Delete
new
[291] Fix | Delete
end
[292] Fix | Delete
}
[293] Fix | Delete
for method in methods
[294] Fix | Delete
begin
[295] Fix | Delete
klass.module_eval <<-EOS, __FILE__, __LINE__+1
[296] Fix | Delete
def #{method}(*args, &block)
[297] Fix | Delete
begin
[298] Fix | Delete
@_dc_obj.__send__(:#{method}, *args, &block)
[299] Fix | Delete
ensure
[300] Fix | Delete
$@.delete_if{|s| ::Delegator::IgnoreBacktracePat =~ s} if $@
[301] Fix | Delete
end
[302] Fix | Delete
end
[303] Fix | Delete
EOS
[304] Fix | Delete
rescue SyntaxError
[305] Fix | Delete
raise NameError, "invalid identifier %s" % method, caller(3)
[306] Fix | Delete
end
[307] Fix | Delete
end
[308] Fix | Delete
return klass
[309] Fix | Delete
end
[310] Fix | Delete
[311] Fix | Delete
# :enddoc:
[312] Fix | Delete
[313] Fix | Delete
if __FILE__ == $0
[314] Fix | Delete
class ExtArray<DelegateClass(Array)
[315] Fix | Delete
def initialize()
[316] Fix | Delete
super([])
[317] Fix | Delete
end
[318] Fix | Delete
end
[319] Fix | Delete
[320] Fix | Delete
ary = ExtArray.new
[321] Fix | Delete
p ary.class
[322] Fix | Delete
ary.push 25
[323] Fix | Delete
p ary
[324] Fix | Delete
[325] Fix | Delete
foo = Object.new
[326] Fix | Delete
def foo.test
[327] Fix | Delete
25
[328] Fix | Delete
end
[329] Fix | Delete
def foo.error
[330] Fix | Delete
raise 'this is OK'
[331] Fix | Delete
end
[332] Fix | Delete
foo2 = SimpleDelegator.new(foo)
[333] Fix | Delete
p foo.test == foo2.test # => true
[334] Fix | Delete
foo2.error # raise error!
[335] Fix | Delete
end
[336] Fix | Delete
[337] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function