Module: ActiveModel::Attributes

Extended by:
ActiveSupport::Concern
Includes:
AttributeMethods
Defined in:
lib/active_model/attributes.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Constant Summary

Constants included from AttributeMethods

ActiveModel::AttributeMethods::CALL_COMPILABLE_REGEXP, ActiveModel::AttributeMethods::FORWARD_PARAMETERS, ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP

Instance Method Summary collapse

Methods included from AttributeMethods

#attribute_missing, #method_missing, #respond_to?, #respond_to_without_attributes?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ActiveModel::AttributeMethods

Instance Method Details

#attribute_namesObject

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


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

def attribute_names
  @attributes.keys
end

#attributesObject

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(name: 'Francesco', age: 22)
person.attributes
# => {"name"=>"Francesco", "age"=>22}


100
101
102
# File 'lib/active_model/attributes.rb', line 100

def attributes
  @attributes.to_hash
end

#freezeObject



120
121
122
123
# File 'lib/active_model/attributes.rb', line 120

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

#initializeObject



78
79
80
81
# File 'lib/active_model/attributes.rb', line 78

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

#initialize_dup(other) ⇒ Object

:nodoc:



83
84
85
86
# File 'lib/active_model/attributes.rb', line 83

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