Active Support 弃用
弃用指定了 Rails 用于弃用方法、实例变量、对象和常量的 API。它也可用于 gems 或应用程序。
对于 gem,使用 Deprecation.new
创建一个 Deprecation
对象并将其存储在您的模块或类中(以便用户能够配置它)。
module MyLibrary
def self.deprecator
@deprecator ||= ActiveSupport::Deprecation.new("2.0", "MyLibrary")
end
end
对于 Railtie 或 Engine,您可能还想将其添加到应用程序的弃用器中,以便应用程序的配置可以应用于它。
module MyLibrary
class Railtie < Rails::Railtie
initializer "my_library.deprecator" do |app|
app.deprecators[:my_library] = MyLibrary.deprecator
end
end
end
使用上面的初始化程序,以下配置设置将影响 MyLibrary.deprecator
# in config/environments/test.rb
config.active_support.deprecation = :raise
命名空间
- 模块 ActiveSupport::Deprecation::Behavior
- 模块 ActiveSupport::Deprecation::DeprecatedConstantAccessor
- 模块 ActiveSupport::Deprecation::Disallowed
- 模块 ActiveSupport::Deprecation::MethodWrapper
- 模块 ActiveSupport::Deprecation::Reporting
- 类 ActiveSupport::Deprecation::DeprecatedConstantProxy
- 类 ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy
- 类 ActiveSupport::Deprecation::DeprecatedObjectProxy
- 类 ActiveSupport::Deprecation::Deprecators
方法
- N
包含的模块
- ActiveSupport::Deprecation::Behavior
- ActiveSupport::Deprecation::Reporting
- ActiveSupport::Deprecation::Disallowed
- ActiveSupport::Deprecation::MethodWrapper
常量
DEFAULT_BEHAVIORS | = | { raise: ->(message, callstack, deprecator) do e = DeprecationException.new(message) e.set_backtrace(callstack.map(&:to_s)) raise e end, stderr: ->(message, callstack, deprecator) do $stderr.puts(message) $stderr.puts callstack.join("\n ") if deprecator.debug end, log: ->(message, callstack, deprecator) do logger = if defined?(Rails.logger) && Rails.logger Rails.logger else require "active_support/logger" ActiveSupport::Logger.new($stderr) end logger.warn message logger.debug callstack.join("\n ") if deprecator.debug end, notify: ->(message, callstack, deprecator) do ActiveSupport::Notifications.instrument( "deprecation.#{deprecator.gem_name.underscore.tr("/", "_")}", message: message, callstack: callstack, gem_name: deprecator.gem_name, deprecation_horizon: deprecator.deprecation_horizon, ) end, silence: ->(message, callstack, deprecator) { }, report: ->(message, callstack, deprecator) do error = DeprecationException.new(message) error.set_backtrace(callstack.map(&:to_s)) ActiveSupport.error_reporter.report(error) end } |
每个 |
属性
[RW] | deprecation_horizon | 弃用行为将被删除的版本号,默认值。 |
类公共方法
new(deprecation_horizon = "8.1", gem_name = "Rails") 链接
它在初始化时接受两个参数。第一个是库的版本,第二个是库名称。
ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation.rb, line 71 def initialize(deprecation_horizon = "8.1", gem_name = "Rails") self.gem_name = gem_name self.deprecation_horizon = deprecation_horizon # By default, warnings are not silenced and debugging is off. self.silenced = false self.debug = false @silence_counter = Concurrent::ThreadLocalVar.new(0) @explicitly_allowed_warnings = Concurrent::ThreadLocalVar.new(nil) end