Spec-Zone.ru › Ruby on Rails 6.1

модуль ActiveSupport::Deprecation::DeprecatedConstantAccessor

DeprecatedConstantAccessor преобразует константу в устаревшую, подключая const_missing.

Он принимает имена старой (устаревшей) константы и новой константы (оба в строковом виде) и необязательно депрекатор. Депрекатор по умолчанию ActiveSupport::Deprecator если не указано иначе.

Теперь устаревшая константа возвращает тот же объект, что и новая, а не прокси-объект, поэтому её можно использовать прозрачно в rescue блоках и т. д.

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)
include ActiveSupport::Deprecation::DeprecatedConstantAccessor
deprecate_constant '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"]

Публичные методы класса

included(base) Показать исходный код
# File activesupport/lib/active_support/deprecation/constant_accessor.rb, line 29
def self.included(base)
  require "active_support/inflector/methods"

  extension = Module.new do
    def const_missing(missing_const_name)
      if class_variable_defined?(:@@_deprecated_constants)
        if (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
          replacement[:deprecator].warn(replacement[:message] || "#{name}::#{missing_const_name} is deprecated! Use #{replacement[:new]} instead.", caller_locations)
          return ActiveSupport::Inflector.constantize(replacement[:new].to_s)
        end
      end
      super
    end

    def deprecate_constant(const_name, new_constant, message: nil, deprecator: ActiveSupport::Deprecation.instance)
      class_variable_set(:@@_deprecated_constants, {}) unless class_variable_defined?(:@@_deprecated_constants)
      class_variable_get(:@@_deprecated_constants)[const_name.to_s] = { new: new_constant, message: message, deprecator: deprecator }
    end
  end
  base.singleton_class.prepend extension
end

Публичные методы экземпляра

const_missing(missing_const_name) Показать исходный код
# File activesupport/lib/active_support/deprecation/constant_accessor.rb, line 33
def const_missing(missing_const_name)
  if class_variable_defined?(:@@_deprecated_constants)
    if (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
      replacement[:deprecator].warn(replacement[:message] || "#{name}::#{missing_const_name} is deprecated! Use #{replacement[:new]} instead.", caller_locations)
      return ActiveSupport::Inflector.constantize(replacement[:new].to_s)
    end
  end
  super
end
Вызов метода суперкласса
deprecate_constant(const_name, new_constant, message: nil, deprecator: ActiveSupport::Deprecation.instance) Показать исходный код
# File activesupport/lib/active_support/deprecation/constant_accessor.rb, line 43
def deprecate_constant(const_name, new_constant, message: nil, deprecator: ActiveSupport::Deprecation.instance)
  class_variable_set(:@@_deprecated_constants, {}) unless class_variable_defined?(:@@_deprecated_constants)
  class_variable_get(:@@_deprecated_constants)[const_name.to_s] = { new: new_constant, message: message, deprecator: deprecator }
end

© 2004–2020 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API