Spec-Zone.ru › Ruby on Rails 8.1

класс ActiveModel::Error

Родительский класс:
Object

Ошибка Active Model

Представляет одну отдельную ошибку

Константы

CALLBACKS_OPTIONS
MESSAGE_OPTIONS

Атрибуты

attribute [R]

Атрибут base, к которому относится ошибка

base [R]

Объект, к которому относится ошибка

options [R]

Параметры, переданные при вызове errors#add

raw_type [R]

Исходное значение, переданное в качестве второго параметра при вызове errors#add

type [R]

Тип ошибки; по умолчанию — :invalid, если не указано иное

Открытые методы класса

new (base, attribute, type = :invalid, **options) Показать исходный код
# File activemodel/lib/active_model/error.rb, line 102
def initialize(base, attribute, type = :invalid, **options)
  @base = base
  @attribute = attribute
  @raw_type = type
  @type = type || :invalid
  @options = options
end

Открытые методы экземпляра

detail ()
Псевдоним для: details
details () Показать исходный код
# File activemodel/lib/active_model/error.rb, line 148
def details
  { error: raw_type }.merge(options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS))
end

Возвращает сведения об ошибке.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.details
# => { error: :too_short, count: 5 }
Также имеет псевдоним: detail
full_message () Показать исходный код
# File activemodel/lib/active_model/error.rb, line 158
def full_message
  self.class.full_message(attribute, message, @base)
end

Возвращает полное сообщение об ошибке.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.full_message
# => "Name is too short (minimum is 5 characters)"
match? (attribute, type = nil, **options) Показать исходный код
# File activemodel/lib/active_model/error.rb, line 165
def match?(attribute, type = nil, **options)
  if @attribute != attribute || (type && @type != type)
    return false
  end

  options.each do |key, value|
    if @options[key] != value
      return false
    end
  end

  true
end

Проверяет, соответствует ли ошибка переданным attribute, type и options.

Пропущенные параметры не участвуют в проверке на соответствие.

message () Показать исходный код
# File activemodel/lib/active_model/error.rb, line 134
def message
  case raw_type
  when Symbol
    self.class.generate_message(attribute, raw_type, @base, options.except(*CALLBACKS_OPTIONS))
  else
    raw_type
  end
end

Возвращает сообщение об ошибке.

error = ActiveModel::Error.new(person, :name, :too_short, count: 5)
error.message
# => "is too short (minimum is 5 characters)"
strict_match? (attribute, type, **options) Показать исходный код
# File activemodel/lib/active_model/error.rb, line 183
def strict_match?(attribute, type, **options)
  return false unless match?(attribute, type)

  options == @options.except(*CALLBACKS_OPTIONS + MESSAGE_OPTIONS)
end

Проверяет, точно ли ошибка соответствует переданным attribute, type и options.

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

Защищённые методы экземпляра

attributes_for_hash () Показать исходный код
# File activemodel/lib/active_model/error.rb, line 203
def attributes_for_hash
  [@base, @attribute, @raw_type, @options.except(*CALLBACKS_OPTIONS)]
end

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

Spec-Zone.ru

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