Active Support Deprecation
Deprecation 指定 Rails 用於捨棄方法、實例變數、物件和常數的 API。它也可用於 gem 或應用程式。
對於 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 = "7.2", gem_name = "Rails") 連結
它接受兩個初始化參數。第一個是程式庫版本,第二個是程式庫名稱。
ActiveSupport::Deprecation.new('2.0', 'MyLibrary')
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation.rb, line 68 def initialize(deprecation_horizon = "7.2", 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