Module: ActiveModel::Attributes
- Extended by:
- ActiveSupport::Concern
- Includes:
- AttributeMethods, AttributeRegistration
- Defined in:
- lib/active_model/attributes.rb
Overview
Active Model Attributes
The Attributes module allows models to define attributes beyond simple Ruby readers and writers. Similar to Active Record attributes, which are typically inferred from the database schema, Active Model Attributes are aware of data types, can have default values, and can handle casting and serialization.
To use Attributes, include the module in your model class and define your attributes using the attribute
macro. It accepts a name, a type, a default value, and any other options supported by the attribute type.
Examples
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
Defined Under Namespace
Modules: ClassMethods
Constant Summary
Constants included from AttributeMethods
ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP, ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP
Instance Method Summary collapse
-
#attribute_names ⇒ Object
Returns an array of attribute names as strings.
-
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
-
#freeze ⇒ Object
:nodoc:.
-
#initialize ⇒ Object
:nodoc:.
-
#initialize_dup(other) ⇒ Object
:nodoc:.
Methods included from AttributeMethods
#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods
Instance Method Details
#attribute_names ⇒ Object
Returns an array of attribute names as strings.
class Person
include ActiveModel::Attributes
attribute :name, :string
attribute :age, :integer
end
person = Person.new
person.attribute_names # => ["name", "age"]
146 147 148 |
# File 'lib/active_model/attributes.rb', line 146 def attribute_names @attributes.keys end |
#attributes ⇒ Object
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
class Person
include ActiveModel::Attributes
attribute :name, :string
attribute :age, :integer
end
person = Person.new
person.name = "Francesco"
person.age = 22
person.attributes # => { "name" => "Francesco", "age" => 22}
131 132 133 |
# File 'lib/active_model/attributes.rb', line 131 def attributes @attributes.to_hash end |
#freeze ⇒ Object
:nodoc:
150 151 152 153 |
# File 'lib/active_model/attributes.rb', line 150 def freeze # :nodoc: @attributes = @attributes.clone.freeze unless frozen? super end |
#initialize ⇒ Object
:nodoc:
106 107 108 109 |
# File 'lib/active_model/attributes.rb', line 106 def initialize(*) # :nodoc: @attributes = self.class._default_attributes.deep_dup super end |
#initialize_dup(other) ⇒ Object
:nodoc:
111 112 113 114 |
# File 'lib/active_model/attributes.rb', line 111 def initialize_dup(other) # :nodoc: @attributes = @attributes.deep_dup super end |