1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activesupport/lib/active_support/deprecation/proxy_wrappers.rb

150 lines
5.1 KiB
Ruby
Raw Normal View History

require 'active_support/inflector/methods'
2009-04-18 00:29:30 -04:00
module ActiveSupport
class Deprecation
2009-04-18 00:29:30 -04:00
class DeprecationProxy #:nodoc:
def self.new(*args, &block)
object = args.first
2010-07-26 12:57:29 -04:00
return object unless object
super
end
2010-07-26 12:57:29 -04:00
2009-04-26 18:56:08 -04:00
instance_methods.each { |m| undef_method m unless m =~ /^__|^object_id$/ }
2009-04-18 00:29:30 -04:00
# 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_locations, called, args
2009-04-18 00:29:30 -04:00
target.__send__(called, *args, &block)
end
end
# DeprecatedObjectProxy transforms an object into a deprecated one. It
# takes an object, a deprecation message and optionally a deprecator. The
# deprecator defaults to +ActiveSupport::Deprecator+ if none is specified.
#
# deprecated_object = ActiveSupport::Deprecation::DeprecatedObjectProxy.new(Object.new, "This object is now deprecated")
# # => #<Object:0x007fb9b34c34b0>
#
# deprecated_object.to_s
# DEPRECATION WARNING: This object is now deprecated.
# (Backtrace)
# # => "#<Object:0x007fb9b34c34b0>"
class DeprecatedObjectProxy < DeprecationProxy
def initialize(object, message, deprecator = ActiveSupport::Deprecation.instance)
2009-04-18 00:29:30 -04:00
@object = object
@message = message
@deprecator = deprecator
2009-04-18 00:29:30 -04:00
end
private
def target
@object
end
def warn(callstack, called, args)
@deprecator.warn(@message, callstack)
2009-04-18 00:29:30 -04:00
end
end
# DeprecatedInstanceVariableProxy transforms an instance variable into a
# deprecated one. It takes an instance of a class, a method on that class
# and an instance variable. It optionally takes a deprecator as the last
# argument. The deprecator defaults to +ActiveSupport::Deprecator+ if none
# is specified.
#
# class Example
# def initialize
# @request = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(self, :request, :@request)
# @_request = :special_request
# end
#
# def request
# @_request
# end
#
# def old_request
# @request
# end
# end
#
# example = Example.new
# # => #<Example:0x007fb9b31090b8 @_request=:special_request, @request=:special_request>
#
# example.old_request.to_s
# # => DEPRECATION WARNING: @request is deprecated! Call request.to_s instead of
# @request.to_s
# (Bactrace information…)
# "special_request"
#
# example.request.to_s
# # => "special_request"
class DeprecatedInstanceVariableProxy < DeprecationProxy
def initialize(instance, method, var = "@#{method}", deprecator = ActiveSupport::Deprecation.instance)
@instance = instance
@method = method
@var = var
@deprecator = deprecator
2009-04-18 00:29:30 -04:00
end
private
def target
@instance.__send__(@method)
end
def warn(callstack, called, args)
@deprecator.warn("#{@var} is deprecated! Call #{@method}.#{called} instead of #{@var}.#{called}. Args: #{args.inspect}", callstack)
2009-04-18 00:29:30 -04:00
end
end
# DeprecatedConstantProxy transforms a constant into a deprecated one. It
# takes the names of an old (deprecated) constant and of a new constant
# (both in string form) and optionally a deprecator. The deprecator defaults
# to +ActiveSupport::Deprecator+ if none is specified. The deprecated constant
# now returns the value of the new one.
#
# PLANETS = %w(mercury venus earth mars jupiter saturn uranus neptune pluto)
#
2015-09-19 16:10:42 -04:00
# (In a later update, the original implementation of `PLANETS` has been removed.)
#
# PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
# PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
#
# PLANETS.map { |planet| planet.capitalize }
# # => DEPRECATION WARNING: PLANETS is deprecated! Use PLANETS_POST_2006 instead.
# (Bactrace information…)
# ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]
class DeprecatedConstantProxy < DeprecationProxy
def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance)
2009-04-18 00:29:30 -04:00
@old_const = old_const
@new_const = new_const
@deprecator = deprecator
2009-04-18 00:29:30 -04:00
end
# Returns the class of the new constant.
#
# PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
# PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
# PLANETS.class # => Array
2009-04-18 00:29:30 -04:00
def class
target.class
end
private
def target
ActiveSupport::Inflector.constantize(@new_const.to_s)
2009-04-18 00:29:30 -04:00
end
def warn(callstack, called, args)
@deprecator.warn("#{@old_const} is deprecated! Use #{@new_const} instead.", callstack)
2009-04-18 00:29:30 -04:00
end
end
end
end