mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	Initial revision
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1375 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
		
							parent
							
								
									6bbab099c0
								
							
						
					
					
						commit
						32d17e265c
					
				
					 3 changed files with 257 additions and 0 deletions
				
			
		
							
								
								
									
										84
									
								
								doc/forwardable.rd
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								doc/forwardable.rd
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,84 @@
 | 
			
		|||
 -- forwardable.rb
 | 
			
		||||
						
 | 
			
		||||
                                                $Release Version: 1.1 $
 | 
			
		||||
                                                $Revision$
 | 
			
		||||
                                                $Date$
 | 
			
		||||
						Original version by Tosh
 | 
			
		||||
 | 
			
		||||
==begin
 | 
			
		||||
 | 
			
		||||
= Forwardable
 | 
			
		||||
 | 
			
		||||
A Module to define delegations for selected methods to a class.
 | 
			
		||||
 | 
			
		||||
== Usage
 | 
			
		||||
 | 
			
		||||
Using through extending the class.
 | 
			
		||||
  
 | 
			
		||||
  class Foo
 | 
			
		||||
    extend Forwardable
 | 
			
		||||
 | 
			
		||||
    def_delegators("@out", "printf", "print")
 | 
			
		||||
    def_delegators(:@in, :gets)
 | 
			
		||||
    def_delegator(:@contents, :[], "content_at")
 | 
			
		||||
  end
 | 
			
		||||
  f = Foo.new
 | 
			
		||||
  f.printf ...
 | 
			
		||||
  f.gets
 | 
			
		||||
  f.content_at(1)
 | 
			
		||||
 | 
			
		||||
