1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/activerecord/lib/active_record/attributes.rb
Eric Chapweske f936a1f100 Refactoring attributes/types [#3348 state:resolved]
Signed-off-by: Joshua Peek <josh@joshpeek.com>
2009-10-17 12:37:15 -05:00

37 lines
1.1 KiB
Ruby

module ActiveRecord
module Attributes
# Returns true if the given attribute is in the attributes hash
def has_attribute?(attr_name)
_attributes.key?(attr_name)
end
# Returns an array of names for the attributes available on this object sorted alphabetically.
def attribute_names
_attributes.keys.sort!
end
# Returns a hash of all the attributes with their names as keys and the values of the attributes as values.
def attributes
attributes = _attributes.dup
attributes.typecast! unless _attributes.frozen?
attributes.to_h
end
protected
# Not to be confused with the public #attributes method, which returns a typecasted Hash.
def _attributes
@attributes
end
def initialize_attribute_store(merge_attributes = nil)
@attributes = ActiveRecord::Attributes::Store.new
@attributes.merge!(merge_attributes) if merge_attributes
@attributes.types.merge!(self.class.attribute_types)
@attributes.aliases.merge!('id' => self.class.primary_key) unless 'id' == self.class.primary_key
@attributes
end
end
end