Class: ActiveSupport::Deprecation::DeprecatedConstantProxy

Inherits:
Module
  • Object
show all
Defined in:
activesupport/lib/active_support/deprecation/proxy_wrappers.rb

Overview

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)

# (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.
     (Backtrace information…)
     ["Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"]

Constant Summary

Constants inherited from Module

Module::DELEGATION_RESERVED_KEYWORDS, Module::DELEGATION_RESERVED_METHOD_NAMES, Module::RUBY_RESERVED_KEYWORDS

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#alias_attribute, #anonymous?, #attr_internal_accessor, #attr_internal_reader, #attr_internal_writer, #delegate, #delegate_missing_to, #deprecate, #mattr_accessor, #mattr_reader, #mattr_writer, #method_visibility, #module_parent, #module_parent_name, #module_parents, #parent, #parent_name, #parents, #redefine_method, #redefine_singleton_method, #remove_possible_method, #remove_possible_singleton_method, #silence_redefinition_of_method, #thread_mattr_accessor, #thread_mattr_reader, #thread_mattr_writer

Methods included from Module::Concerning

#concern, #concerning

Constructor Details

#initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance, message: "#{old_const} is deprecated! Use #{new_const} instead.") ⇒ DeprecatedConstantProxy

Returns a new instance of DeprecatedConstantProxy.



131
132
133
134
135
136
137
138
# File 'activesupport/lib/active_support/deprecation/proxy_wrappers.rb', line 131

def initialize(old_const, new_const, deprecator = ActiveSupport::Deprecation.instance, message: "#{old_const} is deprecated! Use #{new_const} instead.")
  Kernel.require "active_support/inflector/methods"

  @old_const = old_const
  @new_const = new_const
  @deprecator = deprecator
  @message = message
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(called, *args, &block) ⇒ Object (private)



171
172
173
174
# File 'activesupport/lib/active_support/deprecation/proxy_wrappers.rb', line 171

def method_missing(called, *args, &block)
  @deprecator.warn(@message, caller_locations)
  target.__send__(called, *args, &block)
end

Class Method Details

.new(*args, **options, &block) ⇒ Object



124
125
126
127
128
129
# File 'activesupport/lib/active_support/deprecation/proxy_wrappers.rb', line 124

def self.new(*args, **options, &block)
  object = args.first

  return object unless object
  super
end

Instance Method Details

#classObject

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


157
158
159
# File 'activesupport/lib/active_support/deprecation/proxy_wrappers.rb', line 157

def class
  target.class
end

#inspectObject

Don’t give a deprecation warning on inspect since test/unit and error logs rely on it for diagnostics.



144
145
146
# File 'activesupport/lib/active_support/deprecation/proxy_wrappers.rb', line 144

def inspect
  target.inspect
end