Active Support 廢棄
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
命名空間
- MODULE ActiveSupport::Deprecation::Behavior
- MODULE ActiveSupport::Deprecation::DeprecatedConstantAccessor
- MODULE ActiveSupport::Deprecation::Disallowed
- MODULE ActiveSupport::Deprecation::MethodWrapper
- MODULE ActiveSupport::Deprecation::Reporting
- CLASS ActiveSupport::Deprecation::DeprecatedConstantProxy
- CLASS ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy
- CLASS ActiveSupport::Deprecation::DeprecatedObjectProxy
- CLASS 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')
# 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