module GdatastoreMapper::Persistence

Public Instance Methods

delete() click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 33
def delete
  destroy
end
destroy() click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 25
def destroy
  run_callbacks :destroy do
    update_owner(self, :delete)
    GdatastoreMapper::Session.dataset.delete \
      Google::Cloud::Datastore::Key.new self.class.to_s, id
  end
end
persisted?() click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 37
def persisted?
  id.present?
end
save() click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 4
def save
  return false if !valid?
  run_callbacks :save do
    entity = to_entity
    GdatastoreMapper::Session.dataset.save(entity)
    self.id = entity.key.id
    update_owner(self, :add)
    true
  end
end
update(attributes) click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 15
def update attributes
  return false if !valid?
  run_callbacks :update do
    attributes.each do |name, value|
      send "#{name}=", value if respond_to? "#{name}="
    end
    save
  end
end

Private Instance Methods

owner_attr(belongings_id, existing_ids, flg) click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 53
def owner_attr belongings_id, existing_ids, flg
  owner_attr = {}
  if flg == :add
    existing_ids << self.id
  elsif flg == :delete
    existing_ids.delete(self.id)
  end
  owner_attr[belongings_id] = (existing_ids.uniq)
  owner_attr
end
update_owner(belonging, flg = :add) click to toggle source
# File lib/gdatastore_mapper/persistence.rb, line 43
def update_owner belonging, flg = :add
  return if self.class.belongs_to_models.nil?
  belongings_id = belonging.class.to_s.pluralize.underscore + '_id'
  self.class.belongs_to_models.each do |owner|
    return unless owner_record = belonging.send(owner)
    existing_ids = owner_record.send(belongings_id) || []
    owner_record.update(owner_attr(belongings_id, existing_ids, flg))
  end
end