diff options
author | Michael Koziarski <michael@koziarski.com> | 2006-07-27 00:10:06 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2006-07-27 00:10:06 +0000 |
commit | 7692191f5ab207ff76cc2558f2a02e299cd51392 (patch) | |
tree | 10c7e0a5ee77351e5c114cb6e6c8dae9d3a5cf1a | |
parent | 45e319d0f81b8e97bee69ced1db5636075b38f1d (diff) | |
download | rails-7692191f5ab207ff76cc2558f2a02e299cd51392.tar.gz rails-7692191f5ab207ff76cc2558f2a02e299cd51392.tar.bz2 rails-7692191f5ab207ff76cc2558f2a02e299cd51392.zip |
Initial Version of Deprecation for Rails[Koz]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4623 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support.rb | 1 | ||||
-rw-r--r-- | activesupport/lib/active_support/deprecation.rb | 46 | ||||
-rw-r--r-- | activesupport/test/deprecation_test.rb | 85 |
4 files changed, 134 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 4825cdbe6d..01b1e6d7b9 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* First cut of the Rails Deprecation system. [Koz] + * Strip boolean XML content before checking for 'true' [Rick Olson] * Customize default BigDecimal formatting. References #5672 [dave@pragprog.com] diff --git a/activesupport/lib/active_support.rb b/activesupport/lib/active_support.rb index 037905bea9..0c0762ad8e 100644 --- a/activesupport/lib/active_support.rb +++ b/activesupport/lib/active_support.rb @@ -32,6 +32,7 @@ require 'active_support/core_ext' require 'active_support/clean_logger' require 'active_support/dependencies' require 'active_support/reloadable' +require 'active_support/deprecation' require 'active_support/ordered_options' require 'active_support/option_merger' diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb new file mode 100644 index 0000000000..a72b12bcfe --- /dev/null +++ b/activesupport/lib/active_support/deprecation.rb @@ -0,0 +1,46 @@ +module ActiveSupport + module Deprecation + @@warning_method = :print + mattr_accessor :warning_method + + class << self + + def print_warning(lines) + lines.each {|l| $stderr.write("#{l}\n")} + end + + def log_warning(lines) + if Object.const_defined?("RAILS_DEFAULT_LOGGER") + lines.each {|l| RAILS_DEFAULT_LOGGER.warn l} + else + print_warning(lines) + end + end + + def issue_warning(line) + lines = + ["@@@@@@@@@@ Deprecation Warning @@@@@@@@@@", line, + "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"] + self.send("#{@@warning_method}_warning", lines) + end + + def instance_method_warning(clazz, method) + issue_warning("Your application calls #{clazz}##{method}, which is now deprecated. Please see the API documents at http://api.rubyonrails.org/ for more information.") + end + end + + module ClassMethods + def deprecate(method_name) + alias_method "#{method_name}_before_deprecation", method_name + class_eval(<<-EOS, __FILE__, __LINE__) + def #{method_name}(*args) + ::ActiveSupport::Deprecation.instance_method_warning(self.class, :#{method_name}) + #{method_name}_before_deprecation *args + end + EOS + end + end + end +end + +Object.extend(ActiveSupport::Deprecation::ClassMethods) diff --git a/activesupport/test/deprecation_test.rb b/activesupport/test/deprecation_test.rb new file mode 100644 index 0000000000..c691b5bde0 --- /dev/null +++ b/activesupport/test/deprecation_test.rb @@ -0,0 +1,85 @@ +require 'test/unit' +require File.dirname(__FILE__) + '/../lib/active_support/deprecation' + +# Stub out the warnings to allow assertions +module ActiveSupport + module Deprecation + class << self + def issue_warning(message) + @@warning = message + end + def last_warning + @@warning + end + end + end +end + +class DeprecationTestingClass + + def partiallly_deprecated(foo = nil) + if foo.nil? + ActiveSupport::Deprecation.issue_warning("calling partially_deprecated with foo=nil is now deprecated") + end + end + + def not_deprecated + 2 + end + + def deprecated_no_args + 1 + end + deprecate :deprecated_no_args + + def deprecated_one_arg(a) + a + end + deprecate :deprecated_one_arg + + def deprecated_multiple_args(a,b,c) + [a,b,c] + end + deprecate :deprecated_multiple_args + +end + + +class DeprecationTest < Test::Unit::TestCase + def setup + @dtc = DeprecationTestingClass.new + ActiveSupport::Deprecation.issue_warning(nil) # reset + end + + def test_partial_deprecation + @dtc.partiallly_deprecated + assert_warning_matches /foo=nil/ + end + + def test_raises_nothing + assert_equal 2, @dtc.not_deprecated + end + + def test_deprecating_class_method + assert_equal 1, @dtc.deprecated_no_args + assert_deprecation_warning + assert_warning_matches /DeprecationTestingClass#deprecated_no_args/ + end + + def test_deprecating_class_method_with_argument + assert_equal 1, @dtc.deprecated_one_arg(1) + end + + def test_deprecating_class_method_with_argument + assert_equal [1,2,3], @dtc.deprecated_multiple_args(1,2,3) + end + + private + def assert_warning_matches(rx) + assert ActiveSupport::Deprecation.last_warning =~ rx, "The deprecation warning did not match #{rx}" + end + + def assert_deprecation_warning + assert_not_nil ActiveSupport::Deprecation.last_warning, "No Deprecation warnings were issued" + end +end |