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, 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

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 ]


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.

[ GitHub ]

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

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

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

def attributes
  @attributes.to_hash
end

#freeze

This method is for internal use only.
[ GitHub ]

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

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 106

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 111

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