Sha256: 8dce40b6182e86a4b5203b6b2c07700746e3f4375ae96f9e48256e9f16841d0d
Contents?: true
Size: 1.84 KB
Versions: 5
Compression:
Stored size: 1.84 KB
Contents
# TODO: optimistic locking? module DynaModel module Persistence extend ActiveSupport::Concern private def populate_id #@_id = UUIDTools::UUID.random_create.to_s.downcase end private def dynamo_db_table self.class.dynamo_db_table(shard) end private def create_storage run_callbacks :save do run_callbacks :create do self.class.dynamo_db_table.write(serialize_attributes) end end end private def update_storage # Only enumerating dirty (i.e. changed) attributes. Empty # (nil and empty set) values are deleted, the others are replaced. run_callbacks :save do run_callbacks :update do attr_updates = {} changed.each do |attr_name| attribute = self.class.attribute_for(attr_name) value = serialize_attribute(attribute, @_data[attr_name]) if value.nil? or value == [] attr_updates[attr_name] = nil else attr_updates[attr_name] = value end end self.class.dynamo_db_table.write(attr_updates, { update_item: dynamo_db_item_key_values, shard_name: self.shard }) end end end private def delete_storage run_callbacks :destroy do self.class.dynamo_db_table.delete_item( delete_item: dynamo_db_item_key_values, shard_name: self.shard ) end end private def deserialize_item_data data data.inject({}) do |hash,(attr_name,value)| if attribute = self.class.attributes[attr_name] hash[attr_name] = value.is_a?(Set) ? value.map{|v| attribute.deserialize(v) } : attribute.deserialize(value) end hash end end module ClassMethods end end end
Version data entries
5 entries across 5 versions & 1 rubygems