Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
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
##
[4] Fix | Delete
# This library provides three different ways to delegate method calls to an
[5] Fix | Delete
# object. The easiest to use is SimpleDelegator. Pass an object to the
[6] Fix | Delete
# constructor and all methods supported by the object will be delegated. This
[7] Fix | Delete
# object can be changed later.
[8] Fix | Delete
#
[9] Fix | Delete
# Going a step further, the top level DelegateClass method allows you to easily
[10] Fix | Delete
# setup delegation through class inheritance. This is considerably more
[11] Fix | Delete
# flexible and thus probably the most common use for this library.
[12] Fix | Delete
#
[13] Fix | Delete
# Finally, if you need full control over the delegation scheme, you can inherit
[14] Fix | Delete
# from the abstract class Delegator and customize as needed. (If you find
[15] Fix | Delete
# yourself needing this control, have a look at Forwardable which is also in
[16] Fix | Delete
# the standard library. It may suit your needs better.)
[17] Fix | Delete
#
[18] Fix | Delete
# SimpleDelegator's implementation serves as a nice example if the use of
[19] Fix | Delete
# Delegator:
[20] Fix | Delete
#
[21] Fix | Delete
# class SimpleDelegator < Delegator
[22] Fix | Delete
# def initialize(obj)
[23] Fix | Delete
# super # pass obj to Delegator constructor, required
[24] Fix | Delete
# @delegate_sd_obj = obj # store obj for future use
[25] Fix | Delete
# end
[26] Fix | Delete
#
[27] Fix | Delete
# def __getobj__
[28] Fix | Delete
# @delegate_sd_obj # return object we are delegating to, required
[29] Fix | Delete
# end
[30] Fix | Delete
#
[31] Fix | Delete
# def __setobj__(obj)
[32] Fix | Delete
# @delegate_sd_obj = obj # change delegation object,
[33] Fix | Delete
# # a feature we're providing
[34] Fix | Delete
# end
[35] Fix | Delete
# end
[36] Fix | Delete
#
[37] Fix | Delete
# == Notes
[38] Fix | Delete
#
[39] Fix | Delete
# Be advised, RDoc will not detect delegated methods.
[40] Fix | Delete
#
[41] Fix | Delete
class Delegator < BasicObject
[42] Fix | Delete
kernel = ::Kernel.dup
[43] Fix | Delete
kernel.class_eval do
[44] Fix | Delete
alias __raise__ raise
[45] Fix | Delete
[:to_s,:inspect,:=~,:!~,:===,:<=>,:eql?,:hash].each do |m|
[46] Fix | Delete
undef_method m
[47] Fix | Delete
end
[48] Fix | Delete
private_instance_methods.each do |m|
[49] Fix | Delete
if /\Ablock_given\?\z|iterator\?\z|\A__.*__\z/ =~ m
[50] Fix | Delete
next
[51] Fix | Delete
end
[52] Fix | Delete
undef_method m
[53] Fix | Delete
end
[54] Fix | Delete
end
[55] Fix | Delete
include kernel
[56] Fix | Delete
[57] Fix | Delete
# :stopdoc:
[58] Fix | Delete
def self.const_missing(n)
[59] Fix | Delete
::Object.const_get(n)
[60] Fix | Delete
end
[61] Fix | Delete
# :startdoc:
[62] Fix | Delete
[63] Fix | Delete
##
[64] Fix | Delete
# :method: raise
[65] Fix | Delete
# Use __raise__ if your Delegator does not have a object to delegate the
[66] Fix | Delete
# raise method call.
[67] Fix | Delete
#
[68] Fix | Delete
[69] Fix | Delete
#
[70] Fix | Delete
# Pass in the _obj_ to delegate method calls to. All methods supported by
[71] Fix | Delete
# _obj_ will be delegated to.
[72] Fix | Delete
#
[73] Fix | Delete
def initialize(obj)
[74] Fix | Delete
__setobj__(obj)
[75] Fix | Delete
end
[76] Fix | Delete
[77] Fix | Delete
#
[78] Fix | Delete
# Handles the magic of delegation through \_\_getobj\_\_.
[79] Fix | Delete
#
[80] Fix | Delete
def method_missing(m, *args, &block)
[81] Fix | Delete
r = true
[82] Fix | Delete
target = self.__getobj__ {r = false}
[83] Fix | Delete
begin
[84] Fix | Delete
if r && target.respond_to?(m)
[85] Fix | Delete
target.__send__(m, *args, &block)
[86] Fix | Delete
elsif ::Kernel.respond_to?(m, true)
[87] Fix | Delete
::Kernel.instance_method(m).bind(self).(*args, &block)
[88] Fix | Delete
else
[89] Fix | Delete
super(m, *args, &block)
[90] Fix | Delete
end
[91] Fix | Delete
ensure
[92] Fix | Delete
$@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:(?:#{[__LINE__-7, __LINE__-5, __LINE__-3].join('|')}):"o =~ t} if $@
[93] Fix | Delete
end
[94] Fix | Delete
end
[95] Fix | Delete
[96] Fix | Delete
#
[97] Fix | Delete
# Checks for a method provided by this the delegate object by forwarding the
[98] Fix | Delete
# call through \_\_getobj\_\_.
[99] Fix | Delete
#
[100] Fix | Delete
def respond_to_missing?(m, include_private)
[101] Fix | Delete
r = true
[102] Fix | Delete
target = self.__getobj__ {r = false}
[103] Fix | Delete
r &&= target.respond_to?(m, include_private)
[104] Fix | Delete
if r && include_private && !target.respond_to?(m, false)
[105] Fix | Delete
warn "#{caller(3)[0]}: delegator does not forward private method \##{m}"
[106] Fix | Delete
return false
[107] Fix | Delete
end
[108] Fix | Delete
r
[109] Fix | Delete
end
[110] Fix | Delete
[111] Fix | Delete
#
[112] Fix | Delete
# Returns the methods available to this delegate object as the union
[113] Fix | Delete
# of this object's and \_\_getobj\_\_ methods.
[114] Fix | Delete
#
[115] Fix | Delete
def methods(all=true)
[116] Fix | Delete
__getobj__.methods(all) | super
[117] Fix | Delete
end
[118] Fix | Delete
[119] Fix | Delete
#
[120] Fix | Delete
# Returns the methods available to this delegate object as the union
[121] Fix | Delete
# of this object's and \_\_getobj\_\_ public methods.
[122] Fix | Delete
#
[123] Fix | Delete
def public_methods(all=true)
[124] Fix | Delete
__getobj__.public_methods(all) | super
[125] Fix | Delete
end
[126] Fix | Delete
[127] Fix | Delete
#
[128] Fix | Delete
# Returns the methods available to this delegate object as the union
[129] Fix | Delete
# of this object's and \_\_getobj\_\_ protected methods.
[130] Fix | Delete
#
[131] Fix | Delete
def protected_methods(all=true)
[132] Fix | Delete
__getobj__.protected_methods(all) | super
[133] Fix | Delete
end
[134] Fix | Delete
[135] Fix | Delete
# Note: no need to specialize private_methods, since they are not forwarded
[136] Fix | Delete
[137] Fix | Delete
#
[138] Fix | Delete
# Returns true if two objects are considered of equal value.
[139] Fix | Delete
#
[140] Fix | Delete
def ==(obj)
[141] Fix | Delete
return true if obj.equal?(self)
[142] Fix | Delete
self.__getobj__ == obj
[143] Fix | Delete
end
[144] Fix | Delete
[145] Fix | Delete
#
[146] Fix | Delete
# Returns true if two objects are not considered of equal value.
[147] Fix | Delete
#
[148] Fix | Delete
def !=(obj)
[149] Fix | Delete
return false if obj.equal?(self)
[150] Fix | Delete
__getobj__ != obj
[151] Fix | Delete
end
[152] Fix | Delete
[153] Fix | Delete
#
[154] Fix | Delete
# Delegates ! to the \_\_getobj\_\_
[155] Fix | Delete
#
[156] Fix | Delete
def !
[157] Fix | Delete
!__getobj__
[158] Fix | Delete
end
[159] Fix | Delete
[160] Fix | Delete
#
[161] Fix | Delete
# This method must be overridden by subclasses and should return the object
[162] Fix | Delete
# method calls are being delegated to.
[163] Fix | Delete
#
[164] Fix | Delete
def __getobj__
[165] Fix | Delete
__raise__ ::NotImplementedError, "need to define `__getobj__'"
[166] Fix | Delete
end
[167] Fix | Delete
[168] Fix | Delete
#
[169] Fix | Delete
# This method must be overridden by subclasses and change the object delegate
[170] Fix | Delete
# to _obj_.
[171] Fix | Delete
#
[172] Fix | Delete
def __setobj__(obj)
[173] Fix | Delete
__raise__ ::NotImplementedError, "need to define `__setobj__'"
[174] Fix | Delete
end
[175] Fix | Delete
[176] Fix | Delete
#
[177] Fix | Delete
# Serialization support for the object returned by \_\_getobj\_\_.
[178] Fix | Delete
#
[179] Fix | Delete
def marshal_dump
[180] Fix | Delete
ivars = instance_variables.reject {|var| /\A@delegate_/ =~ var}
[181] Fix | Delete
[
[182] Fix | Delete
:__v2__,
[183] Fix | Delete
ivars, ivars.map{|var| instance_variable_get(var)},
[184] Fix | Delete
__getobj__
[185] Fix | Delete
]
[186] Fix | Delete
end
[187] Fix | Delete
[188] Fix | Delete
#
[189] Fix | Delete
# Reinitializes delegation from a serialized object.
[190] Fix | Delete
#
[191] Fix | Delete
def marshal_load(data)
[192] Fix | Delete
version, vars, values, obj = data
[193] Fix | Delete
if version == :__v2__
[194] Fix | Delete
vars.each_with_index{|var, i| instance_variable_set(var, values[i])}
[195] Fix | Delete
__setobj__(obj)
[196] Fix | Delete
else
[197] Fix | Delete
__setobj__(data)
[198] Fix | Delete
end
[199] Fix | Delete
end
[200] Fix | Delete
[201] Fix | Delete
def initialize_clone(obj) # :nodoc:
[202] Fix | Delete
self.__setobj__(obj.__getobj__.clone)
[203] Fix | Delete
end
[204] Fix | Delete
def initialize_dup(obj) # :nodoc:
[205] Fix | Delete
self.__setobj__(obj.__getobj__.dup)
[206] Fix | Delete
end
[207] Fix | Delete
private :initialize_clone, :initialize_dup
[208] Fix | Delete
[209] Fix | Delete
##
[210] Fix | Delete
# :method: trust
[211] Fix | Delete
# Trust both the object returned by \_\_getobj\_\_ and self.
[212] Fix | Delete
#
[213] Fix | Delete
[214] Fix | Delete
##
[215] Fix | Delete
# :method: untrust
[216] Fix | Delete
# Untrust both the object returned by \_\_getobj\_\_ and self.
[217] Fix | Delete
#
[218] Fix | Delete
[219] Fix | Delete
##
[220] Fix | Delete
# :method: taint
[221] Fix | Delete
# Taint both the object returned by \_\_getobj\_\_ and self.
[222] Fix | Delete
#
[223] Fix | Delete
[224] Fix | Delete
##
[225] Fix | Delete
# :method: untaint
[226] Fix | Delete
# Untaint both the object returned by \_\_getobj\_\_ and self.
[227] Fix | Delete
#
[228] Fix | Delete
[229] Fix | Delete
##
[230] Fix | Delete
# :method: freeze
[231] Fix | Delete
# Freeze both the object returned by \_\_getobj\_\_ and self.
[232] Fix | Delete
#
[233] Fix | Delete
[234] Fix | Delete
[:trust, :untrust, :taint, :untaint, :freeze].each do |method|
[235] Fix | Delete
define_method method do
[236] Fix | Delete
__getobj__.send(method)
[237] Fix | Delete
super()
[238] Fix | Delete
end
[239] Fix | Delete
end
[240] Fix | Delete
[241] Fix | Delete
@delegator_api = self.public_instance_methods
[242] Fix | Delete
def self.public_api # :nodoc:
[243] Fix | Delete
@delegator_api
[244] Fix | Delete
end
[245] Fix | Delete
end
[246] Fix | Delete
[247] Fix | Delete
##
[248] Fix | Delete
# A concrete implementation of Delegator, this class provides the means to
[249] Fix | Delete
# delegate all supported method calls to the object passed into the constructor
[250] Fix | Delete
# and even to change the object being delegated to at a later time with
[251] Fix | Delete
# #__setobj__.
[252] Fix | Delete
#
[253] Fix | Delete
# class User
[254] Fix | Delete
# def born_on
[255] Fix | Delete
# Date.new(1989, 9, 10)
[256] Fix | Delete
# end
[257] Fix | Delete
# end
[258] Fix | Delete
#
[259] Fix | Delete
# class UserDecorator < SimpleDelegator
[260] Fix | Delete
# def birth_year
[261] Fix | Delete
# born_on.year
[262] Fix | Delete
# end
[263] Fix | Delete
# end
[264] Fix | Delete
#
[265] Fix | Delete
# decorated_user = UserDecorator.new(User.new)
[266] Fix | Delete
# decorated_user.birth_year #=> 1989
[267] Fix | Delete
# decorated_user.__getobj__ #=> #<User: ...>
[268] Fix | Delete
#
[269] Fix | Delete
# A SimpleDelegator instance can take advantage of the fact that SimpleDelegator
[270] Fix | Delete
# is a subclass of +Delegator+ to call <tt>super</tt> to have methods called on
[271] Fix | Delete
# the object being delegated to.
[272] Fix | Delete
#
[273] Fix | Delete
# class SuperArray < SimpleDelegator
[274] Fix | Delete
# def [](*args)
[275] Fix | Delete
# super + 1
[276] Fix | Delete
# end
[277] Fix | Delete
# end
[278] Fix | Delete
#
[279] Fix | Delete
# SuperArray.new([1])[0] #=> 2
[280] Fix | Delete
#
[281] Fix | Delete
# Here's a simple example that takes advantage of the fact that
[282] Fix | Delete
# SimpleDelegator's delegation object can be changed at any time.
[283] Fix | Delete
#
[284] Fix | Delete
# class Stats
[285] Fix | Delete
# def initialize
[286] Fix | Delete
# @source = SimpleDelegator.new([])
[287] Fix | Delete
# end
[288] Fix | Delete
#
[289] Fix | Delete
# def stats(records)
[290] Fix | Delete
# @source.__setobj__(records)
[291] Fix | Delete
#
[292] Fix | Delete
# "Elements: #{@source.size}\n" +
[293] Fix | Delete
# " Non-Nil: #{@source.compact.size}\n" +
[294] Fix | Delete
# " Unique: #{@source.uniq.size}\n"
[295] Fix | Delete
# end
[296] Fix | Delete
# end
[297] Fix | Delete
#
[298] Fix | Delete
# s = Stats.new
[299] Fix | Delete
# puts s.stats(%w{James Edward Gray II})
[300] Fix | Delete
# puts
[301] Fix | Delete
# puts s.stats([1, 2, 3, nil, 4, 5, 1, 2])
[302] Fix | Delete
#
[303] Fix | Delete
# Prints:
[304] Fix | Delete
#
[305] Fix | Delete
# Elements: 4
[306] Fix | Delete
# Non-Nil: 4
[307] Fix | Delete
# Unique: 4
[308] Fix | Delete
#
[309] Fix | Delete
# Elements: 8
[310] Fix | Delete
# Non-Nil: 7
[311] Fix | Delete
# Unique: 6
[312] Fix | Delete
#
[313] Fix | Delete
class SimpleDelegator<Delegator
[314] Fix | Delete
# Returns the current object method calls are being delegated to.
[315] Fix | Delete
def __getobj__
[316] Fix | Delete
unless defined?(@delegate_sd_obj)
[317] Fix | Delete
return yield if block_given?
[318] Fix | Delete
__raise__ ::ArgumentError, "not delegated"
[319] Fix | Delete
end
[320] Fix | Delete
@delegate_sd_obj
[321] Fix | Delete
end
[322] Fix | Delete
[323] Fix | Delete
#
[324] Fix | Delete
# Changes the delegate object to _obj_.
[325] Fix | Delete
#
[326] Fix | Delete
# It's important to note that this does *not* cause SimpleDelegator's methods
[327] Fix | Delete
# to change. Because of this, you probably only want to change delegation
[328] Fix | Delete
# to objects of the same type as the original delegate.
[329] Fix | Delete
#
[330] Fix | Delete
# Here's an example of changing the delegation object.
[331] Fix | Delete
#
[332] Fix | Delete
# names = SimpleDelegator.new(%w{James Edward Gray II})
[333] Fix | Delete
# puts names[1] # => Edward
[334] Fix | Delete
# names.__setobj__(%w{Gavin Sinclair})
[335] Fix | Delete
# puts names[1] # => Sinclair
[336] Fix | Delete
#
[337] Fix | Delete
def __setobj__(obj)
[338] Fix | Delete
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
[339] Fix | Delete
@delegate_sd_obj = obj
[340] Fix | Delete
end
[341] Fix | Delete
end
[342] Fix | Delete
[343] Fix | Delete
def Delegator.delegating_block(mid) # :nodoc:
[344] Fix | Delete
lambda do |*args, &block|
[345] Fix | Delete
target = self.__getobj__
[346] Fix | Delete
begin
[347] Fix | Delete
target.__send__(mid, *args, &block)
[348] Fix | Delete
ensure
[349] Fix | Delete
$@.delete_if {|t| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:/o =~ t} if $@
[350] Fix | Delete
end
[351] Fix | Delete
end
[352] Fix | Delete
end
[353] Fix | Delete
[354] Fix | Delete
#
[355] Fix | Delete
# The primary interface to this library. Use to setup delegation when defining
[356] Fix | Delete
# your class.
[357] Fix | Delete
#
[358] Fix | Delete
# class MyClass < DelegateClass(ClassToDelegateTo) # Step 1
[359] Fix | Delete
# def initialize
[360] Fix | Delete
# super(obj_of_ClassToDelegateTo) # Step 2
[361] Fix | Delete
# end
[362] Fix | Delete
# end
[363] Fix | Delete
#
[364] Fix | Delete
# Here's a sample of use from Tempfile which is really a File object with a
[365] Fix | Delete
# few special rules about storage location and when the File should be
[366] Fix | Delete
# deleted. That makes for an almost textbook perfect example of how to use
[367] Fix | Delete
# delegation.
[368] Fix | Delete
#
[369] Fix | Delete
# class Tempfile < DelegateClass(File)
[370] Fix | Delete
# # constant and class member data initialization...
[371] Fix | Delete
#
[372] Fix | Delete
# def initialize(basename, tmpdir=Dir::tmpdir)
[373] Fix | Delete
# # build up file path/name in var tmpname...
[374] Fix | Delete
#
[375] Fix | Delete
# @tmpfile = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL, 0600)
[376] Fix | Delete
#
[377] Fix | Delete
# # ...
[378] Fix | Delete
#
[379] Fix | Delete
# super(@tmpfile)
[380] Fix | Delete
#
[381] Fix | Delete
# # below this point, all methods of File are supported...
[382] Fix | Delete
# end
[383] Fix | Delete
#
[384] Fix | Delete
# # ...
[385] Fix | Delete
# end
[386] Fix | Delete
#
[387] Fix | Delete
def DelegateClass(superclass)
[388] Fix | Delete
klass = Class.new(Delegator)
[389] Fix | Delete
methods = superclass.instance_methods
[390] Fix | Delete
methods -= ::Delegator.public_api
[391] Fix | Delete
methods -= [:to_s,:inspect,:=~,:!~,:===]
[392] Fix | Delete
klass.module_eval do
[393] Fix | Delete
def __getobj__ # :nodoc:
[394] Fix | Delete
unless defined?(@delegate_dc_obj)
[395] Fix | Delete
return yield if block_given?
[396] Fix | Delete
__raise__ ::ArgumentError, "not delegated"
[397] Fix | Delete
end
[398] Fix | Delete
@delegate_dc_obj
[399] Fix | Delete
end
[400] Fix | Delete
def __setobj__(obj) # :nodoc:
[401] Fix | Delete
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
[402] Fix | Delete
@delegate_dc_obj = obj
[403] Fix | Delete
end
[404] Fix | Delete
methods.each do |method|
[405] Fix | Delete
define_method(method, Delegator.delegating_block(method))
[406] Fix | Delete
end
[407] Fix | Delete
end
[408] Fix | Delete
klass.define_singleton_method :public_instance_methods do |all=true|
[409] Fix | Delete
super(all) - superclass.protected_instance_methods
[410] Fix | Delete
end
[411] Fix | Delete
klass.define_singleton_method :protected_instance_methods do |all=true|
[412] Fix | Delete
super(all) | superclass.protected_instance_methods
[413] Fix | Delete
end
[414] Fix | Delete
return klass
[415] Fix | Delete
end
[416] Fix | Delete
[417] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function