module Dynamoid::Fields
All fields on a Dynamoid::Document
must be explicitly defined – if you have fields in the database that are not specified with field, then they will be ignored.
Attributes
You can access the attributes of an object directly on its attributes method, which is by default an empty hash.
You can access the attributes of an object directly on its attributes method, which is by default an empty hash.
Public Instance Methods
Return attributes values before type casting.
user = User.new user.age = '21' user.age # => 21 user.attributes_before_type_cast # => { age: '21' }
@return [Hash] original attribute values
# File lib/dynamoid/fields.rb, line 335 def attributes_before_type_cast @attributes_before_type_cast end
Read an attribute from an object.
user.age = 20 user.read_attribute(:age) # => 20
@param name [Symbol] the name of the field @return attribute value @since 0.2.0
# File lib/dynamoid/fields.rb, line 321 def read_attribute(name) attributes[name.to_sym] end
Return the value of the attribute identified by name before type casting.
user = User.new user.age = '21' user.age # => 21 user.read_attribute_before_type_cast(:age) # => '21'
@param name [Symbol] attribute name @return original attribute value
# File lib/dynamoid/fields.rb, line 349 def read_attribute_before_type_cast(name) return nil unless name.respond_to?(:to_sym) @attributes_before_type_cast[name.to_sym] end
Write an attribute on the object.
user.age = 20 user.write_attribute(:age, 21) user.age # => 21
Also marks the previous value as dirty.
@param name [Symbol] the name of the field @param value [Object] the value to assign to that field @return [Dynamoid::Document] self
@since 0.2.0
# File lib/dynamoid/fields.rb, line 291 def write_attribute(name, value) name = name.to_sym old_value = read_attribute(name) unless attribute_is_present_on_model?(name) raise Dynamoid::Errors::UnknownAttribute, "Attribute #{name} is not part of the model" end if association = @associations[name] association.reset end @attributes_before_type_cast[name] = value value_casted = TypeCasting.cast_field(value, self.class.attributes[name]) attribute_will_change!(name) if old_value != value_casted # Dirty API attributes[name] = value_casted self end
Private Instance Methods
# File lib/dynamoid/fields.rb, line 381 def attribute_is_present_on_model?(attribute_name) setter = "#{attribute_name}=".to_sym respond_to?(setter) end
# File lib/dynamoid/fields.rb, line 357 def set_expires_field options = self.class.options[:expires] if options.present? name = options[:field] seconds = options[:after] if self[name].blank? send("#{name}=", Time.now.to_i + seconds) end end end
# File lib/dynamoid/fields.rb, line 370 def set_inheritance_field # actually it does only following logic: # self.type ||= self.class.sti_name if self.class.attributes[:type] return if self.class.abstract_class? type = self.class.inheritance_field if self.class.attributes[type] && send(type).nil? send("#{type}=", self.class.sti_name) end end