Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tidy up the delegate method

Make sure we test the stack trace, too.
  • Loading branch information...
commit e346ceed0b1acda03101303e785183cfa1eb0b95 1 parent 9296a72
Daniel Schierbeck authored
38 activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -128,23 +128,29 @@ def delegate(*methods)
128 128 %(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
129 129 end
130 130
  131 + nil_clause = <<-EOS
  132 + if #{to}.nil? # if client.nil?
  133 + #{on_nil} # return # depends on :allow_nil
  134 + else # else
  135 + raise(e) # raise(e)
  136 + end # end
  137 + EOS
  138 +
131 139 rescue_clause = <<-EOS
132   - rescue NoMethodError => e # rescue NoMethodError => e
133   - raise unless e.name == #{method_name} # raise unless e.name == :name
134   - begin # begin
135   - result = #{to}.__send__(#{method_name}, *args, &block) # result = client.__send__(:name, *args, &block)
136   - rescue NoMethodError => e2 # rescue NoMethodError => e2
137   - raise unless e2.name == #{method_name} # raise unless e2.name == :name
138   - if #{to}.nil? # if client.nil?
139   - #{on_nil} # return # depends on :allow_nil
140   - else # else
141   - raise(e) # raise(e)
142   - end # end
143   - else # else
144   - ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
145   - 'Delegating to non-public methods is deprecated.', caller) # 'Delegating to non-public methods is deprecated.', caller)
146   - result # result
147   - end # end
  140 + rescue NoMethodError => e # rescue NoMethodError => e
  141 + if e.name == #{method_name} && # if e.name == :name &&
  142 + e.message =~ /(private|protected) method/ && # e.message =~ /(private|protected) method/ &&
  143 + e.backtrace.first.include?(__FILE__) # e.backtrace.first.include?(__FILE__)
  144 + begin # begin
  145 + ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
  146 + 'Delegating to non-public methods is deprecated.', caller) # 'Delegating to non-public methods is deprecated.', caller)
  147 + #{to}.__send__(#{method_name}, *args, &block) # client.__send__(:name, *args, &block)
  148 + rescue NoMethodError # rescue NoMethodError
  149 + #{nil_clause}
  150 + end # end
  151 + else # else
  152 + #{nil_clause}
  153 + end # end
148 154 EOS
149 155
150 156 method_body =
8 activesupport/test/core_ext/module_test.rb
@@ -60,7 +60,7 @@ def something_private
60 60
61 61 def raises_no_method_error_in_private
62 62 @raise_calls += 1
63   - raise NoMethodError.new("no method!", :foodle)
  63 + raise NoMethodError.new("no method!")
64 64 end
65 65
66 66 protected
@@ -158,10 +158,12 @@ def test_does_not_swallow_no_method_errors_from_private_methods
158 158 @tester = Tester.new(@david)
159 159
160 160 error = assert_raise(NoMethodError) do
161   - @tester.raises_no_method_error_in_private
  161 + assert_deprecated do
  162 + @tester.raises_no_method_error_in_private
  163 + end
162 164 end
163 165
164   - assert_equal :foodle, error.name
  166 + assert_equal :raises_no_method_error_in_private, error.name
165 167 assert_equal 1, @david.raise_calls
166 168 end
167 169

0 comments on commit e346cee

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