PHP 8.2.31
Preview: forwardable.rb Size: 8.22 KB
//proc/thread-self/root/opt/alt/ruby22/lib64/ruby/2.2.0/forwardable.rb

# frozen_string_literal: false
#
#   forwardable.rb -
#       $Release Version: 1.1$
#       $Revision: 58085 $
#       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"

  FILE_REGEXP = %r"#{Regexp.quote(__FILE__)}"

  @debug = nil
  class << self
    # If true, <tt>__FILE__</tt> will remain in the backtrace in the event an
    # Exception is raised.
    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}
          ensure
            $@.delete_if {|s| ::Forwardable::FILE_REGEXP =~ s} if $@ and !::Forwardable::debug
          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: 29 × Files: 75

Name Size Perms Modified Actions
cgi DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
digest DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
drb DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
fiddle DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
io DIR
- drwxr-xr-x 2024-03-03 22:43:16
Edit Download
irb DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
json DIR
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
matrix DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
net DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
openssl DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
optparse DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
psych DIR
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
racc DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rake DIR
- drwxr-xr-x 2024-03-03 22:53:17
Edit Download
rbconfig DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
rdoc DIR
- drwxr-xr-x 2024-03-03 22:43:18
Edit Download
rexml DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rinda DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
ripper DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rss DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
rubygems DIR
- drwxr-xr-x 2024-03-03 22:43:19
Edit Download
shell DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
syslog DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
uri DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
webrick DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
- drwxr-xr-x 2024-03-03 22:43:17
Edit Download
xmlrpc DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
yaml DIR
- drwxr-xr-x 2024-03-03 22:43:14
Edit Download
3.46 KB lrw-r--r-- 2014-07-11 08:16:05
Edit Download
2.63 KB lrw-r--r-- 2013-05-19 03:10:21
Edit Download
17.73 KB lrw-r--r-- 2014-09-11 08:09:07
Edit Download
9.77 KB lrw-r--r-- 2014-08-07 06:14:29
Edit Download
9.34 KB lrw-r--r-- 2015-07-03 19:31:05
Edit Download
82.45 KB lrw-r--r-- 2014-11-04 03:21:53
Edit Download
980 B lrw-r--r-- 2023-07-26 14:31:54
Edit Download
29.08 KB lrw-r--r-- 2013-12-14 14:48:36
Edit Download
10.71 KB lrw-r--r-- 2014-07-26 17:11:56
Edit Download
2.79 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
19 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
3.77 KB lrw-r--r-- 2014-08-24 07:16:25
Edit Download
6.42 KB lrw-r--r-- 2014-05-31 19:54:55
Edit Download
26.35 KB lrw-r--r-- 2014-12-12 10:48:57
Edit Download
2.14 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
1.65 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
47.46 KB lrw-r--r-- 2017-03-25 18:24:28
Edit Download
2.48 KB lrw-r--r-- 2014-10-13 07:34:23
Edit Download
8.22 KB lrw-r--r-- 2017-03-25 14:43:16
Edit Download
15.38 KB lrw-r--r-- 2013-05-19 14:50:47
Edit Download
17.06 KB lrw-r--r-- 2015-11-24 15:49:21
Edit Download
20.03 KB lrw-r--r-- 2016-03-28 15:07:06
Edit Download
1.74 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
5.74 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
20.33 KB lrw-r--r-- 2016-10-27 07:47:14
Edit Download
3.84 KB lrw-r--r-- 2014-08-26 12:07:57
Edit Download
53.14 KB lrw-r--r-- 2014-11-27 01:03:46
Edit Download
82.59 KB lrw-r--r-- 2023-07-26 14:29:02
Edit Download
6.93 KB lrw-r--r-- 2012-11-16 16:55:29
Edit Download
2.00 KB lrw-r--r-- 2013-02-20 02:51:51
Edit Download
5.80 KB lrw-r--r-- 2014-08-27 12:21:41
Edit Download
24.58 KB lrw-r--r-- 2014-12-24 09:11:05
Edit Download
20.55 KB lrw-r--r-- 2014-11-04 03:09:28
Edit Download
528 B lrw-r--r-- 2023-07-26 14:31:54
Edit Download
28 B lrw-r--r-- 2014-05-26 03:25:38
Edit Download
52.05 KB lrw-r--r-- 2017-03-25 18:28:27
Edit Download
8.66 KB lrw-r--r-- 2017-03-25 18:35:09
Edit Download
15.58 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
14.16 KB lrw-r--r-- 2014-12-05 19:35:19
Edit Download
15.85 KB lrw-r--r-- 2014-08-22 02:38:59
Edit Download
13.11 KB lrw-r--r-- 2014-12-10 20:38:13
Edit Download
205 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
4.51 KB lrw-r--r-- 2013-05-19 23:04:36
Edit Download
14.55 KB lrw-r--r-- 2014-07-23 19:31:18
Edit Download
14.88 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
2.23 KB lrw-r--r-- 2014-12-06 00:22:51
Edit Download
4.96 KB lrw-r--r-- 2014-12-07 01:22:37
Edit Download
1.73 KB lrw-r--r-- 2013-03-11 13:47:04
Edit Download
72.06 KB lrw-r--r-- 2015-05-19 17:05:35
Edit Download
2.53 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
2.84 KB lrw-r--r-- 2011-05-11 10:22:16
Edit Download
31.85 KB lrw-r--r-- 2018-02-16 16:27:56
Edit Download
23.54 KB lrw-r--r-- 2014-02-15 01:33:03
Edit Download
9.20 KB lrw-r--r-- 2016-04-22 09:17:57
Edit Download
19.15 KB lrw-r--r-- 2014-08-06 11:28:21
Edit Download
11.30 KB lrw-r--r-- 2014-08-24 07:16:25
Edit Download
5.96 KB lrw-r--r-- 2014-09-05 10:00:46
Edit Download
4.02 KB lrw-r--r-- 2011-05-18 14:09:38
Edit Download
25.60 KB lrw-r--r-- 2023-07-26 14:31:54
Edit Download
7.25 KB lrw-r--r-- 2013-05-19 03:10:21
Edit Download
11.11 KB lrw-r--r-- 2014-09-21 01:40:21
Edit Download
3.31 KB lrw-r--r-- 2014-08-27 12:10:21
Edit Download
22.25 KB lrw-r--r-- 2015-08-03 19:11:41
Edit Download
3.64 KB lrw-r--r-- 2015-08-10 17:08:37
Edit Download
4.13 KB lrw-r--r-- 2018-03-28 14:34:14
Edit Download
6.40 KB lrw-r--r-- 2013-07-18 13:50:32
Edit Download
14.27 KB lrw-r--r-- 2014-11-26 10:46:50
Edit Download
268 B lrw-r--r-- 2009-10-02 10:45:39
Edit Download
8.87 KB lrw-r--r-- 2013-07-05 13:43:25
Edit Download
3.16 KB lrw-r--r-- 2015-01-16 02:06:34
Edit Download
3.07 KB lrw-r--r-- 2014-11-03 00:33:44
Edit Download
2.92 KB lrw-r--r-- 2014-07-26 17:12:11
Edit Download
6.69 KB lrw-r--r-- 2013-10-05 23:39:32
Edit Download
8.49 KB lrw-r--r-- 2013-12-12 03:09:19
Edit Download
1.70 KB lrw-r--r-- 2014-06-26 02:52:46
Edit Download

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