Module: ActiveModel::Attributes
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Instance Chain:
self,
AttributeMethods
|
|
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. |
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.
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 |
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
#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