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



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

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

#persisted?Boolean

Returns:

  • (Boolean)


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

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.



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

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



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

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

#save!(options = {}) ⇒ Object



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

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.



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

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.



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

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.



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

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.



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

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.



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

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