From a53331a161f72b25f6e9c860db43acaf23250f68 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Tue, 6 May 2008 11:52:44 +0100 Subject: [PATCH] Add class to deprecate instance variables Add ActiveSupport::Deprecation::DeprecatedInstanceVariable class to deprecate instance variables of primitive types such as stings. --- activesupport/lib/active_support/deprecation.rb | 14 ++++++++++++++ activesupport/test/deprecation_test.rb | 10 ++++++++++ 2 files changed, 24 insertions(+) diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index c9f1884da3..7613652c71 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -175,6 +175,20 @@ module ActiveSupport ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack) end end + + class DeprecatedInstanceVariable < Delegator #:nodoc: + def initialize(value, method) + super(value) + @method = method + @value = value + end + + def __getobj__ + ActiveSupport::Deprecation.warn("Instance variable @#{@method} is deprecated! Call instance method #{@method} instead.") + @value + end + end + end end diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb index ebfa405947..11357e250f 100644 --- a/activesupport/test/deprecation_test.rb +++ b/activesupport/test/deprecation_test.rb @@ -149,3 +149,13 @@ class DeprecationTest < Test::Unit::TestCase assert_nil @last_message end end + +class DeprecatedIvarTest < Test::Unit::TestCase + + def test_deprecated_ivar + @action = ActiveSupport::Deprecation::DeprecatedInstanceVariable.new("fubar", :foobar) + + assert_deprecated(/Instance variable @foobar is deprecated! Call instance method foobar instead/) { assert_equal "fubar", @action } + end + +end