Module: ActiveModel::Attributes
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Instance Chain:
|
|
Defined in: | activemodel/lib/active_model/attributes.rb |
Overview
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
Constant Summary
AttributeMethods
- Attributes & Methods
- .attribute_aliases rw
- #attribute_aliases readonly
- .attribute_aliases? ⇒ Boolean rw
- #attribute_aliases? ⇒ Boolean readonly
- .attribute_method_patterns rw
- #attribute_method_patterns readonly
- .attribute_method_patterns? ⇒ Boolean rw
- #attribute_method_patterns? ⇒ Boolean readonly
Class Method Summary
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
append_features, prepend_features |
Instance Method Summary
-
#attribute_names
Returns an array of attribute names as strings.
-
#attributes
Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
- #_write_attribute(attr_name, value) (also: #attribute=) private
- #attribute(attr_name) private
-
#attribute=(attr_name, value)
private
Alias for #_write_attribute.
- #freeze Internal use only
- #initialize Internal use only
- #initialize_dup(other) Internal use only
AttributeMethods
- Included
#attribute_missing |
|
#method_missing | Allows access to the object attributes, which are held in the hash returned by #attributes, as though they were first-class methods. |
#respond_to?, | |
#respond_to_without_attributes? | A |
#_read_attribute, #attribute_method?, | |
#matched_attribute_method | Returns a struct representing the matching attribute method. |
#missing_attribute |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods
DSL Calls
included
[ GitHub ]35 36 37
# File 'activemodel/lib/active_model/attributes.rb', line 35
included do attribute_method_suffix "=", parameters: "value" end
Class Attribute Details
.attribute_aliases (rw)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_methods.rb', line 71
class_attribute :attribute_aliases, instance_writer: false, default: {}
.attribute_aliases? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute_methods.rb', line 71
class_attribute :attribute_aliases, instance_writer: false, default: {}
.attribute_method_patterns (rw)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_methods.rb', line 72
class_attribute :attribute_method_patterns, instance_writer: false, default: [ ClassMethods::AttributeMethodPattern.new ]
.attribute_method_patterns? ⇒ Boolean
(rw)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute_methods.rb', line 72
class_attribute :attribute_method_patterns, instance_writer: false, default: [ ClassMethods::AttributeMethodPattern.new ]
Instance Attribute Details
#attribute_aliases (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_methods.rb', line 71
class_attribute :attribute_aliases, instance_writer: false, default: {}
#attribute_aliases? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute_methods.rb', line 71
class_attribute :attribute_aliases, instance_writer: false, default: {}
#attribute_method_patterns (readonly)
[ GitHub ]# File 'activemodel/lib/active_model/attribute_methods.rb', line 72
class_attribute :attribute_method_patterns, instance_writer: false, default: [ ClassMethods::AttributeMethodPattern.new ]
#attribute_method_patterns? ⇒ Boolean
(readonly)
[ GitHub ]
# File 'activemodel/lib/active_model/attribute_methods.rb', line 72
class_attribute :attribute_method_patterns, instance_writer: false, default: [ ClassMethods::AttributeMethodPattern.new ]
Instance Method Details
#_write_attribute(attr_name, value) (private) Also known as: #attribute=
[ GitHub ]# File 'activemodel/lib/active_model/attributes.rb', line 156
def _write_attribute(attr_name, value) @attributes.write_from_user(attr_name, value) end
#attribute(attr_name) (private)
[ GitHub ]# File 'activemodel/lib/active_model/attributes.rb', line 161
def attribute(attr_name) @attributes.fetch_value(attr_name) end
#attribute=(attr_name, value) (private)
Alias for #_write_attribute.
# File 'activemodel/lib/active_model/attributes.rb', line 159
alias :attribute= :_write_attribute
#attribute_names
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"]
# File 'activemodel/lib/active_model/attributes.rb', line 146
def attribute_names @attributes.keys end
#attributes
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}
# File 'activemodel/lib/active_model/attributes.rb', line 131
def attributes @attributes.to_hash end
#freeze
# File 'activemodel/lib/active_model/attributes.rb', line 150
def freeze # :nodoc: @attributes = @attributes.clone.freeze unless frozen? super end
#initialize
# File 'activemodel/lib/active_model/attributes.rb', line 106
def initialize(*) # :nodoc: @attributes = self.class._default_attributes.deep_dup super end
#initialize_dup(other)
# File 'activemodel/lib/active_model/attributes.rb', line 111
def initialize_dup(other) # :nodoc: @attributes = @attributes.deep_dup super end