module DatastaxRails::Persistence

Handles persisting data into Datastax

Public Instance Methods

destroy(options = {}) click to toggle source
# File lib/datastax_rails/persistence.rb, line 189
def destroy(options = {})
  self.class.remove(id_for_destroy, options)
  @destroyed = true
  freeze
end
Also aliased as: destroy_without_callbacks
destroy_without_callbacks(options = {})
Alias for: destroy
persisted?() click to toggle source
# File lib/datastax_rails/persistence.rb, line 174
def persisted?
  !(new_record? || destroyed?)
end
reload(options = nil) click to toggle source

Reloads the attributes of this object from the database. The optional options argument is passed to find when reloading so you may do e.g. record.reload(:lock => true) to reload the same record with an exclusive row lock.

# File lib/datastax_rails/persistence.rb, line 268
def reload(options = nil)
  clear_association_cache

  fresh_object = self.class.unscoped { self.class.find(id, options) }
  @attributes.update(fresh_object.instance_variable_get('@attributes'))

  @attributes_cache = {}
  self
end
save(options = {}) click to toggle source

See {DatastaxRails::Persistence.write} for allowed options

# File lib/datastax_rails/persistence.rb, line 179
def save(options = {})
  _create_or_update(options)
rescue DatastaxRails::RecordInvalid
  false
end
save!(options = {}) click to toggle source
# File lib/datastax_rails/persistence.rb, line 185
def save!(options = {})
  _create_or_update(options) || fail(RecordNotSaved)
end
toggle(attribute) click to toggle source

Assigns to attribute the boolean opposite of attribute?. So if the predicate returns true the attribute will become false. This method toggles directly the underlying value without calling any setter. Returns self.

# File lib/datastax_rails/persistence.rb, line 251
def toggle(attribute)
  self[attribute] = !send("#{attribute}?")
  self
end
toggle!(attribute) click to toggle source

Wrapper around toggle that saves the record. This method differs from its non-bang version in that it passes through the attribute setter. Saving is not subjected to validation checks. Returns true if the record could be saved.

# File lib/datastax_rails/persistence.rb, line 260
def toggle!(attribute)
  toggle(attribute).update_attribute(attribute, self[attribute])
end
update_attribute(name, value) click to toggle source

Updates a single attribute and saves the record. This is especially useful for boolean flags on existing records. Also note that

  • Validation is skipped.

  • Callbacks are invoked.

  • updated_at/updated_on column is updated if that column is available.

  • Updates all the attributes that are dirty in this object.

# File lib/datastax_rails/persistence.rb, line 228
def update_attribute(name, value)
  send("#{name}=", value)
  save(validate: false)
end
update_attributes(attributes, _options = {}) click to toggle source

Updates the attributes of the model from the passed-in hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

# File lib/datastax_rails/persistence.rb, line 235
def update_attributes(attributes, _options = {})
  assign_attributes(attributes)
  save
end
update_attributes!(attributes, _options = {}) click to toggle source

Updates its receiver just like update_attributes but calls save! instead of save, so an exception is raised if the record is invalid.

# File lib/datastax_rails/persistence.rb, line 242
def update_attributes!(attributes, _options = {})
  assign_attributes(attributes)
  save!
end

Private Instance Methods

_create_or_update(options) click to toggle source
# File lib/datastax_rails/persistence.rb, line 280
def _create_or_update(options)
  result = new_record? ? _create_record(options) : _update_record(options)
  result != false
end
_create_record(options) click to toggle source
# File lib/datastax_rails/persistence.rb, line 285
def _create_record(options)
  # TODO: handle the non-UUID case
  self.id ||= Cassandra::TimeUuid::Generator.new.now
  _write(options)
  @new_record = false
  self.id
end
_update_record(options) click to toggle source
# File lib/datastax_rails/persistence.rb, line 293
def _update_record(options)
  _write(options)
end