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 attibutes at once, saving the object once the updates are complete.
-
#update_attributes!(attributes) ⇒ Object
Updates multiple attibutes 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.
236 237 238 239 240 |
# File 'lib/dynamoid/persistence.rb', line 236 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.
244 245 246 247 |
# File 'lib/dynamoid/persistence.rb', line 244 def decrement!(attribute, by = 1) decrement(attribute, by) save end |
#delete ⇒ Object
Delete this object from the datastore.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/dynamoid/persistence.rb', line 266 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.
252 253 254 255 256 257 |
# File 'lib/dynamoid/persistence.rb', line 252 def destroy ret = run_callbacks(:destroy) do delete end ret == false ? false : self end |
#destroy! ⇒ Object
259 260 261 |
# File 'lib/dynamoid/persistence.rb', line 259 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.
221 222 223 224 225 |
# File 'lib/dynamoid/persistence.rb', line 221 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.
229 230 231 232 |
# File 'lib/dynamoid/persistence.rb', line 229 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.
134 135 136 |
# File 'lib/dynamoid/persistence.rb', line 134 def persisted? !new_record? end |
#save(_options = {}) ⇒ Object
Run the callbacks and then persist this object in the datastore.
141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/dynamoid/persistence.rb', line 141 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.
124 125 126 127 128 129 |
# File 'lib/dynamoid/persistence.rb', line 124 def touch(name = nil) now = DateTime.now self.updated_at = now attributes[name] = now if name save end |
#update(conditions = {}, &block) ⇒ Object
212 213 214 215 216 217 |
# File 'lib/dynamoid/persistence.rb', line 212 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.
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/dynamoid/persistence.rb', line 189 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.
179 180 181 182 |
# File 'lib/dynamoid/persistence.rb', line 179 def update_attribute(attribute, value) write_attribute(attribute, value) save end |
#update_attributes(attributes) ⇒ Object
Updates multiple attibutes at once, saving the object once the updates are complete.
159 160 161 162 |
# File 'lib/dynamoid/persistence.rb', line 159 def update_attributes(attributes) attributes.each { |attribute, value| write_attribute(attribute, value) } unless attributes.nil? || attributes.empty? save end |
#update_attributes!(attributes) ⇒ Object
Updates multiple attibutes at once, saving the object once the updates are complete. Raises a Dynamoid::Errors::DocumentNotValid exception if there is vaidation and it fails.
168 169 170 171 |
# File 'lib/dynamoid/persistence.rb', line 168 def update_attributes!(attributes) attributes.each { |attribute, value| write_attribute(attribute, value) } unless attributes.nil? || attributes.empty? save! end |