Module: DatastaxRails::Persistence

Extended by:
ActiveSupport::Concern
Included in:
Base
Defined in:
lib/datastax_rails/persistence.rb

Overview

Handles persisting data into Datastax

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#destroy(options = {}) ⇒ Object



150
151
152
153
154
# File 'lib/datastax_rails/persistence.rb', line 150

def destroy(options = {})
  self.class.remove(id_for_update, options)
  @destroyed = true
  freeze
end

#persisted?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/datastax_rails/persistence.rb', line 136

def persisted?
  !(new_record? || destroyed?)
end

#reload(options = nil) ⇒ Object

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.



228
229
230
231
232
233
234
235
236
# File 'lib/datastax_rails/persistence.rb', line 228

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 = {}) ⇒ Object



140
141
142
143
144
# File 'lib/datastax_rails/persistence.rb', line 140

def save(options = {})
  _create_or_update(options)
rescue DatastaxRails::RecordInvalid
  false
end

#save!(options = {}) ⇒ Object



146
147
148
# File 'lib/datastax_rails/persistence.rb', line 146

def save!(options = {})
  _create_or_update(options) || fail(RecordNotSaved)
end

#toggle(attribute) ⇒ Object

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.



211
212
213
214
# File 'lib/datastax_rails/persistence.rb', line 211

def toggle(attribute)
  self[attribute] = !send("#{attribute}?")
  self
end

#toggle!(attribute) ⇒ Object

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.



220
221
222
# File 'lib/datastax_rails/persistence.rb', line 220

def toggle!(attribute)
  toggle(attribute).update_attribute(attribute, self[attribute])
end

#update_attribute(name, value) ⇒ Object

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.



188
189
190
191
# File 'lib/datastax_rails/persistence.rb', line 188

def update_attribute(name, value)
  send("#{name}=", value)
  save(validate: false)
end

#update_attributes(attributes, _options = {}) ⇒ Object

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.



195
196
197
198
# File 'lib/datastax_rails/persistence.rb', line 195

def update_attributes(attributes, _options = {})
  assign_attributes(attributes)
  save
end

#update_attributes!(attributes, _options = {}) ⇒ Object

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



202
203
204
205
# File 'lib/datastax_rails/persistence.rb', line 202

def update_attributes!(attributes, _options = {})
  assign_attributes(attributes)
  save!
end