Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/alt/ruby31/share/ruby
File: forwardable.rb
# frozen_string_literal: false
[0] Fix | Delete
#
[1] Fix | Delete
# forwardable.rb -
[2] Fix | Delete
# $Release Version: 1.1$
[3] Fix | Delete
# $Revision$
[4] Fix | Delete
# by Keiju ISHITSUKA(keiju@ishitsuka.com)
[5] Fix | Delete
# original definition by delegator.rb
[6] Fix | Delete
# Revised by Daniel J. Berger with suggestions from Florian Gross.
[7] Fix | Delete
#
[8] Fix | Delete
# Documentation by James Edward Gray II and Gavin Sinclair
[9] Fix | Delete
[10] Fix | Delete
[11] Fix | Delete
[12] Fix | Delete
# The Forwardable module provides delegation of specified
[13] Fix | Delete
# methods to a designated object, using the methods #def_delegator
[14] Fix | Delete
# and #def_delegators.
[15] Fix | Delete
#
[16] Fix | Delete
# For example, say you have a class RecordCollection which
[17] Fix | Delete
# contains an array <tt>@records</tt>. You could provide the lookup method
[18] Fix | Delete
# #record_number(), which simply calls #[] on the <tt>@records</tt>
[19] Fix | Delete
# array, like this:
[20] Fix | Delete
#
[21] Fix | Delete
# require 'forwardable'
[22] Fix | Delete
#
[23] Fix | Delete
# class RecordCollection
[24] Fix | Delete
# attr_accessor :records
[25] Fix | Delete
# extend Forwardable
[26] Fix | Delete
# def_delegator :@records, :[], :record_number
[27] Fix | Delete
# end
[28] Fix | Delete
#
[29] Fix | Delete
# We can use the lookup method like so:
[30] Fix | Delete
#
[31] Fix | Delete
# r = RecordCollection.new
[32] Fix | Delete
# r.records = [4,5,6]
[33] Fix | Delete
# r.record_number(0) # => 4
[34] Fix | Delete
#
[35] Fix | Delete
# Further, if you wish to provide the methods #size, #<<, and #map,
[36] Fix | Delete
# all of which delegate to @records, this is how you can do it:
[37] Fix | Delete
#
[38] Fix | Delete
# class RecordCollection # re-open RecordCollection class
[39] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[40] Fix | Delete
# end
[41] Fix | Delete
#
[42] Fix | Delete
# r = RecordCollection.new
[43] Fix | Delete
# r.records = [1,2,3]
[44] Fix | Delete
# r.record_number(0) # => 1
[45] Fix | Delete
# r.size # => 3
[46] Fix | Delete
# r << 4 # => [1, 2, 3, 4]
[47] Fix | Delete
# r.map { |x| x * 2 } # => [2, 4, 6, 8]
[48] Fix | Delete
#
[49] Fix | Delete
# You can even extend regular objects with Forwardable.
[50] Fix | Delete
#
[51] Fix | Delete
# my_hash = Hash.new
[52] Fix | Delete
# my_hash.extend Forwardable # prepare object for delegation
[53] Fix | Delete
# my_hash.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
[54] Fix | Delete
# my_hash.puts "Howdy!"
[55] Fix | Delete
#
[56] Fix | Delete
# == Another example
[57] Fix | Delete
#
[58] Fix | Delete
# You could use Forwardable as an alternative to inheritance, when you don't want
[59] Fix | Delete
# to inherit all methods from the superclass. For instance, here is how you might
[60] Fix | Delete
# add a range of +Array+ instance methods to a new class +Queue+:
[61] Fix | Delete
#
[62] Fix | Delete
# class Queue
[63] Fix | Delete
# extend Forwardable
[64] Fix | Delete
#
[65] Fix | Delete
# def initialize
[66] Fix | Delete
# @q = [ ] # prepare delegate object
[67] Fix | Delete
# end
[68] Fix | Delete
#
[69] Fix | Delete
# # setup preferred interface, enq() and deq()...
[70] Fix | Delete
# def_delegator :@q, :push, :enq
[71] Fix | Delete
# def_delegator :@q, :shift, :deq
[72] Fix | Delete
#
[73] Fix | Delete
# # support some general Array methods that fit Queues well
[74] Fix | Delete
# def_delegators :@q, :clear, :first, :push, :shift, :size
[75] Fix | Delete
# end
[76] Fix | Delete
#
[77] Fix | Delete
# q = Thread::Queue.new
[78] Fix | Delete
# q.enq 1, 2, 3, 4, 5
[79] Fix | Delete
# q.push 6
[80] Fix | Delete
#
[81] Fix | Delete
# q.shift # => 1
[82] Fix | Delete
# while q.size > 0
[83] Fix | Delete
# puts q.deq
[84] Fix | Delete
# end
[85] Fix | Delete
#
[86] Fix | Delete
# q.enq "Ruby", "Perl", "Python"
[87] Fix | Delete
# puts q.first
[88] Fix | Delete
# q.clear
[89] Fix | Delete
# puts q.first
[90] Fix | Delete
#
[91] Fix | Delete
# This should output:
[92] Fix | Delete
#
[93] Fix | Delete
# 2
[94] Fix | Delete
# 3
[95] Fix | Delete
# 4
[96] Fix | Delete
# 5
[97] Fix | Delete
# 6
[98] Fix | Delete
# Ruby
[99] Fix | Delete
# nil
[100] Fix | Delete
#
[101] Fix | Delete
# == Notes
[102] Fix | Delete
#
[103] Fix | Delete
# Be advised, RDoc will not detect delegated methods.
[104] Fix | Delete
#
[105] Fix | Delete
# +forwardable.rb+ provides single-method delegation via the def_delegator and
[106] Fix | Delete
# def_delegators methods. For full-class delegation via DelegateClass, see
[107] Fix | Delete
# +delegate.rb+.
[108] Fix | Delete
#
[109] Fix | Delete
module Forwardable
[110] Fix | Delete
require 'forwardable/impl'
[111] Fix | Delete
[112] Fix | Delete
# Version of +forwardable.rb+
[113] Fix | Delete
VERSION = "1.3.2"
[114] Fix | Delete
FORWARDABLE_VERSION = VERSION
[115] Fix | Delete
[116] Fix | Delete
@debug = nil
[117] Fix | Delete
class << self
[118] Fix | Delete
# ignored
[119] Fix | Delete
attr_accessor :debug
[120] Fix | Delete
end
[121] Fix | Delete
[122] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[123] Fix | Delete
# symbols. These symbols correspond to method names, instance variable
[124] Fix | Delete
# names, or constant names (see def_delegator). The value is
[125] Fix | Delete
# the accessor to which the methods will be delegated.
[126] Fix | Delete
#
[127] Fix | Delete
# :call-seq:
[128] Fix | Delete
# delegate method => accessor
[129] Fix | Delete
# delegate [method, method, ...] => accessor
[130] Fix | Delete
#
[131] Fix | Delete
def instance_delegate(hash)
[132] Fix | Delete
hash.each do |methods, accessor|
[133] Fix | Delete
unless defined?(methods.each)
[134] Fix | Delete
def_instance_delegator(accessor, methods)
[135] Fix | Delete
else
[136] Fix | Delete
methods.each {|method| def_instance_delegator(accessor, method)}
[137] Fix | Delete
end
[138] Fix | Delete
end
[139] Fix | Delete
end
[140] Fix | Delete
[141] Fix | Delete
#
[142] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[143] Fix | Delete
# provision for using a different name. The following two code
[144] Fix | Delete
# samples have the same effect:
[145] Fix | Delete
#
[146] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[147] Fix | Delete
#
[148] Fix | Delete
# def_delegator :@records, :size
[149] Fix | Delete
# def_delegator :@records, :<<
[150] Fix | Delete
# def_delegator :@records, :map
[151] Fix | Delete
#
[152] Fix | Delete
def def_instance_delegators(accessor, *methods)
[153] Fix | Delete
methods.each do |method|
[154] Fix | Delete
next if /\A__(?:send|id)__\z/ =~ method
[155] Fix | Delete
def_instance_delegator(accessor, method)
[156] Fix | Delete
end
[157] Fix | Delete
end
[158] Fix | Delete
[159] Fix | Delete
# Define +method+ as delegator instance method with an optional
[160] Fix | Delete
# alias name +ali+. Method calls to +ali+ will be delegated to
[161] Fix | Delete
# +accessor.method+. +accessor+ should be a method name, instance
[162] Fix | Delete
# variable name, or constant name. Use the full path to the
[163] Fix | Delete
# constant if providing the constant name.
[164] Fix | Delete
# Returns the name of the method defined.
[165] Fix | Delete
#
[166] Fix | Delete
# class MyQueue
[167] Fix | Delete
# CONST = 1
[168] Fix | Delete
# extend Forwardable
[169] Fix | Delete
# attr_reader :queue
[170] Fix | Delete
# def initialize
[171] Fix | Delete
# @queue = []
[172] Fix | Delete
# end
[173] Fix | Delete
#
[174] Fix | Delete
# def_delegator :@queue, :push, :mypush
[175] Fix | Delete
# def_delegator 'MyQueue::CONST', :to_i
[176] Fix | Delete
# end
[177] Fix | Delete
#
[178] Fix | Delete
# q = MyQueue.new
[179] Fix | Delete
# q.mypush 42
[180] Fix | Delete
# q.queue #=> [42]
[181] Fix | Delete
# q.push 23 #=> NoMethodError
[182] Fix | Delete
# q.to_i #=> 1
[183] Fix | Delete
#
[184] Fix | Delete
def def_instance_delegator(accessor, method, ali = method)
[185] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[186] Fix | Delete
[187] Fix | Delete
# If it's not a class or module, it's an instance
[188] Fix | Delete
mod = Module === self ? self : singleton_class
[189] Fix | Delete
ret = mod.module_eval(&gen)
[190] Fix | Delete
mod.__send__(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
[191] Fix | Delete
ret
[192] Fix | Delete
end
[193] Fix | Delete
[194] Fix | Delete
alias delegate instance_delegate
[195] Fix | Delete
alias def_delegators def_instance_delegators
[196] Fix | Delete
alias def_delegator def_instance_delegator
[197] Fix | Delete
[198] Fix | Delete
# :nodoc:
[199] Fix | Delete
def self._delegator_method(obj, accessor, method, ali)
[200] Fix | Delete
accessor = accessor.to_s unless Symbol === accessor
[201] Fix | Delete
[202] Fix | Delete
if Module === obj ?
[203] Fix | Delete
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
[204] Fix | Delete
obj.respond_to?(accessor, true)
[205] Fix | Delete
accessor = "#{accessor}()"
[206] Fix | Delete
end
[207] Fix | Delete
[208] Fix | Delete
method_call = ".__send__(:#{method}, *args, &block)"
[209] Fix | Delete
if _valid_method?(method)
[210] Fix | Delete
loc, = caller_locations(2,1)
[211] Fix | Delete
pre = "_ ="
[212] Fix | Delete
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
[213] Fix | Delete
method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}"
[214] Fix | Delete
begin;
[215] Fix | Delete
unless defined? _.#{method}
[216] Fix | Delete
::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
[217] Fix | Delete
_#{method_call}
[218] Fix | Delete
else
[219] Fix | Delete
_.#{method}(*args, &block)
[220] Fix | Delete
end
[221] Fix | Delete
end;
[222] Fix | Delete
end
[223] Fix | Delete
[224] Fix | Delete
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
[225] Fix | Delete
begin;
[226] Fix | Delete
proc do
[227] Fix | Delete
def #{ali}(*args, &block)
[228] Fix | Delete
#{pre}
[229] Fix | Delete
begin
[230] Fix | Delete
#{accessor}
[231] Fix | Delete
end#{method_call}
[232] Fix | Delete
end
[233] Fix | Delete
end
[234] Fix | Delete
end;
[235] Fix | Delete
end
[236] Fix | Delete
end
[237] Fix | Delete
[238] Fix | Delete
# SingleForwardable can be used to setup delegation at the object level as well.
[239] Fix | Delete
#
[240] Fix | Delete
# printer = String.new
[241] Fix | Delete
# printer.extend SingleForwardable # prepare object for delegation
[242] Fix | Delete
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
[243] Fix | Delete
# printer.puts "Howdy!"
[244] Fix | Delete
#
[245] Fix | Delete
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
[246] Fix | Delete
#
[247] Fix | Delete
# class Implementation
[248] Fix | Delete
# def self.service
[249] Fix | Delete
# puts "serviced!"
[250] Fix | Delete
# end
[251] Fix | Delete
# end
[252] Fix | Delete
#
[253] Fix | Delete
# module Facade
[254] Fix | Delete
# extend SingleForwardable
[255] Fix | Delete
# def_delegator :Implementation, :service
[256] Fix | Delete
# end
[257] Fix | Delete
#
[258] Fix | Delete
# Facade.service #=> serviced!
[259] Fix | Delete
#
[260] Fix | Delete
# If you want to use both Forwardable and SingleForwardable, you can
[261] Fix | Delete
# use methods def_instance_delegator and def_single_delegator, etc.
[262] Fix | Delete
module SingleForwardable
[263] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[264] Fix | Delete
# symbols. These symbols correspond to method names. The value is
[265] Fix | Delete
# the accessor to which the methods will be delegated.
[266] Fix | Delete
#
[267] Fix | Delete
# :call-seq:
[268] Fix | Delete
# delegate method => accessor
[269] Fix | Delete
# delegate [method, method, ...] => accessor
[270] Fix | Delete
#
[271] Fix | Delete
def single_delegate(hash)
[272] Fix | Delete
hash.each do |methods, accessor|
[273] Fix | Delete
unless defined?(methods.each)
[274] Fix | Delete
def_single_delegator(accessor, methods)
[275] Fix | Delete
else
[276] Fix | Delete
methods.each {|method| def_single_delegator(accessor, method)}
[277] Fix | Delete
end
[278] Fix | Delete
end
[279] Fix | Delete
end
[280] Fix | Delete
[281] Fix | Delete
#
[282] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[283] Fix | Delete
# provision for using a different name. The following two code
[284] Fix | Delete
# samples have the same effect:
[285] Fix | Delete
#
[286] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[287] Fix | Delete
#
[288] Fix | Delete
# def_delegator :@records, :size
[289] Fix | Delete
# def_delegator :@records, :<<
[290] Fix | Delete
# def_delegator :@records, :map
[291] Fix | Delete
#
[292] Fix | Delete
def def_single_delegators(accessor, *methods)
[293] Fix | Delete
methods.each do |method|
[294] Fix | Delete
next if /\A__(?:send|id)__\z/ =~ method
[295] Fix | Delete
def_single_delegator(accessor, method)
[296] Fix | Delete
end
[297] Fix | Delete
end
[298] Fix | Delete
[299] Fix | Delete
# :call-seq:
[300] Fix | Delete
# def_single_delegator(accessor, method, new_name=method)
[301] Fix | Delete
#
[302] Fix | Delete
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
[303] Fix | Delete
# the method of the same name in _accessor_). If _new_name_ is
[304] Fix | Delete
# provided, it is used as the name for the delegate method.
[305] Fix | Delete
# Returns the name of the method defined.
[306] Fix | Delete
def def_single_delegator(accessor, method, ali = method)
[307] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[308] Fix | Delete
[309] Fix | Delete
ret = instance_eval(&gen)
[310] Fix | Delete
singleton_class.__send__(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
[311] Fix | Delete
ret
[312] Fix | Delete
end
[313] Fix | Delete
[314] Fix | Delete
alias delegate single_delegate
[315] Fix | Delete
alias def_delegators def_single_delegators
[316] Fix | Delete
alias def_delegator def_single_delegator
[317] Fix | Delete
end
[318] Fix | Delete
[319] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function