dynamini 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 109ce4b83dcb67fe1efb7c9396fbfb2b73edf943
4
- data.tar.gz: a11d91c67bde8d3e8840290321b881d8876e93c4
3
+ metadata.gz: 1fdfa96c8bf677260145f5d6bb35de6cf667cf90
4
+ data.tar.gz: 9044d8ce4cf6dfbaab3daa7003e0c5eab9862d43
5
5
  SHA512:
6
- metadata.gz: 26e9af4f62032585c3ec8ab3712f079a3a2256312e7ee307a4e33f2152075e25f48dd11cfc5fd981e6ca1ec5f2466bee162faffa99dfd34e6f683ed772b02ae8
7
- data.tar.gz: afa9bfc847326989792d6be8d1d7d64e4ca773b0d52d55187cf18419e5eaf79b983618e19576b0f873f3ee83fa409ecf0dbdadad5ed7923aa53dd821fd4e6baa
6
+ metadata.gz: c73f11b146c731254765292c25a777d77efef22f50298db7e203480d1ac01e6cd58c12a585f777dfdea06fc2526fb21cad65d46198ebada01b8f480709c8f15e
7
+ data.tar.gz: 6c586d97d5bafa884c5011020cfcd716ce3e8023ab807a51249a44d5cd3d605459f8b1350eac758b0bfdd7671bc50d1bfcfb42eaa7330b8e39fb7bf32e23e968
@@ -1,3 +1,4 @@
1
+ ### Central module for Dynamini.
1
2
  module Dynamini
2
3
  require 'active_model'
3
4
  require 'dynamini/base'
@@ -15,4 +16,4 @@ module Dynamini
15
16
  def self.configure
16
17
  yield(configuration)
17
18
  end
18
- end
19
+ end
@@ -104,8 +104,10 @@ module Dynamini
104
104
  def batch_find(ids = [])
105
105
  return [] if ids.length < 1
106
106
  objects = []
107
- raise StandardError, 'Batch find is limited to 100 items' if ids.length > 100
108
- key_structure = ids.map { |i| {hash_key => i.to_s} }
107
+ if ids.length > 100
108
+ raise StandardError, 'Batch find is limited to 100 items'
109
+ end
110
+ key_structure = ids.map { |i| { hash_key => i.to_s } }
109
111
  response = self.dynamo_batch_get(key_structure)
110
112
  response.responses[table_name].each do |item|
111
113
  objects << self.new(item.symbolize_keys, false)
@@ -192,17 +194,11 @@ module Dynamini
192
194
  end
193
195
  end
194
196
 
195
- def increment!(attribute, amount=1, opts = {})
196
- if amount.is_a?(Integer) || amount.is_a?(Float)
197
- current_value = self.send(attribute)
198
- if current_value.nil? || current_value.is_a?(Integer) || current_value.is_a?(Float)
199
- increment_to_dynamo(attribute, amount, opts)
200
- else
201
- raise StandardError, 'You cannot increment! a non-numeric non-nil value. If your current value is a numeric string, use :handle to autocast it as a number.'
202
- end
203
- else
204
- raise StandardError, 'You cannot increment an attribute by a non-numeric value.'
197
+ def increment!(attribute_increments, opts = {})
198
+ attribute_increments.each do |a, v|
199
+ validate_incrementable_attribute(a, v)
205
200
  end
201
+ increment_to_dynamo(attribute_increments, opts)
206
202
  end
207
203
 
208
204
  def delete
@@ -252,19 +248,15 @@ module Dynamini
252
248
  end
253
249
 
254
250
  def touch_to_dynamo
255
- self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: attributes[:updated_at], action: 'PUT'}})
251
+ self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: Time.now.to_f, action: 'PUT'}})
256
252
  end
257
253
 
258
254
  def delete_from_dynamo
259
255
  self.class.client.delete_item(table_name: self.class.table_name, key: key)
260
256
  end
261
257
 
262
- def increment_to_dynamo(attribute, amount, opts={})
263
- if opts[:skip_timestamps]
264
- self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {attribute => {value: amount, action: 'ADD'}})
265
- else
266
- self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {attribute => {value: amount, action: 'ADD'}, updated_at: {value: attributes[:updated_at], action: 'PUT'}})
267
- end
258
+ def increment_to_dynamo(attribute_increments, opts={})
259
+ self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: increment_updates(attribute_increments, opts))
268
260
  end
269
261
 
270
262
  def self.dynamo_batch_get(key_structure)
@@ -274,10 +266,10 @@ module Dynamini
274
266
  def self.dynamo_batch_save(model_array)
275
267
  put_requests = []
276
268
  model_array.each do |model|
277
- put_requests << {put_request: {item: model.attributes.reject{|k, v| v.blank?}.stringify_keys}}
269
+ put_requests << { put_request: { item: model.attributes.reject{|_k, v| v.blank? }.stringify_keys } }
278
270
  end
279
- request_options = {request_items: {
280
- "#{table_name}" => put_requests}
271
+ request_options = { request_items: {
272
+ "#{table_name}" => put_requests }
281
273
  }
282
274
  client.batch_write_item(request_options)
283
275
  end
@@ -301,6 +293,27 @@ module Dynamini
301
293
  end
302
294
  end
303
295
 
296
+ def increment_updates(attribute_increments, opts={})
297
+ updates = {}
298
+ attribute_increments.each do |k,v|
299
+ updates[k] = {value: v, action: 'ADD'}
300
+ end
301
+ updates[:updated_at] = {value: Time.now.to_f, action: 'PUT'} unless opts[:skip_timestamps]
302
+ updates[:created_at] = {value: Time.now.to_f, action: 'PUT'} unless attributes[:created_at]
303
+ updates
304
+ end
305
+
306
+ def validate_incrementable_attribute(attribute, value)
307
+ if value.is_a?(Integer) || value.is_a?(Float)
308
+ current_value = self.send(attribute)
309
+ unless current_value.nil? || current_value.is_a?(Integer) || current_value.is_a?(Float)
310
+ raise StandardError, "You cannot increment a non-numeric non-nil value: #{attribute} is currently #{current_value}. If your current value is a numeric string, use :handle to autocast it as a number."
311
+ end
312
+ else
313
+ raise StandardError, "You cannot increment an attribute by a non-numeric value: #{value}"
314
+ end
315
+ end
316
+
304
317
  def clear_changes
305
318
  @changed = Set.new
306
319
  end
@@ -68,7 +68,7 @@ module Dynamini
68
68
  attribute_hash = {}
69
69
 
70
70
  args[:attribute_updates].each do |k, v|
71
- if v[:action] == 'ADD'
71
+ if v[:action] == 'ADD' && @data[args[:table_name]][args[:key][hash_key]] #if record has been saved
72
72
  attribute_hash[k] = v[:value] + @data[args[:table_name]][args[:key][hash_key]][k].to_f
73
73
  else
74
74
  attribute_hash[k] = v[:value]
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.5.1
4
+ version: 1.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward
@@ -93,8 +93,9 @@ dependencies:
93
93
  - - ~>
94
94
  - !ruby/object:Gem::Version
95
95
  version: '2'
96
- description: Lightweight DynamoDB interface gem designed as a drop-in replacement
97
- for ActiveRecord. Built & maintained by the team at yroo.com.
96
+ description: |-
97
+ Lightweight DynamoDB interface gem designed as a drop-in replacement for ActiveRecord.
98
+ Built & maintained by the team at yroo.com.
98
99
  email: dev@retailcommon.com
99
100
  executables: []
100
101
  extensions: []