module ActiveModel::Attributes::ClassMethods
def attribute(name, cast_type = nil, default: NO_DEFAULT_PROVIDED, **options)
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
def attribute_names
Person.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_default_attribute(name, value, type)
def define_default_attribute(name, value, type) self._default_attributes = _default_attributes.deep_dup if value == NO_DEFAULT_PROVIDED default_attribute = _default_attributes[name].with_type(type) else default_attribute = Attribute::UserProvidedDefault.new( name, value, type, _default_attributes.fetch(name.to_s) { nil }, ) end _default_attributes[name] = default_attribute end
def define_method_attribute=(name, owner:)
def define_method_attribute=(name, owner:) ActiveModel::AttributeMethods::AttrNames.define_attribute_accessor_method( owner, name, writer: true, ) do |temp_method_name, attr_name_expr| owner.define_cached_method("#{name}=", as: temp_method_name, namespace: :active_model) do |batch| batch << "def #{temp_method_name}(value)" << " _write_attribute(#{attr_name_expr}, value)" << "end" end end end