PHP 8.2.31
Preview: forwardable.rb Size: 7.97 KB
/proc/thread-self/root/opt/alt/ruby23/lib64/ruby/2.3.0/forwardable.rb

# frozen_string_literal: false
#
#   forwardable.rb -
#       $Release Version: 1.1$
#       $Revision: 62820 $
#       by Keiju ISHITSUKA(keiju@ishitsuka.com)
#       original definition by delegator.rb
#       Revised by Daniel J. Berger with suggestions from Florian Gross.
#
#       Documentation by James Edward Gray II and Gavin Sinclair



# The Forwardable module provides delegation of specified
# methods to a designated object, using the methods #def_delegator
# and #def_delegators.
#
# For example, say you have a class RecordCollection which
# contains an array <tt>@records</tt>.  You could provide the lookup method
# #record_number(), which simply calls #[] on the <tt>@records</tt>
# array, like this:
#
#   require 'forwardable'
#
#   class RecordCollection
#     attr_accessor :records
#     extend Forwardable
#     def_delegator :@records, :[], :record_number
#   end
#
# We can use the lookup method like so:
#
#   r = RecordCollection.new
#   r.records = [4,5,6]
#   r.record_number(0)  # => 4
#
# Further, if you wish to provide the methods #size, #<<, and #map,
# all of which delegate to @records, this is how you can do it:
#
#   class RecordCollection # re-open RecordCollection class
#     def_delegators :@records, :size, :<<, :map
#   end
#
#   r = RecordCollection.new
#   r.records = [1,2,3]
#   r.record_number(0)   # => 1
#   r.size               # => 3
#   r << 4               # => [1, 2, 3, 4]
#   r.map { |x| x * 2 }  # => [2, 4, 6, 8]
#
# You can even extend regular objects with Forwardable.
#
#   my_hash = Hash.new
#   my_hash.extend Forwardable              # prepare object for delegation
#   my_hash.def_delegator "STDOUT", "puts"  # add delegation for STDOUT.puts()
#   my_hash.puts "Howdy!"
#
# == Another example
#
# We want to rely on what has come before obviously, but with delegation we can
# take just the methods we need and even rename them as appropriate.  In many
# cases this is preferable to inheritance, which gives us the entire old
# interface, even if much of it isn't needed.
#
#   class Queue
#     extend Forwardable
#
#     def initialize
#       @q = [ ]    # prepare delegate object
#     end
#
#     # setup preferred interface, enq() and deq()...
#     def_delegator :@q, :push, :enq
#     def_delegator :@q, :shift, :deq
#
#     # support some general Array methods that fit Queues well
#     def_delegators :@q, :clear, :first, :push, :shift, :size
#   end
#
#   q = Queue.new
#   q.enq 1, 2, 3, 4, 5
#   q.push 6
#
#   q.shift    # => 1
#   while q.size > 0
#     puts q.deq
#   end
#
#   q.enq "Ruby", "Perl", "Python"
#   puts q.first
#   q.clear
#   puts q.first
#
# This should output:
#
#   2
#   3
#   4
#   5
#   6
#   Ruby
#   nil
#
# == Notes
#
# Be advised, RDoc will not detect delegated methods.
#
# +forwardable.rb+ provides single-method delegation via the def_delegator and
# def_delegators methods. For full-class delegation via DelegateClass, see
# +delegate.rb+.
#
module Forwardable
  # Version of +forwardable.rb+
  FORWARDABLE_VERSION = "1.1.0"

  @debug = nil
  class << self
    # ignored
    attr_accessor :debug
  end

  # Takes a hash as its argument.  The key is a symbol or an array of
  # symbols.  These symbols correspond to method names.  The value is
  # the accessor to which the methods will be delegated.
  #
  # :call-seq:
  #    delegate method => accessor
  #    delegate [method, method, ...] => accessor
  #
  def instance_delegate(hash)
    hash.each{ |methods, accessor|
      methods = [methods] unless methods.respond_to?(:each)
      methods.each{ |method|
        def_instance_delegator(accessor, method)
      }
    }
  end

  #
  # Shortcut for defining multiple delegator methods, but with no
  # provision for using a different name.  The following two code
  # samples have the same effect:
  #
  #   def_delegators :@records, :size, :<<, :map
  #
  #   def_delegator :@records, :size
  #   def_delegator :@records, :<<
  #   def_delegator :@records, :map
  #
  def def_instance_delegators(accessor, *methods)
    methods.delete("__send__")
    methods.delete("__id__")
    for method in methods
      def_instance_delegator(accessor, method)
    end
  end

  # Define +method+ as delegator instance method with an optional
  # alias name +ali+. Method calls to +ali+ will be delegated to
  # +accessor.method+.
  #
  #   class MyQueue
  #     extend Forwardable
  #     attr_reader :queue
  #     def initialize
  #       @queue = []
  #     end
  #
  #     def_delegator :@queue, :push, :mypush
  #   end
  #
  #   q = MyQueue.new
  #   q.mypush 42
  #   q.queue    #=> [42]
  #   q.push 23  #=> NoMethodError
  #
  def def_instance_delegator(accessor, method, ali = method)
    gen = Forwardable._delegator_method(self, accessor, method, ali)

    # If it's not a class or module, it's an instance
    (Module === self ? self : singleton_class).module_eval(&gen)
  end

  alias delegate instance_delegate
  alias def_delegators def_instance_delegators
  alias def_delegator def_instance_delegator

  def self._delegator_method(obj, accessor, method, ali)
    accessor = accessor.to_s unless Symbol === accessor

    if Module === obj ?
         obj.method_defined?(accessor) || obj.private_method_defined?(accessor) :
         obj.respond_to?(accessor, true)
      accessor = "#{accessor}()"
    end

    line_no = __LINE__+1; str = "#{<<-"begin;"}\n#{<<-"end;"}"
    begin;
      proc do
        def #{ali}(*args, &block)
          begin
            #{accessor}
          end.__send__ :#{method}, *args, &block
        end
      end
    end;

    RubyVM::InstructionSequence
      .compile(str, __FILE__, __FILE__, line_no,
               trace_instruction: false,
               tailcall_optimization: true)
      .eval
  end
