module DataSteroid::Persistable::Savable

Defines behaviour for save operations.

Public Instance Methods

before_save(method) click to toggle source
# File lib/data_steroid/persistable/savable.rb, line 45
def before_save(method)
  before_save_methods << method
end
gcloud_entity() click to toggle source
# File lib/data_steroid/persistable/savable.rb, line 39
def gcloud_entity
  @gcloud_entity ||= self.class.datastore_entity
end
save() click to toggle source
# File lib/data_steroid/persistable/savable.rb, line 12
def save
  return false if invalid?

  before_save_methods.each do |name|
    send(name)
  end

  to_gcloud.each_pair do |key, value|
    if key == 'id' && (value.present? || parent.present?)
      gcloud_entity.key = gcloud_key unless gcloud_entity.persisted?
    elsif value.present?
      gcloud_entity[key] = value
    elsif gcloud_entity.properties.exist? key
      gcloud_entity.properties.delete key
    end
  end

  if (result = self.class.datastore.save(gcloud_entity).first)
    send('id=', result.key.id || result.key.name) if id.nil?
    true
  else
    false
  end
end