module ActiveModel::Attributes::ClassMethods
def attribute(name, ...)
person.name # => "Volmer"
person.name = "Volmer"
person = Person.new
end
attribute :active, :boolean, default: true
attribute :name, :string
include ActiveModel::Attributes
class Person
supported by the given cast type.
type and default value may be specified, as well as any options
Defines a model attribute. In addition to the attribute name, a cast
:call-seq: attribute(name, cast_type = nil, default: nil, **options)
#
def attribute(name, ...) super define_attribute_method(name) end
def attribute_names
end
attribute :age, :integer
attribute :name, :string
include ActiveModel::Attributes
class Person
Returns an array of attribute names as strings.
def attribute_names attribute_types.keys end
def define_method_attribute=(canonical_name, owner:, as: canonical_name)
def define_method_attribute=(canonical_name, owner:, as: canonical_name) ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method( owner, canonical_name, writer: true, ) do |temp_method_name, attr_name_expr| owner.define_cached_method(temp_method_name, as: "#{as}=", namespace: :active_model) do |batch| batch << "def #{temp_method_name}(value)" << " _write_attribute(#{attr_name_expr}, value)" << "end" end end end