diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-01 11:12:38 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-01 11:12:38 +0000 |
commit | 7d017781393b72a5257dbbfc99b124a7a0c492f1 (patch) | |
tree | fe08d7d97f58e320476450e0f8e450bc4a1693a9 /activesupport/lib | |
parent | 4f017bb1b02089846fc26c873599a1026b674be5 (diff) | |
download | rails-7d017781393b72a5257dbbfc99b124a7a0c492f1.tar.gz rails-7d017781393b72a5257dbbfc99b124a7a0c492f1.tar.bz2 rails-7d017781393b72a5257dbbfc99b124a7a0c492f1.zip |
Deprecation: easier to work with warning behavior as procs; default behaviors for each environment so users needn't update env.rb; and testing pleasure with assert_deprecated, assert_not_deprecated. Test prints to , dev logs, production ignores.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4647 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/deprecation.rb | 106 |
1 files changed, 74 insertions, 32 deletions
diff --git a/activesupport/lib/active_support/deprecation.rb b/activesupport/lib/active_support/deprecation.rb index a72b12bcfe..77c00f5a67 100644 --- a/activesupport/lib/active_support/deprecation.rb +++ b/activesupport/lib/active_support/deprecation.rb @@ -1,46 +1,88 @@ module ActiveSupport module Deprecation - @@warning_method = :print - mattr_accessor :warning_method - + # Choose the default warn behavior according to RAILS_ENV. + # Ignore deprecation warnings in production. + DEFAULT_BEHAVIORS = { + 'test' => Proc.new { |message| $stderr.puts message }, + 'development' => Proc.new { |message| RAILS_DEFAULT_LOGGER.warn message }, + } + class << self + def warn(message = nil, callstack = caller) + behavior.call(deprecation_message(callstack, message)) if behavior + end - def print_warning(lines) - lines.each {|l| $stderr.write("#{l}\n")} + def default_behavior + DEFAULT_BEHAVIORS[RAILS_ENV.to_s] if defined?(RAILS_ENV) end - - def log_warning(lines) - if Object.const_defined?("RAILS_DEFAULT_LOGGER") - lines.each {|l| RAILS_DEFAULT_LOGGER.warn l} - else - print_warning(lines) + + private + def deprecation_message(callstack, message = nil) + file, line, method = extract_callstack(callstack) + message ||= "WARNING: #{method} is deprecated and will be removed from the next Rails release" + "#{message} (#{method} at #{file}:#{line})" + end + + def extract_callstack(callstack) + callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/).captures 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 - + + # Behavior is a block that takes a message argument. + mattr_accessor :behavior + self.behavior = default_behavior + 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 + # Declare that a method has been deprecated. + def deprecate(*method_names) + method_names.each do |method_name| + class_eval(<<-EOS, __FILE__, __LINE__) + def #{method_name}_with_deprecation(*args, &block) + ::ActiveSupport::Deprecation.warn + #{method_name}_without_deprecation(*args, &block) + end + EOS + alias_method_chain(method_name, :deprecation) end - EOS end end + + module Assertions + def assert_deprecated(regexp = 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 + end + + def assert_not_deprecated(&block) + last = with_last_message_tracking_deprecation_behavior(&block) + assert_nil last, "Expected no deprecation warning within the block but received one: #{last}" + end + + private + def with_last_message_tracking_deprecation_behavior + old_behavior = ActiveSupport::Deprecation.behavior + last_message = nil + ActiveSupport::Deprecation.behavior = Proc.new { |message| last_message = message; old_behavior.call(message) if old_behavior } + yield + last_message + ensure + ActiveSupport::Deprecation.behavior = old_behavior + end + end end end -Object.extend(ActiveSupport::Deprecation::ClassMethods) +class Class + include ActiveSupport::Deprecation::ClassMethods +end + +module Test + module Unit + class TestCase + include ActiveSupport::Deprecation::Assertions + end + end +end |