end

# SingleForwardable can be used to setup delegation at the object level as well.
#
#    printer = String.new
#    printer.extend SingleForwardable        # prepare object for delegation
#    printer.def_delegator "STDOUT", "puts"  # add delegation for STDOUT.puts()
#    printer.puts "Howdy!"
#
# Also, SingleForwardable can be used to set up delegation for a Class or Module.
#
#   class Implementation
#     def self.service
#       puts "serviced!"
#     end
#   end
#
#   module Facade
#     extend SingleForwardable
#     def_delegator :Implementation, :service
#   end
#
#   Facade.service #=> serviced!
#
# If you want to use both Forwardable and SingleForwardable, you can
# use methods def_instance_delegator and def_single_delegator, etc.
module SingleForwardable
  # Takes a hash as its argument.  The key is a symbol or an array of
  # symbols.  These symbols correspond to method names.  The value is
  # the accessor to which the methods will be delegated.
  #
  # :call-seq:
  #    delegate method => accessor
  #    delegate [method, method, ...] => accessor
  #
  def single_delegate(hash)
    hash.each{ |methods, accessor|
      methods = [methods] unless methods.respond_to?(:each)
      methods.each{ |method|
        def_single_delegator(accessor, method)
      }
    }
  end

  #
  # Shortcut for defining multiple delegator methods, but with no
  # provision for using a different name.  The following two code
  # samples have the same effect:
  #
  #   def_delegators :@records, :size, :<<, :map
  #
  #   def_delegator :@records, :size
  #   def_delegator :@records, :<<
  #   def_delegator :@records, :map
  #
  def def_single_delegators(accessor, *methods)
    methods.delete("__send__")
    methods.delete("__id__")
    for method in methods
      def_single_delegator(accessor, method)
    end
  end

  # :call-seq:
  #   def_single_delegator(accessor, method, new_name=method)
  #
  # Defines a method _method_ which delegates to _accessor_ (i.e. it calls
  # the method of the same name in _accessor_).  If _new_name_ is
  # provided, it is used as the name for the delegate method.
  def def_single_delegator(accessor, method, ali = method)
    gen = Forwardable._delegator_method(self, accessor, method, ali)

    instance_eval(&gen)
  end

  alias delegate single_delegate
  alias def_delegators def_single_delegators
  alias def_delegator def_single_delegator
end

Directory Contents

