Module: ActiveModel::Attributes::ClassMethods

Defined in:
activemodel/lib/active_model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type = Type::Value.new, **options) ⇒ Object


19
20
21
22
23
24
25
26
27
# File 'activemodel/lib/active_model/attributes.rb', line 19

def attribute(name, type = Type::Value.new, **options)
  name = name.to_s
  if type.is_a?(Symbol)
    type = ActiveModel::Type.lookup(type, **options.except(:default))
  end
  self.attribute_types = attribute_types.merge(name => type)
  define_default_attribute(name, options.fetch(:default, NO_DEFAULT_PROVIDED), type)
  define_attribute_method(name)
end

#attribute_namesObject

Returns an array of attribute names as strings

class Person
  include ActiveModel::Attributes

  attribute :name, :string
  attribute :age, :integer
end

Person.attribute_names
# => ["name", "age"]

40
41
42
# File 'activemodel/lib/active_model/attributes.rb', line 40

def attribute_names
  attribute_types.keys
end