Skip to content
This repository
Browse code

Revert all the stuff to do with disallowing non-public methods for Mo…

…dule#delegate
  • Loading branch information...
commit 8ba491acc31bf08cf63a83ea0a3c314c52cd020f 1 parent bad6803
Jon Leighton authored August 25, 2011
2  activesupport/CHANGELOG
@@ -2,8 +2,6 @@
2 2
 
3 3
 * Added Array#prepend as an alias for Array#unshift and Array#append as an alias for Array#<< [DHH]
4 4
 
5  
-* Removed support for using Module#delegate to delegate to non-public methods [Jon Leighton]
6  
-
7 5
 * The definition of blank string for Ruby 1.9 has been extended to Unicode whitespace.
8 6
 Also, in 1.8 the ideographic space U+3000 is considered to be whitespace. [Akira Matsuda, Damien Mathieu]
9 7
 
8  activesupport/lib/active_support/core_ext/module/delegation.rb
... ...
@@ -1,6 +1,3 @@
1  
-require 'active_support/core_ext/object/public_send'
2  
-require 'active_support/core_ext/string/starts_ends_with'
3  
-
4 1
 class Module
5 2
   # Provides a delegate class method to easily expose contained objects' methods
6 3
   # as your own. Pass one or more methods (specified as symbols or strings)
@@ -127,13 +124,12 @@ def delegate(*methods)
127 124
 
128 125
     methods.each do |method|
129 126
       method = method.to_s
130  
-      call   = method.ends_with?('=') ? "public_send(:#{method}, " : "#{method}("
131 127
 
132 128
       if allow_nil
133 129
         module_eval(<<-EOS, file, line - 2)
134 130
           def #{method_prefix}#{method}(*args, &block)        # def customer_name(*args, &block)
135 131
             if #{to} || #{to}.respond_to?(:#{method})         #   if client || client.respond_to?(:name)
136  
-              #{to}.#{call}*args, &block)                     #     client.name(*args, &block)
  132
+              #{to}.__send__(:#{method}, *args, &block)       #     client.__send__(:name, *args, &block)
137 133
             end                                               #   end
138 134
           end                                                 # end
139 135
         EOS
@@ -142,7 +138,7 @@ def #{method_prefix}#{method}(*args, &block)        # def customer_name(*args, &
142 138
 
143 139
         module_eval(<<-EOS, file, line - 1)
144 140
           def #{method_prefix}#{method}(*args, &block)        # def customer_name(*args, &block)
145  
-            #{to}.#{call}*args, &block)                       #   client.name(*args, &block)
  141
+            #{to}.__send__(:#{method}, *args, &block)         #   client.__send__(:name, *args, &block)
146 142
           rescue NoMethodError                                # rescue NoMethodError
147 143
             if #{to}.nil?                                     #   if client.nil?
148 144
               #{exception}                                    #     # add helpful message to the exception
1  activesupport/lib/active_support/core_ext/object.rb
@@ -3,7 +3,6 @@
3 3
 require 'active_support/core_ext/object/duplicable'
4 4
 require 'active_support/core_ext/object/try'
5 5
 require 'active_support/core_ext/object/inclusion'
6  
-require 'active_support/core_ext/object/public_send'
7 6
 
8 7
 require 'active_support/core_ext/object/conversions'
9 8
 require 'active_support/core_ext/object/instance_variables'
25  activesupport/lib/active_support/core_ext/object/public_send.rb
... ...
@@ -1,25 +0,0 @@
1  
-require 'active_support/core_ext/kernel/singleton_class'
2  
-
3  
-class Object
4  
-  unless Object.public_method_defined?(:public_send)
5  
-    # Backports Object#public_send from 1.9
6  
-    def public_send(method, *args, &block)
7  
-      # Don't create a singleton class for the object if it doesn't already have one
8  
-      # (This also protects us from classes like Fixnum and Symbol, which cannot have a
9  
-      # singleton class.)
10  
-      klass = singleton_methods.any? ? self.singleton_class : self.class
11  
-
12  
-      if klass.public_method_defined?(method)
13  
-        send(method, *args, &block)
14  
-      else
15  
-        if klass.private_method_defined?(method)
16  
-          raise NoMethodError, "private method `#{method}' called for #{inspect}"
17  
-        elsif klass.protected_method_defined?(method)
18  
-          raise NoMethodError, "protected method `#{method}' called for #{inspect}"
19  
-        else
20  
-          raise NoMethodError, "undefined method `#{method}' for #{inspect}"
21  
-        end
22  
-      end
23  
-    end
24  
-  end
25  
-end
20  activesupport/test/core_ext/module_test.rb
@@ -28,20 +28,10 @@ class Cd
28 28
 
29 29
 Somewhere = Struct.new(:street, :city) do
30 30
   attr_accessor :name
31  
-
32  
-  protected
33  
-
34  
-  def protected_method
35  
-  end
36  
-
37  
-  private
38  
-
39  
-  def private_method
40  
-  end
41 31
 end
42 32
 
43 33
 class Someone < Struct.new(:name, :place)
44  
-  delegate :street, :city, :to_f, :protected_method, :private_method, :to => :place
  34
+  delegate :street, :city, :to_f, :to => :place
45 35
   delegate :name=, :to => :place, :prefix => true
46 36
   delegate :upcase, :to => "place.city"
47 37
 
@@ -93,14 +83,6 @@ def test_delegation_to_assignment_method
93 83
     assert_equal "Fred", @david.place.name
94 84
   end
95 85
 
96  
-  def test_delegation_to_protected_method
97  
-    assert_raise(NoMethodError) { @david.protected_method }
98  
-  end
99  
-
100  
-  def test_delegation_to_private_method
101  
-    assert_raise(NoMethodError) { @david.private_method }
102  
-  end
103  
-
104 86
   def test_delegation_down_hierarchy
