Skip to content
Browse files

DeprecatedInstanceVariableProxy stand-in for @request, @attributes, a…

…nd friends.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4666 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 6c50597 commit 0e73a92c0441fb7faec3ac44dbaa8fbf8335df10 @jeremy jeremy committed Aug 5, 2006
Showing with 39 additions and 4 deletions.
  1. +23 −4 activesupport/lib/active_support/deprecation.rb
  2. +16 −0 activesupport/test/deprecation_test.rb
View
27 activesupport/lib/active_support/deprecation.rb
@@ -48,12 +48,11 @@ def #{method_name}_with_deprecation(*args, &block)
end
module Assertions
- def assert_deprecated(regexp = nil, &block)
+ def assert_deprecated(match = nil, &block)
last = with_last_message_tracking_deprecation_behavior(&block)
assert last, "Expected a deprecation warning within the block but received none"
- if regexp
- assert_match regexp, last, "Deprecation warning didn't match #{regexp}: #{last}"
- end
+ match = Regexp.new(match) unless match.is_a?(Regexp)
+ assert_match match, last, "Deprecation warning didn't match #{match}: #{last}"
end
def assert_not_deprecated(&block)
@@ -72,6 +71,26 @@ def with_last_message_tracking_deprecation_behavior
ActiveSupport::Deprecation.behavior = old_behavior
end
end
+
+ # Stand-in for @request, @attributes, etc.
+ class DeprecatedInstanceVariableProxy
+ instance_methods.each { |m| undef_method m unless m =~ /^__/ }
+
+ def initialize(instance, method, var = "@#{method}")
+ @instance, @method, @var = instance, method, var
+ deprecation_warning :initialize, caller
+ end
+
+ private
+ def deprecation_warning(called, callstack)
+ ActiveSupport::Deprecation.warn("Using #{@var} directly is deprecated - call #{@method} instead.", callstack)
+ end
+
+ def method_missing(called, *args, &block)
+ deprecation_warning called, caller
+ @instance.__send__(@method).__send__(called, *args, &block)
+ end
+ end
end
end
View
16 activesupport/test/deprecation_test.rb
@@ -1,6 +1,13 @@
require File.dirname(__FILE__) + '/abstract_unit'
class Deprecatee
+ def initialize
+ @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request)
+ @_request = 'there we go'
+ end
+ def request; @_request end
+ def old_request; @request end
+
def partially(foo = nil)
ActiveSupport::Deprecation.warn 'calling with foo=nil is out' if foo.nil?
end
@@ -57,4 +64,13 @@ def test_nil_behavior_is_ignored
ActiveSupport::Deprecation.behavior = nil
assert_deprecated(/foo=nil/) { @dtc.partially }
end
+
+ def test_deprecated_instance_variable_proxy
+ assert_not_deprecated { @dtc.request.size }
+
+ assert_deprecated('Using @request directly is deprecated - call request instead.') do
+ assert_equal @dtc.request.size, @dtc.old_request.size
+ assert_equal @dtc.request.to_s, @dtc.old_request.to_s
+ end
+ end
end

0 comments on commit 0e73a92

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