модуль ActiveModel::Attributes::ClassMethods
Публичные методы экземпляра
attribute(name, cast_type = nil, default: nil, **options) Показать исходный код
# File activemodel/lib/active_model/attributes.rb, line 59 def attribute(name, ...) super define_attribute_method(name) end
Определяет атрибут модели. Помимо имени атрибута, можно указать тип преобразования и значение по умолчанию, а также любые параметры, поддерживаемые данным типом преобразования.
class Person include ActiveModel::Attributes attribute :name, :string attribute :active, :boolean, default: true end person = Person.new person.name = "Volmer" person.name # => "Volmer" person.active # => true
Вызывает метод суперкласса
# File activemodel/lib/active_model/attributes.rb, line 74 def attribute_names attribute_types.keys end
Возвращает массив имён атрибутов в виде строк.
class Person include ActiveModel::Attributes attribute :name, :string attribute :age, :integer end Person.attribute_names # => ["name", "age"]
© 2004–2021 David Heinemeier Hansson
Licensed under the MIT License.