Spec-Zone.ru › Ruby on Rails 6.1

модуль ActiveSupport::Deprecation::Reporting

Константы

RAILS_GEM_ROOT

Атрибуты

gem_name[RW]

Имя драгоценности, в которой метод устарел

silenced[W]

Нужно ли печатать сообщение (режим молчания)

Публичные методы экземпляров

allow(allowed_warnings = :all, if: true) { || ... } Показать исходный код
# File activesupport/lib/active_support/deprecation/reporting.rb, line 72
def allow(allowed_warnings = :all, if: true, &block)
  conditional = binding.local_variable_get(:if)
  conditional = conditional.call if conditional.respond_to?(:call)
  if conditional
    @explicitly_allowed_warnings.bind(allowed_warnings, &block)
  else
    yield
  end
end

Разрешить ранее запрещенные предупреждения об устаревании внутри блока. allowed_warnings может быть массивом, содержащим строки, символы или регулярные выражения. (Символы обрабатываются как строки). Они сравниваются с текстом сообщений о предупреждениях об устаревании, сгенерированных внутри блока. Соответствующие предупреждения будут освобождены от правил, установленных ActiveSupport::Deprecation.disallowed_warnings

Необязательный if: аргумент принимает истинное/ложное значение или объект, который отвечает на .call. Если истинно, то соответствующие предупреждения будут разрешены. Если ложно, то метод передает управление в блок без разрешения предупреждения.

ActiveSupport::Deprecation.disallowed_behavior = :raise
ActiveSupport::Deprecation.disallowed_warnings = [
  "something broke"
]

ActiveSupport::Deprecation.warn('something broke!')
# => ActiveSupport::DeprecationException

ActiveSupport::Deprecation.allow ['something broke'] do
  ActiveSupport::Deprecation.warn('something broke!')
end
# => nil

ActiveSupport::Deprecation.allow ['something broke'], if: Rails.env.production? do
  ActiveSupport::Deprecation.warn('something broke!')
end
# => ActiveSupport::DeprecationException for dev/test, nil for production
deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil) Показать исходный код
# File activesupport/lib/active_support/deprecation/reporting.rb, line 86
def deprecation_warning(deprecated_method_name, message = nil, caller_backtrace = nil)
  caller_backtrace ||= caller_locations(2)
  deprecated_method_warning(deprecated_method_name, message).tap do |msg|
    warn(msg, caller_backtrace)
  end
end
silence(&block) Показать исходный код
# File activesupport/lib/active_support/deprecation/reporting.rb, line 40
def silence(&block)
  @silenced_thread.bind(true, &block)
end

Заглушить предупреждения об устаревании внутри блока.

ActiveSupport::Deprecation.warn('something broke!')
# => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"

ActiveSupport::Deprecation.silence do
  ActiveSupport::Deprecation.warn('something broke!')
end
# => nil
silenced() Показать исходный код
# File activesupport/lib/active_support/deprecation/reporting.rb, line 82
def silenced
  @silenced || @silenced_thread.value
end
warn(message = nil, callstack = nil) Показать исходный код
# File activesupport/lib/active_support/deprecation/reporting.rb, line 18
def warn(message = nil, callstack = nil)
  return if silenced

  callstack ||= caller_locations(2)
  deprecation_message(callstack, message).tap do |m|
    if deprecation_disallowed?(message)
      disallowed_behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
    else
      behavior.each { |b| b.call(m, callstack, deprecation_horizon, gem_name) }
    end
  end
end

Выводит предупреждение об устаревании в выходной поток, настроенный с помощью ActiveSupport::Deprecation.behavior

ActiveSupport::Deprecation.warn('something broke!')
# => "DEPRECATION WARNING: something broke! (called from your_code.rb:1)"

© 2004–2020 David Heinemeier Hansson
Licensed under the MIT License.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API