diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2009-04-17 21:29:30 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2009-04-17 21:29:30 -0700 |
commit | 727e9dc18a7059f024b922b65951198ff0184fdd (patch) | |
tree | 740e578645d8fdc45dbec0a4412a8455cc05bfed /activesupport/lib/active_support/deprecation | |
parent | 3202671dc9360c4a89d80355824b239b52b2f611 (diff) | |
download | rails-727e9dc18a7059f024b922b65951198ff0184fdd.tar.gz rails-727e9dc18a7059f024b922b65951198ff0184fdd.tar.bz2 rails-727e9dc18a7059f024b922b65951198ff0184fdd.zip |
Dice up ActiveSupport::Deprecation
Diffstat (limited to 'activesupport/lib/active_support/deprecation')
4 files changed, 186 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/deprecation/behaviors.rb b/activesupport/lib/active_support/deprecation/behaviors.rb new file mode 100644 index 0000000000..c531a1aa58 --- /dev/null +++ b/activesupport/lib/active_support/deprecation/behaviors.rb @@ -0,0 +1,32 @@ +module ActiveSupport + module Deprecation + class << self + # Behavior is a block that takes a message argument. + attr_writer :behavior + + # Whether to print a backtrace along with the warning. + attr_accessor :debug + + def behavior + @behavior ||= default_behavior + end + + def default_behavior + Deprecation::DEFAULT_BEHAVIORS[defined?(Rails) ? Rails.env.to_s : 'test'] + end + end + + # Default warning behaviors per Rails.env. Ignored in production. + DEFAULT_BEHAVIORS = { + 'test' => Proc.new { |message, callstack| + $stderr.puts(message) + $stderr.puts callstack.join("\n ") if debug + }, + 'development' => Proc.new { |message, callstack| + logger = defined?(Rails) ? Rails.logger : Logger.new($stderr) + logger.warn message + logger.debug callstack.join("\n ") if debug + } + } + end +end diff --git a/activesupport/lib/active_support/deprecation/method_wrappers.rb b/activesupport/lib/active_support/deprecation/method_wrappers.rb new file mode 100644 index 0000000000..845bef059f --- /dev/null +++ b/activesupport/lib/active_support/deprecation/method_wrappers.rb @@ -0,0 +1,27 @@ +require 'active_support/core_ext/module/deprecation' + +module ActiveSupport + class << Deprecation + # Declare that a method has been deprecated. + def deprecate_methods(target_module, *method_names) + options = method_names.extract_options! + method_names += options.keys + + method_names.each do |method_name| + target_module.alias_method_chain(method_name, :deprecation) do |target, punctuation| + target_module.module_eval(<<-end_eval, __FILE__, __LINE__) + def #{target}_with_deprecation#{punctuation}(*args, &block) # def generate_secret_with_deprecation(*args, &block) + ::ActiveSupport::Deprecation.warn( # ::ActiveSupport::Deprecation.warn( + ::ActiveSupport::Deprecation.deprecated_method_warning( # ::ActiveSupport::Deprecation.deprecated_method_warning( + :#{method_name}, # :generate_secret, + #{options[method_name].inspect}), # "You should use ActiveSupport::SecureRandom.hex(64)"), + caller # caller + ) # ) + #{target}_without_deprecation#{punctuation}(*args, &block) # generate_secret_without_deprecation(*args, &block) + end # end + end_eval + end + end + end + end +end diff --git a/activesupport/lib/active_support/deprecation/proxy_wrappers.rb b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb new file mode 100644 index 0000000000..1c268d0d9c --- /dev/null +++ b/activesupport/lib/active_support/deprecation/proxy_wrappers.rb @@ -0,0 +1,72 @@ +module ActiveSupport + module Deprecation + class DeprecationProxy #:nodoc: + instance_methods.each { |m| undef_method m unless m =~ /^__/ } + + # Don't give a deprecation warning on inspect since test/unit and error + # logs rely on it for diagnostics. + def inspect + target.inspect + end + + private + def method_missing(called, *args, &block) + warn caller, called, args + target.__send__(called, *args, &block) + end + end + + class DeprecatedObjectProxy < DeprecationProxy #:nodoc: + def initialize(object, message) + @object = object + @message = message + end + + private + def target + @object + end + + def warn(callstack, called, args) + ActiveSupport::Deprecation.warn(@message, callstack) + end + end + + # Stand-in for <tt>@request</tt>, <tt>@attributes</tt>, <tt>@params</tt>, etc. + # which emits deprecation warnings on any method call (except +inspect+). + class DeprecatedInstanceVariableProxy < DeprecationProxy #:nodoc: + def initialize(instance, method, var = "@#{method}") + @instance, @method, @var = instance, method, var + end + + private + def target + @instance.__send__(@method) + end + + def warn(callstack, called, args) + ActiveSupport::Deprecation.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack) + end + end + + class DeprecatedConstantProxy < DeprecationProxy #:nodoc: + def initialize(old_const, new_const) + @old_const = old_const + @new_const = new_const + end + + def class + target.class + end + + private + def target + @new_const.to_s.constantize + end + + def warn(callstack, called, args) + ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack) + end + end + end +end diff --git a/activesupport/lib/active_support/deprecation/reporting.rb b/activesupport/lib/active_support/deprecation/reporting.rb new file mode 100644 index 0000000000..fcb05ad8d9 --- /dev/null +++ b/activesupport/lib/active_support/deprecation/reporting.rb @@ -0,0 +1,55 @@ +module ActiveSupport + module Deprecation + class << self + attr_accessor :silenced + + def warn(message = nil, callstack = caller) + if behavior && !silenced + behavior.call(deprecation_message(callstack, message), callstack) + end + end + + # Silence deprecation warnings within the block. + def silence + old_silenced, @silenced = @silenced, true + yield + ensure + @silenced = old_silenced + end + + def deprecated_method_warning(method_name, message = nil) + warning = "#{method_name} is deprecated and will be removed from Rails #{deprecation_horizon}" + case message + when Symbol then "#{warning} (use #{message} instead)" + when String then "#{warning} (#{message})" + else warning + end + end + + private + def deprecation_message(callstack, message = nil) + message ||= "You are using deprecated behavior which will be removed from the next major or minor release." + "DEPRECATION WARNING: #{message}. #{deprecation_caller_message(callstack)}" + end + + def deprecation_caller_message(callstack) + file, line, method = extract_callstack(callstack) + if file + if line && method + "(called from #{method} at #{file}:#{line})" + else + "(called from #{file}:#{line})" + end + end + end + + def extract_callstack(callstack) + if md = callstack.first.match(/^(.+?):(\d+)(?::in `(.*?)')?/) + md.captures + else + callstack.first + end + end + end + end +end |