Permalink
Browse files

Tidy up the delegate method

Make sure we test the stack trace, too.
  • Loading branch information...
1 parent 9296a72 commit e346ceed0b1acda03101303e785183cfa1eb0b95 @dasch committed Sep 1, 2011
View
38 activesupport/lib/active_support/core_ext/module/delegation.rb
@@ -128,23 +128,29 @@ def delegate(*methods)
%(raise "#{self}##{prefix}#{method} delegated to #{to}.#{method}, but #{to} is nil: \#{self.inspect}")
end
+ nil_clause = <<-EOS
+ if #{to}.nil? # if client.nil?
+ #{on_nil} # return # depends on :allow_nil
+ else # else
+ raise(e) # raise(e)
+ end # end
+ EOS
+
rescue_clause = <<-EOS
- rescue NoMethodError => e # rescue NoMethodError => e
- raise unless e.name == #{method_name} # raise unless e.name == :name
- begin # begin
- result = #{to}.__send__(#{method_name}, *args, &block) # result = client.__send__(:name, *args, &block)
- rescue NoMethodError => e2 # rescue NoMethodError => e2
- raise unless e2.name == #{method_name} # raise unless e2.name == :name
- if #{to}.nil? # if client.nil?
- #{on_nil} # return # depends on :allow_nil
- else # else
- raise(e) # raise(e)
- end # end
- else # else
- ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
- 'Delegating to non-public methods is deprecated.', caller) # 'Delegating to non-public methods is deprecated.', caller)
- result # result
- end # end
+ rescue NoMethodError => e # rescue NoMethodError => e
+ if e.name == #{method_name} && # if e.name == :name &&
+ e.message =~ /(private|protected) method/ && # e.message =~ /(private|protected) method/ &&
+ e.backtrace.first.include?(__FILE__) # e.backtrace.first.include?(__FILE__)
+ begin # begin
+ ActiveSupport::Deprecation.warn( # ActiveSupport::Deprecation.warn(
+ 'Delegating to non-public methods is deprecated.', caller) # 'Delegating to non-public methods is deprecated.', caller)
+ #{to}.__send__(#{method_name}, *args, &block) # client.__send__(:name, *args, &block)
+ rescue NoMethodError # rescue NoMethodError
+ #{nil_clause}
+ end # end
+ else # else
+ #{nil_clause}
+ end # end
EOS
method_body =
View
8 activesupport/test/core_ext/module_test.rb
@@ -60,7 +60,7 @@ def something_private
def raises_no_method_error_in_private
@raise_calls += 1
- raise NoMethodError.new("no method!", :foodle)
+ raise NoMethodError.new("no method!")
end
protected
@@ -158,10 +158,12 @@ def test_does_not_swallow_no_method_errors_from_private_methods
@tester = Tester.new(@david)
error = assert_raise(NoMethodError) do
- @tester.raises_no_method_error_in_private
+ assert_deprecated do
+ @tester.raises_no_method_error_in_private
+ end
end
- assert_equal :foodle, error.name
+ assert_equal :raises_no_method_error_in_private, error.name
assert_equal 1, @david.raise_calls
end

0 comments on commit e346cee

Please sign in to comment.