== Methods
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_instance_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      adding the delegations for each method of ((|methods|)) to
 | 
			
		||||
      ((|accessor|)).
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_instance_delegator(accessor, method, ali = method)
 | 
			
		||||
      
 | 
			
		||||
      adding the delegation for ((|method|)) to ((|accessor|)). When
 | 
			
		||||
      you give optional argument ((|ali|)), ((|ali|)) is used as the
 | 
			
		||||
      name of the delegation method, instead of ((|method|)).
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      the alias of ((|Forwardable#def_instance_delegators|)).
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_delegator(accessor, method, ali = method)
 | 
			
		||||
      
 | 
			
		||||
      the alias of ((|Forwardable#def_instance_delegator|)).
 | 
			
		||||
 | 
			
		||||
= SingleForwardable
 | 
			
		||||
 | 
			
		||||
a Module to define delegations for selected methods to an object.
 | 
			
		||||
 | 
			
		||||
== Usage
 | 
			
		||||
 | 
			
		||||
Using through extending the object.
 | 
			
		||||
 | 
			
		||||
  g = Goo.new
 | 
			
		||||
  g.extend SingleForwardable
 | 
			
		||||
  g.def_delegator("@out", :puts)
 | 
			
		||||
  g.puts ...
 | 
			
		||||
 | 
			
		||||
== Methods
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_singleton_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      adding the delegations for each method of ((|methods|)) to
 | 
			
		||||
      ((|accessor|)).
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_singleton_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      adding the delegation for ((|method|)) to ((|accessor|)). When
 | 
			
		||||
      you give optional argument ((|ali|)), ((|ali|)) is used as the
 | 
			
		||||
      name of the delegation method, instead of ((|method|)).
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      the alias of ((|SingleForwardable#def_instance_delegators|)).
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      the alias of ((|SingleForwardable#def_instance_delegator|)).
 | 
			
		||||
==end
 | 
			
		||||
							
								
								
									
										79
									
								
								doc/forwardable.rd.jp
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										79
									
								
								doc/forwardable.rd.jp
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,79 @@
 | 
			
		|||
  -- forwatable.rb
 | 
			
		||||
                                                $Release Version: 1.1 $
 | 
			
		||||
                                                $Revision$
 | 
			
		||||
                                                $Date$
 | 
			
		||||
 | 
			
		||||
= Forwardable
 | 
			
		||||
 | 
			
		||||
クラスに対しメソッドの委譲機能を定義します.
 | 
			
		||||
 | 
			
		||||
== 使い方
 | 
			
		||||
 | 
			
		||||
クラスに対してextendして使います. 
 | 
			
		||||
  
 | 
			
		||||
  class Foo
 | 
			
		||||
    extend Forwardable
 | 
			
		||||
 | 
			
		||||
    def_delegators("@out", "printf", "print")
 | 
			
		||||
    def_delegators(:@in, :gets)
 | 
			
		||||
    def_delegator(:@contents, :[], "content_at")
 | 
			
		||||
  end
 | 
			
		||||
  f = Foo.new
 | 
			
		||||
  f.printf ...
 | 
			
		||||
  f.gets
 | 
			
		||||
  f.content_at(1)
 | 
			
		||||
 | 
			
		||||
== メソッド
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_instance_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      ((|methods|))で渡されたメソッドのリストを((|accessorに|))委譲する
 | 
			
		||||
      ようにします.
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_instance_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      ((||method|))で渡されたメソッドを((|accessor|))に委譲するようにし
 | 
			
		||||
      ます. ((|ali|))が引数として渡されたときは, メソッド((|ali|))が呼ば
 | 
			
		||||
      れたときには, ((|accessor|))に対し((|method|))を呼び出します.
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      ((|Forwardable#def_instance_delegators|))の別名です.
 | 
			
		||||
 | 
			
		||||
--- Forwardable#def_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      ((|Forwardable#def_instance_delegator|))の別名です.
 | 
			
		||||
 | 
			
		||||
= SingleForwardable
 | 
			
		||||
 | 
			
		||||
オブジェクトに対し, メソッドの委譲機能を定義します.
 | 
			
		||||
 | 
			
		||||
== 使い方
 | 
			
		||||
 | 
			
		||||
オブジェクトに対して((|extend|))して使います. 
 | 
			
		||||
 | 
			
		||||
  g = Goo.new
 | 
			
		||||
  g.extend SingleForwardable
 | 
			
		||||
  g.def_delegator("@out", :puts)
 | 
			
		||||
  g.puts ...
 | 
			
		||||
 | 
			
		||||
== メソッド
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_singleton_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      ((|methods|))で渡されたメソッドのリストを((|accessor|))に委譲する
 | 
			
		||||
      ようにします.
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_singleton_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      ((|method|))で渡されたメソッドを((|accessor|))に委譲するようにしま
 | 
			
		||||
      す. ((|ali|))が引数として渡されたときは, メソッド((|ali|))が呼ばれ
 | 
			
		||||
      たときには, ((|accessor|))に対し((|method|))を呼び出します.
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_delegators(accessor, *methods)
 | 
			
		||||
 | 
			
		||||
      ((|SingleForwardable#def_singleton_delegators|))の別名です.
 | 
			
		||||
 | 
			
		||||
--- SingleForwardable#def_delegator(accessor, method, ali = method)
 | 
			
		||||
 | 
			
		||||
      ((|SingleForwardable#def_singleton_delegator|))の別名です.
 | 
			
		||||
							
								
								
									
										94
									
								
								lib/forwardable.rb
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										94
									
								
								lib/forwardable.rb
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,94 @@
 | 
			
		|||
#
 | 
			
		||||
#   forwardable.rb - 
 | 
			
		||||
#   	$Release Version: 1.1$
 | 
			
		||||
#   	$Revision$
 | 
			
		||||
#   	$Date$
 | 
			
		||||
#   	by Keiju ISHITSUKA(keiju@ishitsuka.com)
 | 
			
		||||
#	original definition by delegator.rb
 | 
			
		||||
# --
 | 
			
		||||
# Usage:
 | 
			
		||||
#
 | 
			
		||||
#   class Foo
 | 
			
		||||
#     extend Forwardable
 | 
			
		||||
#
 | 
			
		||||
#     def_delegators("@out", "printf", "print")
 | 
			
		||||
#     def_delegators(:@in, :gets)
 | 
			
		||||
#     def_delegator(:@contents, :[], "content_at")
 | 
			
		||||
#   end
 | 
			
		||||
#   f = Foo.new
 | 
			
		||||
#   f.printf ...
 | 
			
		||||
#   f.gets
 | 
			
		||||
#   f.content_at(1)
 | 
			
		||||
#
 | 
			
		||||
#   g = Goo.new
 | 
			
		||||
#   g.extend SingleForwardable
 | 
			
		||||
#   g.def_delegator("@out", :puts)
 | 
			
		||||
#   g.puts ...
 | 
			
		||||
#
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
module Forwardable
 | 
			
		||||
 | 
			
		||||
  @debug = nil
 | 
			
		||||
  class<<self
 | 
			
		||||
    attr_accessor :debug
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def def_instance_delegators(accessor, *methods)
 | 
			
		||||
    for method in methods
 | 
			
		||||
      def_instance_delegator(accessor, method)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def def_instance_delegator(accessor, method, ali = method)
 | 
			
		||||
    accessor = accessor.id2name if accessor.kind_of?(Integer)
 | 
			
		||||
    method = method.id2name if method.kind_of?(Integer)
 | 
			
		||||
    ali = ali.id2name if ali.kind_of?(Integer)
 | 
			
		||||
 | 
			
		||||
    module_eval(<<-EOS, "(__FORWARDABLE__)", 1)
 | 
			
		||||
      def #{ali}(*args, &block)
 | 
			
		||||
	begin
 | 
			
		||||
	  #{accessor}.__send__(:#{method}, *args, &block)
 | 
			
		||||
	rescue Exception
 | 
			
		||||
	  $@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug
 | 
			
		||||
	  raise
 | 
			
		||||
	end
 | 
			
		||||
      end
 | 
			
		||||
    EOS
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  alias def_delegators def_instance_delegators
 | 
			
		||||
  alias def_delegator def_instance_delegator
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
module SingleForwardable
 | 
			
		||||
  def def_singleton_delegators(accessor, *methods)
 | 
			
		||||
    for method in methods
 | 
			
		||||
      def_singleton_delegator(accessor, method)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def def_singleton_delegator(accessor, method, ali = method)
 | 
			
		||||
    accessor = accessor.id2name if accessor.kind_of?(Integer)
 | 
			
		||||
    method = method.id2name if method.kind_of?(Integer)
 | 
			
		||||
    ali = ali.id2name if ali.kind_of?(Integer)
 | 
			
		||||
 | 
			
		||||
    instance_eval(<<-EOS, "(__FORWARDABLE__)", 1)
 | 
			
		||||
       def #{ali}(*args, &block)
 | 
			
		||||
	 begin
 | 
			
		||||
	   #{accessor}.__send__(:#{method}, *args,&block)
 | 
			
		||||
	 rescue Exception
 | 
			
		||||
	   $@.delete_if{|s| /^\\(__FORWARDABLE__\\):/ =~ s} unless Forwardable::debug
 | 
			
		||||
	   raise
 | 
			
		||||
	 end
 | 
			
		||||
       end
 | 
			
		||||
    EOS
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  alias def_delegators def_singleton_delegators
 | 
			
		||||
  alias def_delegator def_singleton_delegator
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue