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,
activemodel/lib/active_model/attributes/normalization.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, 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.

append_features, prepend_features

::ActiveSupport::Autoload - Extended

Instance Method Summary

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.

#_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 ]


39
40
41
# File 'activemodel/lib/active_model/attributes.rb', line 39

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 160

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 165

def attribute(attr_name)
  @attributes.fetch_value(attr_name)
end

#attribute=(attr_name, value) (private)

Alias for #_write_attribute.

[ GitHub ]

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

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

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

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 135

def attributes
  @attributes.to_hash
end

#freeze

This method is for internal use only.
[ GitHub ]

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

def freeze # :nodoc:
  @attributes = @attributes.clone.freeze unless frozen?
  super
end

#initialize

This method is for internal use only.
[ GitHub ]

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

def initialize(*) # :nodoc:
  @attributes = self.class._default_attributes.deep_dup
  super
end

#initialize_dup(other)

This method is for internal use only.
[ GitHub ]

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

def initialize_dup(other) # :nodoc:
  @attributes = @attributes.deep_dup
  super
end