Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../proc/self/root/usr/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: 61155 $
[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
# We want to rely on what has come before obviously, but with delegation we can
[59] Fix | Delete
# take just the methods we need and even rename them as appropriate. In many
[60] Fix | Delete
# cases this is preferable to inheritance, which gives us the entire old
[61] Fix | Delete
# interface, even if much of it isn't needed.
[62] Fix | Delete
#
[63] Fix | Delete
# class Queue
[64] Fix | Delete
# extend Forwardable
[65] Fix | Delete
#
[66] Fix | Delete
# def initialize
[67] Fix | Delete
# @q = [ ] # prepare delegate object
[68] Fix | Delete
# end
[69] Fix | Delete
#
[70] Fix | Delete
# # setup preferred interface, enq() and deq()...
[71] Fix | Delete
# def_delegator :@q, :push, :enq
[72] Fix | Delete
# def_delegator :@q, :shift, :deq
[73] Fix | Delete
#
[74] Fix | Delete
# # support some general Array methods that fit Queues well
[75] Fix | Delete
# def_delegators :@q, :clear, :first, :push, :shift, :size
[76] Fix | Delete
# end
[77] Fix | Delete
#
[78] Fix | Delete
# q = Queue.new
[79] Fix | Delete
# q.enq 1, 2, 3, 4, 5
[80] Fix | Delete
# q.push 6
[81] Fix | Delete
#
[82] Fix | Delete
# q.shift # => 1
[83] Fix | Delete
# while q.size > 0
[84] Fix | Delete
# puts q.deq
[85] Fix | Delete
# end
[86] Fix | Delete
#
[87] Fix | Delete
# q.enq "Ruby", "Perl", "Python"
[88] Fix | Delete
# puts q.first
[89] Fix | Delete
# q.clear
[90] Fix | Delete
# puts q.first
[91] Fix | Delete
#
[92] Fix | Delete
# This should output:
[93] Fix | Delete
#
[94] Fix | Delete
# 2
[95] Fix | Delete
# 3
[96] Fix | Delete
# 4
[97] Fix | Delete
# 5
[98] Fix | Delete
# 6
[99] Fix | Delete
# Ruby
[100] Fix | Delete
# nil
[101] Fix | Delete
#
[102] Fix | Delete
# == Notes
[103] Fix | Delete
#
[104] Fix | Delete
# Be advised, RDoc will not detect delegated methods.
[105] Fix | Delete
#
[106] Fix | Delete
# +forwardable.rb+ provides single-method delegation via the def_delegator and
[107] Fix | Delete
# def_delegators methods. For full-class delegation via DelegateClass, see
[108] Fix | Delete
# +delegate.rb+.
[109] Fix | Delete
#
[110] Fix | Delete
module Forwardable
[111] Fix | Delete
require 'forwardable/impl'
[112] Fix | Delete
[113] Fix | Delete
# Version of +forwardable.rb+
[114] Fix | Delete
FORWARDABLE_VERSION = "1.2.0"
[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. The value is
[124] Fix | Delete
# the accessor to which the methods will be delegated.
[125] Fix | Delete
#
[126] Fix | Delete
# :call-seq:
[127] Fix | Delete
# delegate method => accessor
[128] Fix | Delete
# delegate [method, method, ...] => accessor
[129] Fix | Delete
#
[130] Fix | Delete
def instance_delegate(hash)
[131] Fix | Delete
hash.each do |methods, accessor|
[132] Fix | Delete
unless defined?(methods.each)
[133] Fix | Delete
def_instance_delegator(accessor, methods)
[134] Fix | Delete
else
[135] Fix | Delete
methods.each {|method| def_instance_delegator(accessor, method)}
[136] Fix | Delete
end
[137] Fix | Delete
end
[138] Fix | Delete
end
[139] Fix | Delete
[140] Fix | Delete
#
[141] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[142] Fix | Delete
# provision for using a different name. The following two code
[143] Fix | Delete
# samples have the same effect:
[144] Fix | Delete
#
[145] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[146] Fix | Delete
#
[147] Fix | Delete
# def_delegator :@records, :size
[148] Fix | Delete
# def_delegator :@records, :<<
[149] Fix | Delete
# def_delegator :@records, :map
[150] Fix | Delete
#
[151] Fix | Delete
def def_instance_delegators(accessor, *methods)
[152] Fix | Delete
methods.delete("__send__")
[153] Fix | Delete
methods.delete("__id__")
[154] Fix | Delete
for method in methods
[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+.
[162] Fix | Delete
#
[163] Fix | Delete
# class MyQueue
[164] Fix | Delete
# extend Forwardable
[165] Fix | Delete
# attr_reader :queue
[166] Fix | Delete
# def initialize
[167] Fix | Delete
# @queue = []
[168] Fix | Delete
# end
[169] Fix | Delete
#
[170] Fix | Delete
# def_delegator :@queue, :push, :mypush
[171] Fix | Delete
# end
[172] Fix | Delete
#
[173] Fix | Delete
# q = MyQueue.new
[174] Fix | Delete
# q.mypush 42
[175] Fix | Delete
# q.queue #=> [42]
[176] Fix | Delete
# q.push 23 #=> NoMethodError
[177] Fix | Delete
#
[178] Fix | Delete
def def_instance_delegator(accessor, method, ali = method)
[179] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[180] Fix | Delete
[181] Fix | Delete
# If it's not a class or module, it's an instance
[182] Fix | Delete
(Module === self ? self : singleton_class).module_eval(&gen)
[183] Fix | Delete
end
[184] Fix | Delete
[185] Fix | Delete
alias delegate instance_delegate
[186] Fix | Delete
alias def_delegators def_instance_delegators
[187] Fix | Delete
alias def_delegator def_instance_delegator
[188] Fix | Delete
[189] Fix | Delete
# :nodoc:
[190] Fix | Delete
def self._delegator_method(obj, accessor, method, ali)
[191] Fix | Delete
accessor = accessor.to_s unless Symbol === accessor
[192] Fix | Delete
[193] Fix | Delete
if Module === obj ?
[194] Fix | Delete
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
[195] Fix | Delete
obj.respond_to?(accessor, true)
[196] Fix | Delete
accessor = "#{accessor}()"
[197] Fix | Delete
end
[198] Fix | Delete
[199] Fix | Delete
method_call = ".__send__(:#{method}, *args, &block)"
[200] Fix | Delete
if _valid_method?(method)
[201] Fix | Delete
loc, = caller_locations(2,1)
[202] Fix | Delete
pre = "_ ="
[203] Fix | Delete
mesg = "#{Module === obj ? obj : obj.class}\##{ali} at #{loc.path}:#{loc.lineno} forwarding to private method "
[204] Fix | Delete
method_call = "#{<<-"begin;"}\n#{<<-"end;".chomp}"
[205] Fix | Delete
begin;
[206] Fix | Delete
unless defined? _.#{method}
[207] Fix | Delete
::Kernel.warn #{mesg.dump}"\#{_.class}"'##{method}', uplevel: 1
[208] Fix | Delete
_#{method_call}
[209] Fix | Delete
else
[210] Fix | Delete
_.#{method}(*args, &block)
[211] Fix | Delete
end
[212] Fix | Delete
end;
[213] Fix | Delete
end
[214] Fix | Delete
[215] Fix | Delete
_compile_method("#{<<-"begin;"}\n#{<<-"end;"}", __FILE__, __LINE__+1)
[216] Fix | Delete
begin;
[217] Fix | Delete
proc do
[218] Fix | Delete
def #{ali}(*args, &block)
[219] Fix | Delete
#{pre}
[220] Fix | Delete
begin
[221] Fix | Delete
#{accessor}
[222] Fix | Delete
end#{method_call}#{FILTER_EXCEPTION}
[223] Fix | Delete
end
[224] Fix | Delete
end
[225] Fix | Delete
end;
[226] Fix | Delete
end
[227] Fix | Delete
end
[228] Fix | Delete
[229] Fix | Delete
# SingleForwardable can be used to setup delegation at the object level as well.
[230] Fix | Delete
#
[231] Fix | Delete
# printer = String.new
[232] Fix | Delete
# printer.extend SingleForwardable # prepare object for delegation
[233] Fix | Delete
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
[234] Fix | Delete
# printer.puts "Howdy!"
[235] Fix | Delete
#
[236] Fix | Delete
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
[237] Fix | Delete
#
[238] Fix | Delete
# class Implementation
[239] Fix | Delete
# def self.service
[240] Fix | Delete
# puts "serviced!"
[241] Fix | Delete
# end
[242] Fix | Delete
# end
[243] Fix | Delete
#
[244] Fix | Delete
# module Facade
[245] Fix | Delete
# extend SingleForwardable
[246] Fix | Delete
# def_delegator :Implementation, :service
[247] Fix | Delete
# end
[248] Fix | Delete
#
[249] Fix | Delete
# Facade.service #=> serviced!
[250] Fix | Delete
#
[251] Fix | Delete
# If you want to use both Forwardable and SingleForwardable, you can
[252] Fix | Delete
# use methods def_instance_delegator and def_single_delegator, etc.
[253] Fix | Delete
module SingleForwardable
[254] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[255] Fix | Delete
# symbols. These symbols correspond to method names. The value is
[256] Fix | Delete
# the accessor to which the methods will be delegated.
[257] Fix | Delete
#
[258] Fix | Delete
# :call-seq:
[259] Fix | Delete
# delegate method => accessor
[260] Fix | Delete
# delegate [method, method, ...] => accessor
[261] Fix | Delete
#
[262] Fix | Delete
def single_delegate(hash)
[263] Fix | Delete
hash.each do |methods, accessor|
[264] Fix | Delete
unless defined?(methods.each)
[265] Fix | Delete
def_single_delegator(accessor, methods)
[266] Fix | Delete
else
[267] Fix | Delete
methods.each {|method| def_single_delegator(accessor, method)}
[268] Fix | Delete
end
[269] Fix | Delete
end
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
#
[273] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[274] Fix | Delete
# provision for using a different name. The following two code
[275] Fix | Delete
# samples have the same effect:
[276] Fix | Delete
#
[277] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[278] Fix | Delete
#
[279] Fix | Delete
# def_delegator :@records, :size
[280] Fix | Delete
# def_delegator :@records, :<<
[281] Fix | Delete
# def_delegator :@records, :map
[282] Fix | Delete
#
[283] Fix | Delete
def def_single_delegators(accessor, *methods)
[284] Fix | Delete
methods.delete("__send__")
[285] Fix | Delete
methods.delete("__id__")
[286] Fix | Delete
for method in methods
[287] Fix | Delete
def_single_delegator(accessor, method)
[288] Fix | Delete
end
[289] Fix | Delete
end
[290] Fix | Delete
[291] Fix | Delete
# :call-seq:
[292] Fix | Delete
# def_single_delegator(accessor, method, new_name=method)
[293] Fix | Delete
#
[294] Fix | Delete
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
[295] Fix | Delete
# the method of the same name in _accessor_). If _new_name_ is
[296] Fix | Delete
# provided, it is used as the name for the delegate method.
[297] Fix | Delete
def def_single_delegator(accessor, method, ali = method)
[298] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[299] Fix | Delete
[300] Fix | Delete
instance_eval(&gen)
[301] Fix | Delete
end
[302] Fix | Delete
[303] Fix | Delete
alias delegate single_delegate
[304] Fix | Delete
alias def_delegators def_single_delegators
[305] Fix | Delete
alias def_delegator def_single_delegator
[306] Fix | Delete
end
[307] Fix | Delete
[308] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function