dynamini 1.7.4 → 1.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c2c8f93d74ca1c4ef61073edd3858699e8db375
4
- data.tar.gz: c1fa1dbebd43de5f85f4035f2cd360b1c26bb21b
3
+ metadata.gz: 9d4b13372c015a7e191e88361d9b9ea0568ac70d
4
+ data.tar.gz: 83afd9ecb74c668480cb0665fdd4d13686054625
5
5
  SHA512:
6
- metadata.gz: cac32c4a8a7a9c1346e990733e66574c33602b8253a4f40f5c79eed2ed88bd3ec4f941b8acb895d76835d43b9f357829fed29c79bc06ad9ac79aaf54ebab8607
7
- data.tar.gz: de7b259c5169f8874e515f124b902531b6cd2b34d3d037aec89954355cc81089bfd515ea3fc7edba7192903abfef431d1da81173b3d142e036021888620e1d0c
6
+ metadata.gz: 8d3a994dffe9fda84d9c51487198f32b92ba3910d00bfa3c094ec88573cd2210a365599286ff946104ea34471f1e271cfa3d9cf32f3fea4e3001d2052b8695a8
7
+ data.tar.gz: 6bb3d9a6e5612727ce9226a96f38c7f97b873da29c0059ea296a5168372fdba5af816466b40cb6528297c1d9418daa945f46ef50f7eb700e0f9e573e690ea7e9
data/lib/dynamini/base.rb CHANGED
@@ -2,9 +2,18 @@ module Dynamini
2
2
  # Core db interface class.
3
3
  class Base
4
4
  include ActiveModel::Validations
5
+
5
6
  attr_reader :attributes
6
7
 
8
+ class_attribute :handles
9
+
10
+ self.handles = {
11
+ created_at: { format: :time, options: {} },
12
+ updated_at: { format: :time, options: {} }
13
+ }
14
+
7
15
  BATCH_SIZE = 25
