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