proxy_wrappers.rb 2.2 KB
Newer Older
1
require 'active_support/inflector/methods'
2

3 4 5
module ActiveSupport
  module Deprecation
    class DeprecationProxy #:nodoc:
6 7
      def self.new(*args, &block)
        object = args.first
A
Aaron Patterson 已提交
8

9 10 11
        return object unless object
        super
      end
A
Aaron Patterson 已提交
12

J
Jeremy Kemper 已提交
13
      instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

      # 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

61
    class DeprecatedConstantProxy < DeprecationProxy #:nodoc:all
62 63 64 65 66 67 68 69 70 71 72
      def initialize(old_const, new_const)
        @old_const = old_const
        @new_const = new_const
      end

      def class
        target.class
      end

      private
        def target
73
          ActiveSupport::Inflector.constantize(@new_const.to_s)
74 75 76 77 78 79 80 81
        end

        def warn(callstack, called, args)
          ActiveSupport::Deprecation.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
        end
    end
  end
end