Module: ActiveModel::Attributes::ClassMethods

Defined in:
lib/active_model/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name) ⇒ Object

:call-seq: attribute(name, cast_type = nil, default: nil, **options)

Defines a model attribute. In addition to the attribute name, a cast type and default value may be specified, as well as any options supported by the given cast type.

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


59
60
61
62
# File 'lib/active_model/attributes.rb', line 59

def attribute(name, ...)
  super
  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"]


74
75
76
# File 'lib/active_model/attributes.rb', line 74

def attribute_names
  attribute_types.keys
end