105 87
     assert_equal "CHICAGO", @david.upcase
106 88
   end
117  activesupport/test/core_ext/object/public_send_test.rb
... ...
@@ -1,117 +0,0 @@
1  
-require 'abstract_unit'
2  
-require 'active_support/core_ext/object/public_send'
3  
-
4  
-module PublicSendReceiver
5  
-  def receive_public_method(*args)
6  
-    return args + [yield]
7  
-  end
8  
-
9  
-  protected
10  
-
11  
-  def receive_protected_method(*args)
12  
-    return args + [yield]
13  
-  end
14  
-
15  
-  private
16  
-
17  
-  def receive_private_method(*args)
18  
-    return args + [yield]
19  
-  end
20  
-end
21  
-
22  
-# Note, running this on 1.9 will be testing the Ruby core implementation, but it is good to
23  
-# do this to ensure that our backport functions the same as Ruby core in 1.9
24  
-class PublicSendTest < Test::Unit::TestCase
25  
-  def instance
26  
-    @instance ||= begin
27  
-      klass = Class.new do
28  
-        include PublicSendReceiver
29  
-      end
30  
-      klass.new
31  
-    end
32  
-  end
33  
-
34  
-  def singleton_instance
35  
-    @singleton_instance ||= begin
36  
-      object = Object.new
37  
-      object.singleton_class.send(:include, PublicSendReceiver)
38  
-      object
39  
-    end
40  
-  end
41  
-
42  
-  def test_should_receive_public_method
43  
-    assert_equal(
44  
-      [:foo, :bar, :baz],
45  
-      instance.public_send(:receive_public_method, :foo, :bar) { :baz }
46  
-    )
47  
-  end
48  
-
49  
-  def test_should_receive_public_singleton_method
50  
-    assert_equal(
51  
-      [:foo, :bar, :baz],
52  
-      singleton_instance.public_send(:receive_public_method, :foo, :bar) { :baz }
53  
-    )
54  
-  end
55  
-
56  
-  def test_should_raise_on_protected_method
57  
-    assert_raises(NoMethodError) do
58  
-      instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
59  
-    end
60  
-  end
61  
-
62  
-  def test_should_raise_on_protected_singleton_method
63  
-    assert_raises(NoMethodError) do
64  
-      singleton_instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
65  
-    end
66  
-  end
67  
-
68  
-  def test_should_raise_on_private_method
69  
-    assert_raises(NoMethodError) do
70  
-      instance.public_send(:receive_private_method, :foo, :bar) { :baz }
71  
-    end
72  
-  end
73  
-
74  
-  def test_should_raise_on_singleton_private_method
75  
-    assert_raises(NoMethodError) do
76  
-      singleton_instance.public_send(:receive_private_method, :foo, :bar) { :baz }
77  
-    end
78  
-  end
79  
-
80  
-  def test_should_raise_on_undefined_method
81  
-    assert_raises(NoMethodError) do
82  
-      instance.public_send(:receive_undefined_method, :foo, :bar) { :baz }
83  
-    end
84  
-  end
85  
-
86  
-  def test_protected_method_message
87  
-    instance.public_send(:receive_protected_method, :foo, :bar) { :baz }
88  
-  rescue NoMethodError => exception
89  
-    assert_equal(
90  
-      "protected method `receive_protected_method' called for #{instance.inspect}",
91  
-      exception.message
92  
-    )
93  
-  end
94  
-
95  
-  def test_private_method_message
96  
-    instance.public_send(:receive_private_method, :foo, :bar) { :baz }
97  
-  rescue NoMethodError => exception
98  
-    assert_equal(
99  
-      "private method `receive_private_method' called for #{instance.inspect}",
100  
-      exception.message
101  
-    )
102  
-  end
103  
-
104  
-  def test_undefined_method_message
105  
-    instance.public_send(:receive_undefined_method, :foo, :bar) { :baz }
106  
-  rescue NoMethodError => exception
107  
-    assert_equal(
108  
-      "undefined method `receive_undefined_method' for #{instance.inspect}",
109  
-      exception.message
110  
-    )
111  
-  end
112  
-
113  
-  def test_receiver_with_no_singleton
114  
-    assert_equal "5", 5.public_send(:to_s)
115  
-    assert_equal "foo", :foo.public_send(:to_s)
116  
-  end
117  
-end
24  railties/guides/source/active_support_core_extensions.textile
Source Rendered
@@ -452,30 +452,6 @@ Examples of +in?+:
452 452
 
453 453
 NOTE: Defined in +active_support/core_ext/object/inclusion.rb+.
454 454
 
455  
-h4. +public_send+
456  
-
457  
-This method is available by default in Ruby 1.9, and is backported to Ruby 1.8 by Active Support. Like the regular +send+ method, +public_send+ allows you to call a method when the name is not known until runtime. However, if the method is not public then a +NoMethodError+ exception will be raised.
458  
-
459  
-<ruby>
460  
-class Greeter
461  
-  def hello(who)
462  
-    "Hello " + who
463  
-  end
464  
-
465  
-  private
466  
-
467  
-  def secret
468  
-    "sauce"
469  
-  end
470  
-end
471  
-
472  
-greeter = Greeter.new
473  
-greeter.public_send(:hello, 'Jim') # => "Hello Jim"
474  
-greeter.public_send(:secret)       # => NoMethodError
475  
-</ruby>
476  
-
477  
-NOTE: Defined in +active_support/core_ext/object/public_send.rb+.
478  
-
479 455
 h3. Extensions to +Module+
480 456
 
481 457
 h4. +alias_method_chain+

0 notes on commit 8ba491a

Please sign in to comment.
Something went wrong with that request. Please try again.