класс ActiveSupport::Deprecation::DeprecatedConstantProxy
DeprecatedConstantProxy преобразует константу в устаревшую. Он принимает полные имена старой (устаревшей) константы и новой константы (оба в строковом формате) и утилиту для устаревания. Теперь устаревшая константа возвращает значение новой.
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", ActiveSupport::Deprecation.new)
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"]
Публичные методы класса
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 123 def self.new(*args, **options, &block) object = args.first return object unless object super end
Вызывает метод суперкласса
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 130
def initialize(old_const, new_const, deprecator = nil, message: "#{old_const} is deprecated! Use #{new_const} instead.")
Kernel.require "active_support/inflector/methods"
@old_const = old_const
@new_const = new_const
ActiveSupport.deprecator.warn("DeprecatedConstantProxy without a deprecator is deprecated") unless deprecator
@deprecator = deprecator || ActiveSupport::Deprecation._instance
@message = message
end Публичные методы экземпляра
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 161 def append_features(base) @deprecator.warn(@message, caller_locations) base.include(target) end
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 157 def class target.class end
Возвращает класс новой константы.
PLANETS_POST_2006 = %w(mercury venus earth mars jupiter saturn uranus neptune)
PLANETS = ActiveSupport::Deprecation::DeprecatedConstantProxy.new('PLANETS', 'PLANETS_POST_2006')
PLANETS.class # => Array
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 171 def extended(base) @deprecator.warn(@message, caller_locations) base.extend(target) end
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 144 def inspect target.inspect end
Не выводить предупреждение об устаревании в inspect, так как test/unit и журналы ошибок полагаются на него для диагностики.
# File activesupport/lib/active_support/deprecation/proxy_wrappers.rb, line 166 def prepend_features(base) @deprecator.warn(@message, caller_locations) base.prepend(target) end
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.