123456789_123456789_123456789_123456789_123456789_

Module: ActiveModel::Attributes

Relationships & Source Files
Namespace Children
Modules:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
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 - Included

CALL_COMPILABLE_REGEXP, FORWARD_PARAMETERS, NAME_COMPILABLE_REGEXP

AttributeMethods - Attributes & Methods

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

attribute_missing is like method_missing, but for attributes.

#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 Person instance with a name attribute can ask person.respond_to?(:name), person.respond_to?(:name=), and person.respond_to?(:name?) which will all return true.

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 72

class_attribute :attribute_aliases, instance_writer: false, default: {}

.attribute_aliases?Boolean (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_methods.rb', line 72

class_attribute :attribute_aliases, instance_writer: false, default: {}

.attribute_method_patterns (rw)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_methods.rb', line 73

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 73

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 72

class_attribute :attribute_aliases, instance_writer: false, default: {}

#attribute_aliases?Boolean (readonly)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_methods.rb', line 72

class_attribute :attribute_aliases, instance_writer: false, default: {}

#attribute_method_patterns (readonly)

[ GitHub ]

  
# File 'activemodel/lib/active_model/attribute_methods.rb', line 73

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 73

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"]
[ GitHub ]

  
# File 'activemodel/lib/active_model/attributes.rb', line 133

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}
[ GitHub ]

  
# File 'activemodel/lib/active_model/attributes.rb', line 118

def attributes
  @attributes.to_hash
end