deprecation.rb 3.5 KB
Newer Older
1 2
module ActiveSupport
  module Deprecation
3 4 5 6 7 8 9
    # 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 },
    }

10
    class << self
11 12 13
      def warn(message = nil, callstack = caller)
        behavior.call(deprecation_message(callstack, message)) if behavior
      end
14

15 16
      def default_behavior
        DEFAULT_BEHAVIORS[RAILS_ENV.to_s] if defined?(RAILS_ENV)
17
      end
18 19 20 21 22 23 24 25 26 27

      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
28 29
        end
    end
30 31 32 33 34

    # Behavior is a block that takes a message argument.
    mattr_accessor :behavior
    self.behavior = default_behavior

35
    module ClassMethods
36 37 38 39 40 41 42 43 44 45
      # 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)
46 47 48
        end
      end
    end
49 50

    module Assertions
51
      def assert_deprecated(match = nil, &block)
52 53
        last = with_last_message_tracking_deprecation_behavior(&block)
        assert last, "Expected a deprecation warning within the block but received none"
54 55
        match = Regexp.new(match) unless match.is_a?(Regexp)
        assert_match match, last, "Deprecation warning didn't match #{match}: #{last}"
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
      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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

    # 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
94 95 96
  end
end

97 98 99 100 101 102 103 104 105 106 107
class Class
  include ActiveSupport::Deprecation::ClassMethods
end

module Test
  module Unit
    class TestCase
      include ActiveSupport::Deprecation::Assertions
    end
  end
end