Module: Dynamoid::Persistence

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

Overview

Persistence is responsible for dumping objects to and marshalling objects from the datastore. It tries to reserialize values to be of the same type as when they were passed in, based on the fields in the class.

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#new_recordObject Also known as: new_record?

Returns the value of attribute new_record.



11
12
13
# File 'lib/dynamoid/persistence.rb', line 11

def new_record
  @new_record
end

Instance Method Details

#deleteObject

Delete this object from the datastore and all indexes.

Since:

  • 0.2.0



185
186
187
188
189
# File 'lib/dynamoid/persistence.rb', line 185

def delete
  delete_indexes
  options = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
  Dynamoid::Adapter.delete(self.class.table_name, self.hash_key, options)
end

#destroyObject

Delete this object, but only after running callbacks for it.

Since:

  • 0.2.0



175
176
177
178
179
180
# File 'lib/dynamoid/persistence.rb', line 175

def destroy
  run_callbacks(:destroy) do
    self.delete
  end
  self
end

#dumpObject

Dump this object’s attributes into hash form, fit to be persisted into the datastore.

Since:

  • 0.2.0



194
195
196
197
198
199
200
# File 'lib/dynamoid/persistence.rb', line 194

def dump
  Hash.new.tap do |hash|
    self.class.attributes.each do |attribute, options|
      hash[attribute] = dump_field(self.read_attribute(attribute), options)
    end
  end
end

#persisted?Boolean

Is this object persisted in the datastore? Required for some ActiveModel integration stuff.

Returns:

  • (Boolean)

Since:

  • 0.2.0



140
141
142
# File 'lib/dynamoid/persistence.rb', line 140

def persisted?
  !new_record?
end

#save(options = {}) ⇒ Object

Run the callbacks and then persist this object in the datastore.

Since:

  • 0.2.0



147
148
149
150
151
152
153
154
155
156
157
# File 'lib/dynamoid/persistence.rb', line 147

def save(options = {})
  self.class.create_table

  if new_record?
    run_callbacks(:create) { persist }
  else
    persist
  end

  self
end

#touch(name = nil) ⇒ Object

Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.



130
131
132
133
134
135
# File 'lib/dynamoid/persistence.rb', line 130

def touch(name = nil)
  now = DateTime.now
  self.updated_at = now
  attributes[name] = now if name
  save
end

#update(conditions = {}, &block) ⇒ Object



165
166
167
168
169
170
# File 'lib/dynamoid/persistence.rb', line 165

def update(conditions = {}, &block)
  update!(conditions, &block)
  true
rescue Dynamoid::Errors::ConditionalCheckFailedException
  false
end

#update!(conditions = {}, &block) ⇒ Object



159
160
161
162
163
# File 'lib/dynamoid/persistence.rb', line 159

def update!(conditions = {}, &block)
  options = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
  new_attrs = Dynamoid::Adapter.update_item(self.class.table_name, self.hash_key, options.merge(:conditions => conditions), &block)
  load(new_attrs)
end