Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/ExeBy/exe_root.../opt/alt/ruby22/lib64/ruby/2.2.0
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: 58085 $
[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
# Version of +forwardable.rb+
[112] Fix | Delete
FORWARDABLE_VERSION = "1.1.0"
[113] Fix | Delete
[114] Fix | Delete
FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"
[115] Fix | Delete
[116] Fix | Delete
@debug = nil
[117] Fix | Delete
class << self
[118] Fix | Delete
# If true, <tt>__FILE__</tt> will remain in the backtrace in the event an
[119] Fix | Delete
# Exception is raised.
[120] Fix | Delete
attr_accessor :debug
[121] Fix | Delete
end
[122] Fix | Delete
[123] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[124] Fix | Delete
# symbols. These symbols correspond to method names. 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{ |methods, accessor|
[133] Fix | Delete
methods = [methods] unless methods.respond_to?(:each)
[134] Fix | Delete
methods.each{ |method|
[135] Fix | Delete
def_instance_delegator(accessor, method)
[136] Fix | Delete
}
[137] Fix | Delete
}
[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
def self._delegator_method(obj, accessor, method, ali)
[190] Fix | Delete
accessor = accessor.to_s unless Symbol === accessor
[191] Fix | Delete
[192] Fix | Delete
if Module === obj ?
[193] Fix | Delete
obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
[194] Fix | Delete
obj.respond_to?(accessor, true)
[195] Fix | Delete
accessor = "#{accessor}()"
[196] Fix | Delete
end
[197] Fix | Delete
[198] Fix | Delete
line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}"
[199] Fix | Delete
begin;
[200] Fix | Delete
proc do
[201] Fix | Delete
def #{ali}(*args, &block)
[202] Fix | Delete
begin
[203] Fix | Delete
#{accessor}
[204] Fix | Delete
ensure
[205] Fix | Delete
$@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug
[206] Fix | Delete
end.__send__ :#{method}, *args, &block
[207] Fix | Delete
end
[208] Fix | Delete
end
[209] Fix | Delete
end;
[210] Fix | Delete
[211] Fix | Delete
RubyVM::InstructionSequence
[212] Fix | Delete
.compile(str, __FILE__, __FILE__, line_no,
[213] Fix | Delete
trace_instruction: false,
[214] Fix | Delete
tailcall_optimization: true)
[215] Fix | Delete
.eval
[216] Fix | Delete
end
[217] Fix | Delete
end
[218] Fix | Delete
[219] Fix | Delete
# SingleForwardable can be used to setup delegation at the object level as well.
[220] Fix | Delete
#
[221] Fix | Delete
# printer = String.new
[222] Fix | Delete
# printer.extend SingleForwardable # prepare object for delegation
[223] Fix | Delete
# printer.def_delegator "STDOUT", "puts" # add delegation for STDOUT.puts()
[224] Fix | Delete
# printer.puts "Howdy!"
[225] Fix | Delete
#
[226] Fix | Delete
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
[227] Fix | Delete
#
[228] Fix | Delete
# class Implementation
[229] Fix | Delete
# def self.service
[230] Fix | Delete
# puts "serviced!"
[231] Fix | Delete
# end
[232] Fix | Delete
# end
[233] Fix | Delete
#
[234] Fix | Delete
# module Facade
[235] Fix | Delete
# extend SingleForwardable
[236] Fix | Delete
# def_delegator :Implementation, :service
[237] Fix | Delete
# end
[238] Fix | Delete
#
[239] Fix | Delete
# Facade.service #=> serviced!
[240] Fix | Delete
#
[241] Fix | Delete
# If you want to use both Forwardable and SingleForwardable, you can
[242] Fix | Delete
# use methods def_instance_delegator and def_single_delegator, etc.
[243] Fix | Delete
module SingleForwardable
[244] Fix | Delete
# Takes a hash as its argument. The key is a symbol or an array of
[245] Fix | Delete
# symbols. These symbols correspond to method names. The value is
[246] Fix | Delete
# the accessor to which the methods will be delegated.
[247] Fix | Delete
#
[248] Fix | Delete
# :call-seq:
[249] Fix | Delete
# delegate method => accessor
[250] Fix | Delete
# delegate [method, method, ...] => accessor
[251] Fix | Delete
#
[252] Fix | Delete
def single_delegate(hash)
[253] Fix | Delete
hash.each{ |methods, accessor|
[254] Fix | Delete
methods = [methods] unless methods.respond_to?(:each)
[255] Fix | Delete
methods.each{ |method|
[256] Fix | Delete
def_single_delegator(accessor, method)
[257] Fix | Delete
}
[258] Fix | Delete
}
[259] Fix | Delete
end
[260] Fix | Delete
[261] Fix | Delete
#
[262] Fix | Delete
# Shortcut for defining multiple delegator methods, but with no
[263] Fix | Delete
# provision for using a different name. The following two code
[264] Fix | Delete
# samples have the same effect:
[265] Fix | Delete
#
[266] Fix | Delete
# def_delegators :@records, :size, :<<, :map
[267] Fix | Delete
#
[268] Fix | Delete
# def_delegator :@records, :size
[269] Fix | Delete
# def_delegator :@records, :<<
[270] Fix | Delete
# def_delegator :@records, :map
[271] Fix | Delete
#
[272] Fix | Delete
def def_single_delegators(accessor, *methods)
[273] Fix | Delete
methods.delete("__send__")
[274] Fix | Delete
methods.delete("__id__")
[275] Fix | Delete
for method in methods
[276] Fix | Delete
def_single_delegator(accessor, method)
[277] Fix | Delete
end
[278] Fix | Delete
end
[279] Fix | Delete
[280] Fix | Delete
# :call-seq:
[281] Fix | Delete
# def_single_delegator(accessor, method, new_name=method)
[282] Fix | Delete
#
[283] Fix | Delete
# Defines a method _method_ which delegates to _accessor_ (i.e. it calls
[284] Fix | Delete
# the method of the same name in _accessor_). If _new_name_ is
[285] Fix | Delete
# provided, it is used as the name for the delegate method.
[286] Fix | Delete
def def_single_delegator(accessor, method, ali = method)
[287] Fix | Delete
gen = Forwardable._delegator_method(self, accessor, method, ali)
[288] Fix | Delete
[289] Fix | Delete
instance_eval(&gen)
[290] Fix | Delete
end
[291] Fix | Delete
[292] Fix | Delete
alias delegate single_delegate
[293] Fix | Delete
alias def_delegators def_single_delegators
[294] Fix | Delete
alias def_delegator def_single_delegator
[295] Fix | Delete
end
[296] Fix | Delete
[297] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function