Module: Dynamoid::Fields

Extended by:
ActiveSupport::Concern
Included in:
Components
Defined in:
lib/dynamoid/fields.rb

Overview

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.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject Also known as: raw_attributes

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



45
46
47
# File 'lib/dynamoid/fields.rb', line 45

def attributes
  @attributes
end

Instance Method Details

#read_attribute(name) ⇒ Object Also known as: []

Read an attribute from an object.

Parameters:

  • name (Symbol)

    the name of the field

Since:

  • 0.2.0



72
73
74
# File 'lib/dynamoid/fields.rb', line 72

def read_attribute(name)
  attributes[name.to_sym]
end

#update_attribute(attribute, value) ⇒ Object

Update a single attribute, saving the object afterwards.

Parameters:

  • attribute (Symbol)

    the attribute to update

  • value (Object)

    the value to assign it

Since:

  • 0.2.0



109
110
111
112
# File 'lib/dynamoid/fields.rb', line 109

def update_attribute(attribute, value)
  write_attribute(attribute, value)
  save
end

#update_attributes(attributes) ⇒ Object

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

Parameters:

  • attributes (Hash)

    a hash of attributes to update

Since:

  • 0.2.0



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/dynamoid/fields.rb', line 82

def update_attributes(attributes)
  attributes.each {|attribute, value| self.write_attribute(attribute, value)}
  if self.new_record # if never saved save.
    save
  else # update attributes if we have saved.
    # next if self.read_only_attributes.include? attribute.to_s put this back in.
    run_callbacks(:save) do
      update! do |u|
        attributes.each do |attribute, value|
          u.set attribute => dump_field(
            self.read_attribute(attribute),
            self.class.attributes[attribute.to_sym]
          )
        end
      end
    end
    
    save
  end
end

#write_attribute(name, value) ⇒ Object Also known as: []=

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

Parameters:

  • name (Symbol)

    the name of the field

  • value (Object)

    the value to assign to that field

Since:

  • 0.2.0



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dynamoid/fields.rb', line 54

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