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

attributes[RW]

You can access the attributes of an object directly on its attributes method, which is by default an empty hash.

raw_attributes[RW]

You can access the attributes of an object directly on its attributes method, which is by default an empty hash.

Public Instance Methods

[](name)
Alias for: read_attribute
[]=(name, value)
Alias for: write_attribute
read_attribute(name) click to toggle source

Read an attribute from an object.

@param [Symbol] name the name of the field

@since 0.2.0

# File lib/dynamoid/fields.rb, line 90
def read_attribute(name)
  attributes[name.to_sym]
end
Also aliased as: []
update_attribute(attribute, value) click to toggle source

Update a single attribute, saving the object afterwards.

@param [Symbol] attribute the attribute to update @param [Object] value the value to assign it

@since 0.2.0

# File lib/dynamoid/fields.rb, line 111
def update_attribute(attribute, value)
  write_attribute(attribute, value)
  save
end
update_attributes(attributes) click to toggle source

Updates multiple attibutes at once, saving the object once the updates are complete.

@param [Hash] attributes a hash of attributes to update

@since 0.2.0

# File lib/dynamoid/fields.rb, line 100
def update_attributes(attributes)
  attributes.each {|attribute, value| self.write_attribute(attribute, value)} unless attributes.nil? || attributes.empty?
  save
end
write_attribute(name, value) click to toggle source

Write an attribute on the object. Also marks the previous value as dirty.

@param [Symbol] name the name of the field @param [Object] value the value to assign to that field

@since 0.2.0

# File lib/dynamoid/fields.rb, line 72
def write_attribute(name, value)
  if (size = value.to_s.size) > MAX_ITEM_SIZE
    Dynamoid.logger.warn "DynamoDB can't store items larger than #{MAX_ITEM_SIZE} and the #{name} field has a length of #{size}."
  end

  if association = @associations[name]
    association.reset
  end

  attributes[name.to_sym] = value
end
Also aliased as: []=

Private Instance Methods

set_created_at() click to toggle source

Automatically called during the created callback to set the created_at time.

@since 0.2.0

# File lib/dynamoid/fields.rb, line 121
def set_created_at
  self.created_at = DateTime.now
end
set_type() click to toggle source
# File lib/dynamoid/fields.rb, line 132
def set_type
  self.type ||= self.class.to_s if self.class.attributes[:type]
end
set_updated_at() click to toggle source

Automatically called during the save callback to set the updated_at time.

@since 0.2.0

# File lib/dynamoid/fields.rb, line 128
def set_updated_at
  self.updated_at = DateTime.now
end