Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby32/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.3"
[114] Fix | Delete
VERSION.freeze
[115] Fix | Delete
FORWARDABLE_VERSION = VERSION
[116] Fix | Delete
FORWARDABLE_VERSION.freeze
[117] Fix | Delete
[118] Fix | Delete
@debug = nil
[119] Fix | Delete
class << self
[120] Fix | Delete
# ignored
[121] Fix | Delete
attr_accessor :debug
[122] Fix | Delete
end
[123] Fix | Delete
[124] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[125] Fix | Delete
# symbols. These symbols correspond to method names, instance variable
[126] Fix | Delete
# names, or constant names (see def_delegator). The value is
[127] Fix | Delete
# the accessor to which the methods will be delegated.
[128] Fix | Delete
#
[129] Fix | Delete
# :call-seq:
[130] Fix | Delete
# delegate method => accessor
[131] Fix | Delete
# delegate [method, method, ...] => accessor
[132] Fix | Delete
#
[133] Fix | Delete
def instance_delegate(hash)
[134] Fix | Delete
hash.each do |methods, accessor|
[135] Fix | Delete
unless defined?(methods.each)
[136] Fix | Delete
def_instance_delegator(accessor, methods)
[137] Fix | Delete
else
[138] Fix | Delete
methods.each {|method| def_instance_delegator(accessor, method)}
[139] Fix | Delete
end
[140] Fix | Delete
end
[141] Fix | Delete
end
[142] Fix | Delete
[143] Fix | Delete
#
[144] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[145] Fix | Delete
# provision for using a different name. The following two code
[146] Fix | Delete
# samples have the same effect:
[147] Fix | Delete
#
[148] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[149] Fix | Delete
#
[150] Fix | Delete
# def_delegator :@records, :size
[151] Fix | Delete
# def_delegator :@records, :<<
[152] Fix | Delete
# def_delegator :@records, :map
[153] Fix | Delete
#
[154] Fix | Delete
def def_instance_delegators(accessor, *methods)
[155] Fix | Delete
methods.each do |method|
[156] Fix | Delete
next if /\A__(?:send|id)__\z/ =~ method
[157] Fix | Delete
def_instance_delegator(accessor, method)
[158] Fix | Delete
end
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
# Define +method+ as delegator instance method with an optional
[162] Fix | Delete
# alias name +ali+. Method calls to +ali+ will be delegated to
[163] Fix | Delete
# +accessor.method+. +accessor+ should be a method name, instance
[164] Fix | Delete
# variable name, or constant name. Use the full path to the
[165] Fix | Delete
# constant if providing the constant name.
[166] Fix | Delete
# Returns the name of the method defined.
[167] Fix | Delete
#
[168] Fix | Delete
# class MyQueue
[169] Fix | Delete
# CONST = 1
[170] Fix | Delete
# extend Forwardable
[171] Fix | Delete
# attr_reader :queue
[172] Fix | Delete
# def initialize
[173] Fix | Delete
# @queue = []
[174] Fix | Delete
# end
[175] Fix | Delete
#
[176] Fix | Delete
# def_delegator :@queue, :push, :mypush
[177] Fix | Delete
# def_delegator 'MyQueue::CONST', :to_i
[178] Fix | Delete
# end
[179] Fix | Delete
#
[180] Fix | Delete
# q = MyQueue.new
[181] Fix | Delete
# q.mypush 42
[182] Fix | Delete
# q.queue #=> [42]
[183] Fix | Delete
# q.push 23 #=> NoMethodError
[184] Fix | Delete
# q.to_i #=> 1
[185] Fix | Delete
#
[186] Fix | Delete
def def_instance_delegator(accessor, method, ali = method)
[187] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[188] Fix | Delete
[189] Fix | Delete
# If it's not a class or module, it's an instance
[190] Fix | Delete
mod = Module === self ? self : singleton_class
[191] Fix | Delete
ret = mod.module_eval(&gen)
[192] Fix | Delete
mod.__send__(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
[193] Fix | Delete
ret
[194] Fix | Delete
end
[195] Fix | Delete
[196] Fix | Delete
alias delegate instance_delegate
[197] Fix | Delete
alias def_delegators def_instance_delegators
[198] Fix | Delete
alias def_delegator def_instance_delegator
[199] Fix | Delete
[200] Fix | Delete
# :nodoc:
[201] Fix | Delete
def self._delegator_method(obj, accessor, method, ali)
[202] Fix | Delete
accessor = accessor.to_s unless Symbol === accessor
[203] Fix | Delete
[204] Fix | Delete
if Module === obj ?
[205] Fix | Delete
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
[206] Fix | Delete
obj.respond_to?(accessor, true)
[207] Fix | Delete
accessor = "#{accessor}()"
[208] Fix | Delete
end
[209] Fix | Delete
[210] Fix | Delete
method_call = ".__send__(:#{method}, *args, &block)"
[211] Fix | Delete
if _valid_method?(method)
[212] Fix | Delete
loc, = caller_locations(2,1)
[213] Fix | Delete
pre = "_ ="
[214] Fix | Delete
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
[215] Fix | Delete
method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}"
[216] Fix | Delete
begin;
[217] Fix | Delete
unless defined? _.#{method}
[218] Fix | Delete
::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
[219] Fix | Delete
_#{method_call}
[220] Fix | Delete
else
[221] Fix | Delete
_.#{method}(*args, &block)
[222] Fix | Delete
end
[223] Fix | Delete
end;
[224] Fix | Delete
end
[225] Fix | Delete
[226] Fix | Delete
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
[227] Fix | Delete
begin;
[228] Fix | Delete
proc do
[229] Fix | Delete
def #{ali}(*args, &block)
[230] Fix | Delete
#{pre}
[231] Fix | Delete
begin
[232] Fix | Delete
#{accessor}
[233] Fix | Delete
end#{method_call}
[234] Fix | Delete
end
[235] Fix | Delete
end
[236] Fix | Delete
end;
[237] Fix | Delete
end
[238] Fix | Delete
end
[239] Fix | Delete
[240] Fix | Delete
# SingleForwardable can be used to setup delegation at the object level as well.
[241] Fix | Delete
#
[242] Fix | Delete
# printer = String.new
[243] Fix | Delete
# printer.extend SingleForwardable # prepare object for delegation
[244] Fix | Delete
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
[245] Fix | Delete
# printer.puts "Howdy!"
[246] Fix | Delete
#
[247] Fix | Delete
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
[248] Fix | Delete
#
[249] Fix | Delete
# class Implementation
[250] Fix | Delete
# def self.service
[251] Fix | Delete
# puts "serviced!"
[252] Fix | Delete
# end
[253] Fix | Delete
# end
[254] Fix | Delete
#
[255] Fix | Delete
# module Facade
[256] Fix | Delete
# extend SingleForwardable
[257] Fix | Delete
# def_delegator :Implementation, :service
[258] Fix | Delete
# end
[259] Fix | Delete
#
[260] Fix | Delete
# Facade.service #=> serviced!
[261] Fix | Delete
#
[262] Fix | Delete
# If you want to use both Forwardable and SingleForwardable, you can
[263] Fix | Delete
# use methods def_instance_delegator and def_single_delegator, etc.
[264] Fix | Delete
module SingleForwardable
[265] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[266] Fix | Delete
# symbols. These symbols correspond to method names. The value is
[267] Fix | Delete
# the accessor to which the methods will be delegated.
[268] Fix | Delete
#
[269] Fix | Delete
# :call-seq:
[270] Fix | Delete
# delegate method => accessor
[271] Fix | Delete
# delegate [method, method, ...] => accessor
[272] Fix | Delete
#
[273] Fix | Delete
def single_delegate(hash)
[274] Fix | Delete
hash.each do |methods, accessor|
[275] Fix | Delete
unless defined?(methods.each)
[276] Fix | Delete
def_single_delegator(accessor, methods)
[277] Fix | Delete
else
[278] Fix | Delete
methods.each {|method| def_single_delegator(accessor, method)}
[279] Fix | Delete
end
[280] Fix | Delete
end
[281] Fix | Delete
end
[282] Fix | Delete
[283] Fix | Delete
#
[284] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[285] Fix | Delete
# provision for using a different name. The following two code
[286] Fix | Delete
# samples have the same effect:
[287] Fix | Delete
#
[288] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[289] Fix | Delete
#
[290] Fix | Delete
# def_delegator :@records, :size
[291] Fix | Delete
# def_delegator :@records, :<<
[292] Fix | Delete
# def_delegator :@records, :map
[293] Fix | Delete
#
[294] Fix | Delete
def def_single_delegators(accessor, *methods)
[295] Fix | Delete
methods.each do |method|
[296] Fix | Delete
next if /\A__(?:send|id)__\z/ =~ method
[297] Fix | Delete
def_single_delegator(accessor, method)
[298] Fix | Delete
end
[299] Fix | Delete
end
[300] Fix | Delete
[301] Fix | Delete
# :call-seq:
[302] Fix | Delete
# def_single_delegator(accessor, method, new_name=method)
[303] Fix | Delete
#
[304] Fix | Delete
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
[305] Fix | Delete
# the method of the same name in _accessor_). If _new_name_ is
[306] Fix | Delete
# provided, it is used as the name for the delegate method.
[307] Fix | Delete
# Returns the name of the method defined.
[308] Fix | Delete
def def_single_delegator(accessor, method, ali = method)
[309] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[310] Fix | Delete
[311] Fix | Delete
ret = instance_eval(&gen)
[312] Fix | Delete
singleton_class.__send__(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7'
[313] Fix | Delete
ret
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
alias delegate single_delegate
[317] Fix | Delete
alias def_delegators def_single_delegators
[318] Fix | Delete
alias def_delegator def_single_delegator
[319] Fix | Delete
end
[320] Fix | Delete
[321] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function