Module: Cequel::Record::Persistence
- Extended by:
- ActiveSupport::Concern, Forwardable
- Includes:
- Instrumentation
- Defined in:
- lib/cequel/record/persistence.rb
Overview
This module provides functionality for loading and saving records to the Cassandra database.
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#destroy(options = {}) ⇒ Record
Remove this record from the database.
-
#exists? ⇒ Boolean
(also: #exist?)
Check if an unloaded record exists in the database.
- #hydrate(row) ⇒ Object
-
#key_attributes ⇒ Hash
The attributes of this record that make up the primary key.
-
#key_values ⇒ Array
(also: #to_key)
The values of the primary key columns for this record.
-
#load ⇒ Record
Load an unloaded record’s row from the database and hydrate the record’s attributes.
-
#load! ⇒ Record
Attempt to load an unloaded record and raise an error if the record does not correspond to a row in the database.
- #loaded?(column = nil) ⇒ Boolean
-
#new_record? ⇒ Boolean
True if this is a new, unsaved record.
-
#persisted? ⇒ Boolean
True if this record is persisted in the database.
-
#save(options = {}) ⇒ Boolean
Persist the record to the database.
-
#transient? ⇒ Boolean
True if this record is not persisted in the database.
-
#update_attributes(attributes) ⇒ Boolean
Set attributes and save the record.
Instance Method Details
#destroy(options = {}) ⇒ Record
Remove this record from the database
220 221 222 223 224 225 226 |
# File 'lib/cequel/record/persistence.rb', line 220 def destroy( = {}) .assert_valid_keys(:consistency, :timestamp) assert_keys_present! .delete() transient! self end |
#exists? ⇒ Boolean Also known as: exist?
Check if an unloaded record exists in the database
107 108 109 110 111 112 |
# File 'lib/cequel/record/persistence.rb', line 107 def exists? load! true rescue RecordNotFound false end |
#hydrate(row) ⇒ Object
257 258 259 260 261 |
# File 'lib/cequel/record/persistence.rb', line 257 def hydrate(row) init_attributes(row) hydrated! self end |
#key_attributes ⇒ Hash
Returns the attributes of this record that make up the primary key.
84 85 86 |
# File 'lib/cequel/record/persistence.rb', line 84 def key_attributes @attributes.slice(*self.class.key_column_names) end |
#key_values ⇒ Array Also known as: to_key
Returns the values of the primary key columns for this record.
94 95 96 |
# File 'lib/cequel/record/persistence.rb', line 94 def key_values key_attributes.values end |
#load ⇒ Record
Load an unloaded record’s row from the database and hydrate the record’s attributes
123 124 125 126 127 |
# File 'lib/cequel/record/persistence.rb', line 123 def load assert_keys_present! record_collection.load! unless loaded? self end |
#load! ⇒ Record
Attempt to load an unloaded record and raise an error if the record does not correspond to a row in the database
139 140 141 142 143 144 145 146 147 |
# File 'lib/cequel/record/persistence.rb', line 139 def load! load.tap do if transient? fail RecordNotFound, "Couldn't find #{self.class.name} with " \ "#{key_attributes.inspect}" end end end |
#loaded? ⇒ Boolean #loaded?(column) ⇒ Boolean
162 163 164 |
# File 'lib/cequel/record/persistence.rb', line 162 def loaded?(column = nil) !!@loaded && (column.nil? || @attributes.key?(column.to_sym)) end |
#new_record? ⇒ Boolean
Returns true if this is a new, unsaved record.
234 235 236 |
# File 'lib/cequel/record/persistence.rb', line 234 def new_record? !!@new_record end |
#persisted? ⇒ Boolean
Returns true if this record is persisted in the database.
243 244 245 |
# File 'lib/cequel/record/persistence.rb', line 243 def persisted? !!@persisted end |
#save(options = {}) ⇒ Boolean
Persist the record to the database. If this is a new record, it will be saved using an INSERT statement. If it is an existing record, it will be persisted using a series of ‘UPDATE` and `DELETE` statements which will persist all changes to the database, including atomic collection modifications.
186 187 188 189 190 191 192 193 |
# File 'lib/cequel/record/persistence.rb', line 186 def save( = {}) .assert_valid_keys(:consistency, :ttl, :timestamp) if new_record? then create() else update() end @new_record = false true end |
#transient? ⇒ Boolean
Returns true if this record is not persisted in the database.
252 253 254 |
# File 'lib/cequel/record/persistence.rb', line 252 def transient? !persisted? end |
#update_attributes(attributes) ⇒ Boolean
Set attributes and save the record
205 206 207 208 |
# File 'lib/cequel/record/persistence.rb', line 205 def update_attributes(attributes) self.attributes = attributes save end |