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