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