Dirs: 28 × Files: 75

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
digest DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
drb DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
fiddle DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
io DIR
- drwxr-xr-x 2024-03-03 22:43:23
Edit Download
irb DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
json DIR
- drwxr-xr-x 2024-03-03 22:43:24
Edit Download
matrix DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
net DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
openssl DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
optparse DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
psych DIR
- drwxr-xr-x 2024-03-03 22:43:25
Edit Download
racc DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
rbconfig DIR
- drwxr-xr-x 2024-03-03 22:43:28
Edit Download
rdoc DIR
- drwxr-xr-x 2024-03-03 22:43:27
Edit Download
rexml DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
rinda DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
ripper DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
rss DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
rubygems DIR
- drwxr-xr-x 2024-03-03 22:43:28
Edit Download
shell DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
syslog DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
uri DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
webrick DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
- drwxr-xr-x 2024-03-03 22:43:25
Edit Download
xmlrpc DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
yaml DIR
- drwxr-xr-x 2024-03-03 22:43:21
Edit Download
3.49 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
3.31 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
17.90 KB lrw-r--r-- 2015-12-23 03:43:23
Edit Download
9.80 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
9.48 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
82.15 KB lrw-r--r-- 2015-12-19 05:23:50
Edit Download
1011 B lrw-r--r-- 2023-07-26 14:39:32
Edit Download
29.97 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
10.29 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
2.82 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
50 B lrw-r--r-- 2015-12-16 05:07:31
Edit Download
3.80 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
6.45 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
26.45 KB lrw-r--r-- 2017-06-30 11:24:41
Edit Download
2.17 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
1.68 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
47.47 KB lrw-r--r-- 2017-03-27 16:57:39
Edit Download
2.51 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
7.97 KB lrw-r--r-- 2018-03-18 15:04:49
Edit Download
15.41 KB lrw-r--r-- 2015-12-23 03:43:23
Edit Download
17.08 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
20.06 KB lrw-r--r-- 2016-03-29 06:10:16
Edit Download
1.77 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
5.77 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
21.93 KB lrw-r--r-- 2016-10-17 17:38:53
Edit Download
3.87 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
53.37 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
83.79 KB lrw-r--r-- 2023-07-26 14:36:45
Edit Download
7.01 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
2.03 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
5.83 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
24.61 KB lrw-r--r-- 2015-11-11 03:48:45
Edit Download
20.67 KB lrw-r--r-- 2015-11-14 07:43:23
Edit Download
445 B lrw-r--r-- 2023-07-26 14:39:32
Edit Download
59 B lrw-r--r-- 2015-12-16 05:07:31
Edit Download
56.91 KB lrw-r--r-- 2017-08-09 07:56:52
Edit Download
9.95 KB lrw-r--r-- 2018-01-31 11:05:38
Edit Download
16.08 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
14.20 KB lrw-r--r-- 2015-11-10 11:48:14
Edit Download
15.89 KB lrw-r--r-- 2015-11-10 11:48:14
Edit Download
12.35 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
236 B lrw-r--r-- 2015-12-16 05:07:31
Edit Download
4.54 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
14.58 KB lrw-r--r-- 2015-12-23 03:43:23
Edit Download
15.29 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
175 B lrw-r--r-- 2023-07-26 14:39:32
Edit Download
5.07 KB lrw-r--r-- 2015-12-22 12:08:13
Edit Download
1.76 KB lrw-r--r-- 2015-11-14 08:25:30
Edit Download
73.52 KB lrw-r--r-- 2018-03-28 06:03:13
Edit Download
2.56 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
2.87 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
32.24 KB lrw-r--r-- 2018-02-16 16:21:44
Edit Download
23.56 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
7.49 KB lrw-r--r-- 2016-04-22 15:30:27
Edit Download
19.67 KB lrw-r--r-- 2015-11-16 07:41:30
Edit Download
11.33 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
6.28 KB lrw-r--r-- 2015-12-16 09:25:48
Edit Download
4.05 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
43.49 KB lrw-r--r-- 2023-07-26 14:39:32
Edit Download
7.29 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
10.87 KB lrw-r--r-- 2017-03-27 14:47:38
Edit Download
3.34 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
22.28 KB lrw-r--r-- 2015-11-14 12:45:30
Edit Download
3.68 KB lrw-r--r-- 2015-12-18 15:46:50
Edit Download
4.17 KB lrw-r--r-- 2018-03-28 10:29:15
Edit Download
6.43 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
14.30 KB lrw-r--r-- 2015-11-14 08:46:11
Edit Download
299 B lrw-r--r-- 2015-12-16 05:07:31
Edit Download
8.87 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
3.19 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
3.10 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
2.95 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download
6.72 KB lrw-r--r-- 2015-12-23 03:43:23
Edit Download
8.93 KB lrw-r--r-- 2015-12-23 03:43:23
Edit Download
1.73 KB lrw-r--r-- 2015-12-16 05:07:31
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).