Behavior
模組可用來決定如何顯示不贊成使用的訊息。您可以建立自訂行為或是設定 DEFAULT_BEHAVIORS
常數中的任一行為。可用的行為如下
:raise
:stderr
-
將所有不贊成使用警告記錄於
$stderr
。 :log
-
將所有不贊成使用警告記錄於
Rails.logger
。 :notify
-
使用
ActiveSupport::Notifications
來通知deprecation.rails
。 :report
-
使用
ActiveSupport::ErrorReporter
來回報不贊成使用事項。 :silence
-
不執行任何動作。在 Rails 上,設定
config.active_support.report_deprecations = false
可停用所有行為。
設定行為僅影響開機時間後發生的不贊成使用事項。更多資訊,您可以參閱 behavior=
方法的說明文件。
屬性
[RW] | debug | 是否要列印追蹤資訊和警告。 |
執行個體公開方法
behavior() 連結
傳回目前的行為,如果沒有設定,則預設為 :stderr
。
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 74 def behavior @behavior ||= [DEFAULT_BEHAVIORS[:stderr]] end
behavior=(behavior) 連結
將 behavior 設定為指定的數值。可以是單一值、陣列、或回應「呼叫」的物件。
可用的行為
:raise
:stderr
-
將所有不贊成使用警告記錄於
$stderr
。 :log
-
將所有不贊成使用警告記錄於
Rails.logger
。 :notify
-
使用
ActiveSupport::Notifications
來通知deprecation.rails
。 :report
-
使用
ActiveSupport::ErrorReporter
來回報不贊成使用事項。 :silence
-
不執行任何動作。
設定行為僅影響開機時間後發生的不贊成使用事項。 Deprecation
警告是由寶石引發,不會受到這個設定影響,因為它們發生在 Rails 啟動之前。
deprecator = ActiveSupport::Deprecation.new
deprecator.behavior = :stderr
deprecator.behavior = [:stderr, :log]
deprecator.behavior = MyCustomHandler
deprecator.behavior = ->(message, callstack, deprecation_horizon, gem_name) {
# custom stuff
}
如果您正在使用 Rails,您可以設定 config.active_support.report_deprecations = false
來停用所有不贊成使用行為。這類似的 :silence
選項但效能更好。
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 111 def behavior=(behavior) @behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) } end
disallowed_behavior() 連結
傳回目前對不允許的不贊成使用事項的行為,如果沒有設定,則預設為 :raise
。
來源: 顯示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 79 def disallowed_behavior @disallowed_behavior ||= [DEFAULT_BEHAVIORS[:raise]] end
disallowed_behavior=(behavior) 連結
將禁止的過時(已透過ActiveSupport::Deprecation#disallowed_warnings=
設定)的行為設定為指定的值。如同 behavior=
,這可以是單一值、陣列或一個對應到 call
的物件。
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/deprecation/behaviors.rb, line 119 def disallowed_behavior=(behavior) @disallowed_behavior = Array(behavior).map { |b| DEFAULT_BEHAVIORS[b] || arity_coerce(b) } end