16
+
8
17
  GETTER_PROCS = {
9
18
  integer: proc { |v| v.to_i },
10
19
  date: proc { |v| v.is_a?(Date) ? v : Time.at(v).to_date },
@@ -17,7 +26,7 @@ module Dynamini
17
26
 
18
27
  SETTER_PROCS = {
19
28
  integer: proc { |v| v.to_i },
20
- time: proc { |v| v.to_f },
29
+ time: proc { |v| (v.is_a?(Date) ? v.to_time : v).to_f },
21
30
  float: proc { |v| v.to_f },
22
31
  symbol: proc { |v| v.to_s },
23
32
  string: proc { |v| v },
@@ -46,6 +55,8 @@ module Dynamini
46
55
  end
47
56
 
48
57
  def handle(column, format_class, options = {})
58
+ self.handles = self.handles.merge(column => { format: format_class, options: options })
59
+
49
60
  define_handled_getter(column, format_class, options)
50
61
  define_handled_setter(column, format_class)
51
62
  end
@@ -152,8 +163,12 @@ module Dynamini
152
163
  end
153
164
  end
154
165
 
166
+ def keys
167
+ [self.class.hash_key, self.class.range_key]
168
+ end
169
+
155
170
  def changes
156
- @changes.delete_if { |attr, value| [self.class.hash_key, self.class.range_key].include?(attr) }
171
+ @changes.delete_if { |attr, value| keys.include?(attr) }
157
172
  .stringify_keys
158
173
  end
159
174
 
@@ -325,7 +340,7 @@ module Dynamini
325
340
  updates[attr] = { value: value, action: 'ADD' }
326
341
  end
327
342
  updates[:updated_at] = { value: Time.now.to_f, action: 'PUT' } unless opts[:skip_timestamps]
328
- updates[:created_at] = { value: Time.now.to_f, action: 'PUT' } unless attributes[:created_at]
343
+ updates[:created_at] = { value: Time.now.to_f, action: 'PUT' } unless @attributes[:created_at]
329
344
  updates.stringify_keys
330
345
  end
331
346
 
@@ -369,30 +384,18 @@ module Dynamini
369
384
  def self.define_handled_getter(column, format_class, options = {})
370
385
  proc = GETTER_PROCS[format_class]
371
386
  fail 'Unsupported data type: ' + format_class.to_s if proc.nil?
387
+
372
388
  define_method(column) do
373
- if @attributes.key?(column)
374
- v = read_attribute(column)
375
- if v.is_a? Array
376
- v.map{ |e| proc.call(e) }
377
- else
378
- proc.call(read_attribute(column))
379
- end
380
- else
381
- options[:default] || nil
382
- end
389
+ read_attribute(column)
383
390
  end
384
391
  end
385
392
 
386
393
  def self.define_handled_setter(column, format_class)
387
- setter_symbol = (column.to_s + '=').to_sym
394
+ method_name = (column.to_s + '=')
388
395
  proc = SETTER_PROCS[format_class]
389
396
  fail 'Unsupported data type: ' + format_class.to_s if proc.nil?
390
- define_method(setter_symbol) do |value|
391
- if value.is_a? Array
392
- write_attribute(column, value.map{ |e| proc.call(e) })
393
- else
394
- write_attribute(column, proc.call(value))
395
- end
397
+ define_method(method_name) do |value|
398
+ write_attribute(column, value)
396
399
  end
397
400
  end
398
401
 
@@ -401,7 +404,10 @@ module Dynamini
401
404
  end
402
405
 
403
406
  def write_attribute(attribute, new_value, record_change = true)
404
- old_value = @attributes[attribute]
407
+ old_value = read_attribute(attribute)
408
+ if (handle = handles[attribute.to_sym]) && !new_value.nil?
409
+ new_value = attribute_callback(SETTER_PROCS, handle, new_value)
410
+ end
405
411
  @attributes[attribute] = new_value
406
412
  record_change(attribute, new_value, old_value) if record_change
407
413
  end
@@ -411,12 +417,22 @@ module Dynamini
411
417
  end
412
418
 
413
419
  def read_attribute(name)
414
- @attributes[name]
420
+ value = @attributes[name]
421
+ if (handle = handles[name.to_sym])
422
+ value = handle[:options][:default] if value.nil?
423
+ value = attribute_callback(GETTER_PROCS, handle, value) unless value.nil?
424
+ end
425
+ value
426
+ end
427
+
428
+ def attribute_callback(procs, handle, value)
429
+ callback = procs[handle[:format]]
430
+ value.is_a?(Array) ? value.map { |e| callback.call(e) } : callback.call(value)
415
431
  end
416
432
 
417
- #### Default class macros
433
+ def handles
434
+ self.class.handles
435
+ end
418
436
 
419
- handle :updated_at, :time
420
- handle :created_at, :time
421
437
  end
422
438
  end
@@ -104,12 +104,17 @@ module Dynamini
104
104
  def flatten_attribute_updates(args = {})
105
105
  attribute_hash = {}
106
106
 
107
+ hash_key_value = args[:key][hash_key]
108
+ range_key_value = args[:key][range_key]
109
+
107
110
  if args[:attribute_updates]
108
111
  args[:attribute_updates].each do |k, v|
109
112
 
110
- if v[:action] == 'ADD' && @data[args[:table_name]][args[:key][hash_key]]
113
+ if v[:action] == 'ADD' && @data[args[:table_name]][hash_key_value]
111
114
  # if record has been saved
112
- attribute_hash[k] = (v[:value] + @data[args[:table_name]][args[:key][hash_key]][k].to_f)
115
+ data = @data[args[:table_name]][hash_key_value]
116
+ data = data[range_key_value] if range_key_value
117
+ attribute_hash[k] = (v[:value] + data[k].to_f)
113
118
  else
114
119
  attribute_hash[k] = v[:value]
115
120
  end
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.4
4
+ version: 1.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward