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


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

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}


97
98
99
# File 'lib/active_model/attributes.rb', line 97

def attributes
  @attributes.to_hash
end

#freezeObject



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

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

#initializeObject



75
76
77
78
# File 'lib/active_model/attributes.rb', line 75

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

#initialize_dup(other) ⇒ Object

:nodoc:



80
81
82
83
# File 'lib/active_model/attributes.rb', line 80

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