Spec-Zone.ru › Ruby on Rails 7.0

модуль ActiveModel::Attributes::ClassMethods

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

attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options) Показать исходный код
# File activemodel/lib/active_model/attributes.rb, line 19
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
  name = name.to_s

  cast_type = Type.lookup(cast_type, **options) if Symbol === cast_type
  cast_type ||= attribute_types[name]

  self.attribute_types = attribute_types.merge(name => cast_type)
  define_default_attribute(name, default, cast_type)
  define_attribute_method(name)
end
attribute_names() Показать исходный код
# File activemodel/lib/active_model/attributes.rb, line 41
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.

Spec-Zone.ru

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