dynamini 1.7.2 → 1.7.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dynamini/base.rb +27 -32
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 381c96977138b58d80f888d2fc090d9b409269c3
4
- data.tar.gz: 612144e090649f6ca6788031d56a10d9148cc542
3
+ metadata.gz: 2c2c8f93d74ca1c4ef61073edd3858699e8db375
4
+ data.tar.gz: c1fa1dbebd43de5f85f4035f2cd360b1c26bb21b
5
5
  SHA512:
6
- metadata.gz: 2fbb94b77c41b5a740a56675e34475e5bcb89080c68d971c7b1218a354b8309635dc9e523f3663a851089f20e6b9df4523750f7e1d7291f5bdaa1ebe80544706
7
- data.tar.gz: 42a6ee7785bd4476de41eb8e5f44f853db4c7592be93e3ec38f1eba9b6eff42c6015d060b9f31aafb364f4bf1961aed9d1b61cad52264f8cca740e09a4682593
6
+ metadata.gz: cac32c4a8a7a9c1346e990733e66574c33602b8253a4f40f5c79eed2ed88bd3ec4f941b8acb895d76835d43b9f357829fed29c79bc06ad9ac79aaf54ebab8607
7
+ data.tar.gz: de7b259c5169f8874e515f124b902531b6cd2b34d3d037aec89954355cc81089bfd515ea3fc7edba7192903abfef431d1da81173b3d142e036021888620e1d0c
data/lib/dynamini/base.rb CHANGED
@@ -144,15 +144,23 @@ module Dynamini
144
144
  #### Instance Methods
145
145
 
146
146
  def initialize(attributes = {}, new_record = true)
147
- @changed = Set.new
148
147
  @new_record = new_record
149
148
  @attributes = {}
150
-
149
+ clear_changes
151
150
  attributes.each do |k, v|
152
151
  write_attribute(k, v, new_record)
153
152
  end
154
153
  end
155
154
 
155
+ def changes
156
+ @changes.delete_if { |attr, value| [self.class.hash_key, self.class.range_key].include?(attr) }
157
+ .stringify_keys
158
+ end
159
+
160
+ def changed
161
+ changes.keys.map(&:to_s)
162
+ end
163
+
156
164
  def ==(other)
157
165
  hash_key == other.hash_key if other.is_a?(self.class)
158
166
  end
@@ -175,13 +183,13 @@ module Dynamini
175
183
  end
176
184
 
177
185
  def save(options = {})
178
- @changed.empty? || (valid? && trigger_save(options))
186
+ @changes.empty? || (valid? && trigger_save(options))
179
187
  end
180
188
 
181
189
  def save!(options = {})
182
190
  options[:validate] = true if options[:validate].nil?
183
191
 
184
- unless @changed.empty?
192
+ unless @changes.empty?
185
193
  if (options[:validate] && valid?) || !options[:validate]
186
194
  trigger_save(options)
187
195
  else
@@ -199,11 +207,11 @@ module Dynamini
199
207
  end
200
208
  end
201
209
 
202
- def increment!(attribute_increments, opts = {})
203
- attribute_increments.each do |a, v|
204
- validate_incrementable_attribute(a, v)
210
+ def increment!(attributes, opts = {})
211
+ attributes.each do |attr, value|
212
+ validate_incrementable_attribute(attr, value)
205
213
  end
206
- increment_to_dynamo(attribute_increments, opts)
214
+ increment_to_dynamo(attributes, opts)
207
215
  end
208
216
 
209
217
  def delete
@@ -211,16 +219,6 @@ module Dynamini
211
219
  self
212
220
  end
213
221
 
214
- def changes
215
- @attributes.select { |attribute| @changed.include?(attribute.to_s) &&
216
- attribute != self.class.hash_key &&
217
- attribute != self.class.range_key
218
- }
219
- end
220
-
221
- def changed
222
- @changed.to_a
223
- end
224
222
 
225
223
  def new_record?
226
224
  @new_record
@@ -228,10 +226,6 @@ module Dynamini
228
226
 
229
227
  private
230
228
 
231
- def add_changed(attributes)
232
- @changed += attributes.keys.map(&:to_s)
233
- end
234
-
235
229
  def trigger_save(options = {})
236
230
  generate_timestamps! unless options[:skip_timestamps]
237
231
  save_to_dynamo
@@ -276,11 +270,11 @@ module Dynamini
276
270
  self.class.client.delete_item(table_name: self.class.table_name, key: key)
277
271
  end
278
272
 
279
- def increment_to_dynamo(attribute_increments, opts = {})
273
+ def increment_to_dynamo(attributes, opts = {})
280
274
  self.class.client.update_item(
281
275
  table_name: self.class.table_name,
282
276
  key: key,
283
- attribute_updates: increment_updates(attribute_increments, opts)
277
+ attribute_updates: increment_updates(attributes, opts)
284
278
  )
285
279
  end
286
280
 
@@ -319,19 +313,20 @@ module Dynamini
319
313
 
320
314
  def attribute_updates
321
315
  changes.reduce({}) do |updates, (key, value)|
322
- updates[key] = {value: value, action: 'PUT'} unless value.blank?
316
+ current_value = value[1]
317
+ updates[key] = {value: current_value, action: 'PUT'} unless current_value.blank?
323
318
  updates
324
319
  end
325
320
  end
326
321
 
327
- def increment_updates(attribute_increments, opts = {})
322
+ def increment_updates(attributes, opts = {})
328
323
  updates = {}
329
- attribute_increments.each do |k,v|
330
- updates[k] = { value: v, action: 'ADD' }
324
+ attributes.each do |attr,value|
325
+ updates[attr] = { value: value, action: 'ADD' }
331
326
  end
332
327
  updates[:updated_at] = { value: Time.now.to_f, action: 'PUT' } unless opts[:skip_timestamps]
333
328
  updates[:created_at] = { value: Time.now.to_f, action: 'PUT' } unless attributes[:created_at]
334
- updates
329
+ updates.stringify_keys
335
330
  end
336
331
 
337
332
  def validate_incrementable_attribute(attribute, value)
@@ -348,7 +343,7 @@ module Dynamini
348
343
  end
349
344
 
350
345
  def clear_changes
351
- @changed = Set.new
346
+ @changes = Hash.new { |hash, key| hash[key] = Array.new(2) }
352
347
  end
353
348
 
354
349
  def method_missing(name, *args, &block)
@@ -407,12 +402,12 @@ module Dynamini
407
402
 
408
403
  def write_attribute(attribute, new_value, record_change = true)
409
404
  old_value = @attributes[attribute]
410
- @attributes[attribute] = (new_value.nil? ? nil : new_value)
405
+ @attributes[attribute] = new_value
411
406
  record_change(attribute, new_value, old_value) if record_change
412
407
  end
413
408
 
414
409
  def record_change(attribute, new_value, old_value)
415
- @changed << attribute.to_s if new_value != old_value
410
+ @changes[attribute] = [old_value, new_value]
416
411
  end
417
412
 
418
413
  def read_attribute(name)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamini
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.2
4
+ version: 1.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward
@@ -154,7 +154,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
154
  version: '0'
155
155
  requirements: []
156
156
  rubyforge_project:
157
- rubygems_version: 2.2.2
157
+ rubygems_version: 2.4.8
158
158
  signing_key:
159
159
  specification_version: 4
160
160
  summary: DynamoDB interface