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

3
module ActiveSupport
4
  class Deprecation
5
    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

      # 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

28
    # This DeprecatedObjectProxy transforms object to deprecated object.
29 30 31 32
    #
    #   @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!")
    #   @old_object = DeprecatedObjectProxy.new(Object.new, "Don't use this object anymore!", deprecator_instance)
    #
33
    # When someone executes any method except +inspect+ on proxy object this will
34
    # trigger +warn+ method on +deprecator_instance+.
35
    #
36
    # Default deprecator is <tt>ActiveSupport::Deprecation</tt>
37 38
    class DeprecatedObjectProxy < DeprecationProxy
      def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
39 40
        @object = object
        @message = message
41
        @deprecator = deprecator
42 43 44 45 46 47 48 49
      end

      private
        def target
          @object
        end

        def warn(callstack, called, args)
50
          @deprecator.warn(@message, callstack)
51 52 53
        end
    end

54
    # This DeprecatedInstanceVariableProxy transforms instance variable to
55
    # deprecated instance variable.
56
    #
57 58 59 60 61
    #   class Example
    #     def initialize(deprecator)
    #       @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request, deprecator)
    #       @_request = :a_request
    #     end
62
    #
63
    #     def request
64 65 66
    #       @_request
    #     end
    #
67
    #     def old_request
68
    #       @request
69 70
    #     end
    #   end
71
    #
72 73 74 75
    # When someone execute any method on @request variable this will trigger
    # +warn+ method on +deprecator_instance+ and will fetch <tt>@_request</tt>
    # variable via +request+ method and execute the same method on non-proxy
    # instance variable.
76
    #
77
    # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
78 79
    class DeprecatedInstanceVariableProxy < DeprecationProxy
      def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
80 81 82
        @instance = instance
        @method = method
        @var = var
83
        @deprecator = deprecator
84 85 86 87 88 89 90 91
      end

      private
        def target
          @instance.__send__(@method)
        end

        def warn(callstack, called, args)
92
          @deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
93 94 95
        end
    end

96
    # This DeprecatedConstantProxy transforms constant to deprecated constant.
97 98 99 100
    #
    #   OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST')
    #   OLD_CONST = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('OLD_CONST', 'NEW_CONST', deprecator_instance)
    #
101 102 103 104
    # When someone use old constant this will trigger +warn+ method on
    # +deprecator_instance+.
    #
    # Default deprecator is <tt>ActiveSupport::Deprecation</tt>.
105 106
    class DeprecatedConstantProxy < DeprecationProxy
      def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
107 108
        @old_const = old_const
        @new_const = new_const
109
        @deprecator = deprecator
110 111 112 113 114 115 116 117
      end

      def class
        target.class
      end

      private
        def target
118
          ActiveSupport::Inflector.constantize(@new_const.to_s)
119 120 121
        end

        def warn(callstack, called, args)
122
          @deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
123 124 125 126
        end
    end
  end
end