Module: DatastaxRails::Persistence
Overview
Handles persisting data into Datastax
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #destroy(options = {}) ⇒ Object (also: #destroy_without_callbacks)
- #persisted? ⇒ Boolean
-
#reload(options = nil) ⇒ Object
Reloads the attributes of this object from the database.
- #save(options = {}) ⇒ Object
- #save!(options = {}) ⇒ Object
-
#toggle(attribute) ⇒ Object
Assigns to
attribute
the boolean opposite ofattribute?
. -
#toggle!(attribute) ⇒ Object
Wrapper around
toggle
that saves the record. -
#update_attribute(name, value) ⇒ Object
Updates a single attribute and saves the record.
-
#update_attributes(attributes, _options = {}) ⇒ Object
Updates the attributes of the model from the passed-in hash and saves the record.
-
#update_attributes!(attributes, _options = {}) ⇒ Object
Updates its receiver just like
update_attributes
but callssave!
instead ofsave
, so an exception is raised if the record is invalid.
Instance Method Details
#destroy(options = {}) ⇒ Object Also known as: destroy_without_callbacks
180 181 182 183 184 |
# File 'lib/datastax_rails/persistence.rb', line 180 def destroy( = {}) self.class.remove(id_for_update, ) @destroyed = true freeze end |
#persisted? ⇒ Boolean
166 167 168 |
# File 'lib/datastax_rails/persistence.rb', line 166 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.
259 260 261 262 263 264 265 266 267 |
# File 'lib/datastax_rails/persistence.rb', line 259 def reload( = nil) clear_association_cache fresh_object = self.class.unscoped { self.class.find(id, ) } @attributes.update(fresh_object.instance_variable_get('@attributes')) @attributes_cache = {} self end |
#save(options = {}) ⇒ Object
170 171 172 173 174 |
# File 'lib/datastax_rails/persistence.rb', line 170 def save( = {}) _create_or_update() rescue DatastaxRails::RecordInvalid false end |
#save!(options = {}) ⇒ Object
176 177 178 |
# File 'lib/datastax_rails/persistence.rb', line 176 def save!( = {}) _create_or_update() || 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
.
242 243 244 245 |
# File 'lib/datastax_rails/persistence.rb', line 242 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.
251 252 253 |
# File 'lib/datastax_rails/persistence.rb', line 251 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.
219 220 221 222 |
# File 'lib/datastax_rails/persistence.rb', line 219 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.
226 227 228 229 |
# File 'lib/datastax_rails/persistence.rb', line 226 def update_attributes(attributes, = {}) 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.
233 234 235 236 |
# File 'lib/datastax_rails/persistence.rb', line 233 def update_attributes!(attributes, = {}) assign_attributes(attributes) save! end |