Module: Dynamoid::Persistence
Overview
Persistence is responsible for dumping objects to and marshalling objects from the datastore. It tries to reserialize values to be of the same type as when they were passed in, based on the fields in the class.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- UNIX_EPOCH_DATE =
Date.new(1970, 1, 1).freeze
Instance Attribute Summary collapse
-
#new_record ⇒ Object
(also: #new_record?)
Returns the value of attribute new_record.
Instance Method Summary collapse
-
#decrement(attribute, by = 1) ⇒ Object
Initializes attribute to zero if nil and subtracts the value passed as by (default is 1).
-
#decrement!(attribute, by = 1) ⇒ Object
Wrapper around decrement that saves the record.
-
#delete ⇒ Object
Delete this object from the datastore.
-
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
- #destroy! ⇒ Object
-
#increment(attribute, by = 1) ⇒ Object
Initializes attribute to zero if nil and adds the value passed as by (default is 1).
-
#increment!(attribute, by = 1) ⇒ Object
Wrapper around increment that saves the record.
-
#persisted? ⇒ Boolean
Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
-
#save(_options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
-
#touch(name = nil) ⇒ Object
Set updated_at and any passed in field to current DateTime.
- #update(conditions = {}, &block) ⇒ Object
-
#update!(conditions = {}) ⇒ Object
update!() will increment the lock_version if the table has the column, but will not check it.
-
#update_attribute(attribute, value) ⇒ Object
Update a single attribute, saving the object afterwards.
-
#update_attributes(attributes) ⇒ Object
Updates multiple attributes at once, saving the object once the updates are complete.
-
#update_attributes!(attributes) ⇒ Object
Updates multiple attributes at once, saving the object once the updates are complete.
Instance Attribute Details
#new_record ⇒ Object Also known as: new_record?
Returns the value of attribute new_record.
14 15 16 |
# File 'lib/dynamoid/persistence.rb', line 14 def new_record @new_record end |
Instance Method Details
#decrement(attribute, by = 1) ⇒ Object
Initializes attribute to zero if nil and subtracts the value passed as by (default is 1). Only makes sense for number-based attributes. Returns self.
235 236 237 238 239 |
# File 'lib/dynamoid/persistence.rb', line 235 def decrement(attribute, by = 1) self[attribute] ||= 0 self[attribute] -= by self end |
#decrement!(attribute, by = 1) ⇒ Object
Wrapper around decrement that saves the record. Returns true if the record could be saved.
243 244 245 246 |
# File 'lib/dynamoid/persistence.rb', line 243 def decrement!(attribute, by = 1) decrement(attribute, by) save end |
#delete ⇒ Object
Delete this object from the datastore.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/dynamoid/persistence.rb', line 265 def delete = range_key ? { range_key: Dumping.dump_field(read_attribute(range_key), self.class.attributes[range_key]) } : {} # Add an optimistic locking check if the lock_version column exists if self.class.attributes[:lock_version] conditions = { if: {} } conditions[:if][:lock_version] = if changes[:lock_version].nil? lock_version else changes[:lock_version][0] end [:conditions] = conditions end Dynamoid.adapter.delete(self.class.table_name, hash_key, ) rescue Dynamoid::Errors::ConditionalCheckFailedException raise Dynamoid::Errors::StaleObjectError.new(self, 'delete') end |
#destroy ⇒ Object
Delete this object, but only after running callbacks for it.
251 252 253 254 255 256 |
# File 'lib/dynamoid/persistence.rb', line 251 def destroy ret = run_callbacks(:destroy) do delete end ret == false ? false : self end |
#destroy! ⇒ Object
258 259 260 |
# File 'lib/dynamoid/persistence.rb', line 258 def destroy! destroy || (raise Dynamoid::Errors::RecordNotDestroyed, self) end |
#increment(attribute, by = 1) ⇒ Object
Initializes attribute to zero if nil and adds the value passed as by (default is 1). Only makes sense for number-based attributes. Returns self.
220 221 222 223 224 |
# File 'lib/dynamoid/persistence.rb', line 220 def increment(attribute, by = 1) self[attribute] ||= 0 self[attribute] += by self end |
#increment!(attribute, by = 1) ⇒ Object
Wrapper around increment that saves the record. Returns true if the record could be saved.
228 229 230 231 |
# File 'lib/dynamoid/persistence.rb', line 228 def increment!(attribute, by = 1) increment(attribute, by) save end |
#persisted? ⇒ Boolean
Is this object persisted in the datastore? Required for some ActiveModel integration stuff.
133 134 135 |
# File 'lib/dynamoid/persistence.rb', line 133 def persisted? !new_record? end |
#save(_options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/dynamoid/persistence.rb', line 140 def save( = {}) self.class.create_table if new_record? conditions = { unless_exists: [self.class.hash_key] } conditions[:unless_exists] << range_key if range_key run_callbacks(:create) { persist(conditions) } else persist end end |
#touch(name = nil) ⇒ Object
Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.
123 124 125 126 127 128 |
# File 'lib/dynamoid/persistence.rb', line 123 def touch(name = nil) now = DateTime.now self.updated_at = now attributes[name] = now if name save end |
#update(conditions = {}, &block) ⇒ Object
211 212 213 214 215 216 |
# File 'lib/dynamoid/persistence.rb', line 211 def update(conditions = {}, &block) update!(conditions, &block) true rescue Dynamoid::Errors::StaleObjectError false end |
#update!(conditions = {}) ⇒ Object
update!() will increment the lock_version if the table has the column, but will not check it. Thus, a concurrent save will never cause an update! to fail, but an update! may cause a concurrent save to fail.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/dynamoid/persistence.rb', line 188 def update!(conditions = {}) run_callbacks(:update) do = range_key ? { range_key: Dumping.dump_field(read_attribute(range_key), self.class.attributes[range_key]) } : {} begin new_attrs = Dynamoid.adapter.update_item(self.class.table_name, hash_key, .merge(conditions: conditions)) do |t| t.add(lock_version: 1) if self.class.attributes[:lock_version] if Dynamoid::Config. time_now = DateTime.now.in_time_zone(Time.zone) time_now_dumped = Dumping.dump_field(time_now, self.class.attributes[:updated_at]) t.set(updated_at: time_now_dumped) end yield t end load(Undumping.undump_attributes(new_attrs, self.class.attributes)) rescue Dynamoid::Errors::ConditionalCheckFailedException raise Dynamoid::Errors::StaleObjectError.new(self, 'update') end end end |
#update_attribute(attribute, value) ⇒ Object
Update a single attribute, saving the object afterwards.
178 179 180 181 |
# File 'lib/dynamoid/persistence.rb', line 178 def update_attribute(attribute, value) write_attribute(attribute, value) save end |
#update_attributes(attributes) ⇒ Object
Updates multiple attributes at once, saving the object once the updates are complete.
158 159 160 161 |
# File 'lib/dynamoid/persistence.rb', line 158 def update_attributes(attributes) attributes.each { |attribute, value| write_attribute(attribute, value) } save end |
#update_attributes!(attributes) ⇒ Object
Updates multiple attributes at once, saving the object once the updates are complete. Raises a Dynamoid::Errors::DocumentNotValid exception if there is vaidation and it fails.
167 168 169 170 |
# File 'lib/dynamoid/persistence.rb', line 167 def update_attributes!(attributes) attributes.each { |attribute, value| write_attribute(attribute, value) } save! end |