dynamini 1.3.0 → 1.4.0

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 +67 -15
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f3c9ae6d2dbeb10206578039daa3d7574c94a853
4
- data.tar.gz: 0ef0e594c363b62c6bae8c98f31be5f362655ae1
3
+ metadata.gz: c17460deb3b08dafb342ebad70b0a8e44c27c010
4
+ data.tar.gz: 758474e9d031f108efd69f00d6895bcb7af70149
5
5
  SHA512:
6
- metadata.gz: f2a9ca05976a847968a68a306f00bdb11d7e7bf87910ff7d99c6e2a43f13b555abb5b522910fd775c43f782606d3d6b636264d7844fc2d98e81f7976c7a13389
7
- data.tar.gz: 210e3587ff76f28e94b58c637a301c5b67ce47fc9109917e25191b28688068c5fab03da8ffb05c407dd2dbc47b4ddbb5b1e76d75f7c35d865afa3b5a1f191e10
6
+ metadata.gz: d21d68d87d2c27a74cf8bded50632fdb772351805d3964cdde00354c5a44034127ca28c2f2a3e7e706ca6563c3593b897c0287430342e0364da83f7d1ae67d26
7
+ data.tar.gz: 69bcf0301927025a4a4623a530360f856a493a7e8bd55b6a170e0bf08bf92d935d0a51b45101e731b26813a07c75e0a7b8620758e2f6403f2249416914b9b4f9
@@ -6,12 +6,25 @@ module Dynamini
6
6
  BATCH_SIZE = 25
7
7
 
8
8
  class << self
9
- attr_writer :hash_key, :table_name, :batch_write_queue, :in_memory
9
+ attr_writer :batch_write_queue, :in_memory
10
10
 
11
11
  def table_name
12
12
  @table_name || name.demodulize.downcase.pluralize
13
13
  end
14
14
 
15
+ def set_table_name(name)
16
+ @table_name = name
17
+ end
18
+
19
+ def set_hash_key(key)
20
+ @hash_key = key
21
+ end
22
+
23
+ def handle(column, format_class, options={})
24
+ define_handled_getter(column, format_class, options)
25
+ define_handled_setter(column)
26
+ end
27
+
15
28
  def hash_key
16
29
  @hash_key || :id
17
30
  end
@@ -96,36 +109,37 @@ module Dynamini
96
109
 
97
110
  end
98
111
 
112
+ #### Instance Methods
113
+
99
114
  def initialize(attributes={}, new_record = true)
100
- @attributes = attributes
101
115
  @changed = Set.new
102
116
  @new_record = new_record
103
- add_changed(attributes) if new_record
117
+ @attributes = {}
118
+
119
+ attributes.each do |k, v|
120
+ write_attribute(k, v, new_record)
121
+ end
104
122
  end
105
123
 
124
+
106
125
  def ==(object)
107
126
  hash_key == object.hash_key if object.is_a?(self.class)
108
127
  end
109
128
 
110
129
  def assign_attributes(attributes)
111
130
  attributes.each do |key, value|
112
- record_change(key, read_attribute(key), value)
131
+ write_attribute(key, value)
113
132
  end
114
- @attributes.merge!(attributes)
115
133
  nil
116
134
  end
117
135
 
118
136
  def update_attribute(key, value)
119
- record_change(key, value, @attributes[key])
120
- @attributes[key] = value
137
+ write_attribute(key, value)
121
138
  save!
122
139
  end
123
140
 
124
141
  def update_attributes(attributes)
125
- attributes.each do |key, value|
126
- record_change(key, read_attribute(key), value)
127
- end
128
- @attributes.merge!(attributes)
142
+ assign_attributes(attributes)
129
143
  save!
130
144
  end
131
145
 
@@ -202,7 +216,7 @@ module Dynamini
202
216
  end
203
217
 
204
218
  def touch_to_dynamo
205
- self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: updated_at, action: 'PUT'}})
219
+ self.class.client.update_item(table_name: self.class.table_name, key: key, attribute_updates: {updated_at: {value: attributes[:updated_at], action: 'PUT'}})
206
220
  end
207
221
 
208
222
  def delete_from_dynamo
@@ -259,14 +273,47 @@ module Dynamini
259
273
  name =~ /^([a-zA-Z][-_\w]*)=.*$/
260
274
  end
261
275
 
276
+ def self.define_handled_getter(column, format_class, options={})
277
+ case format_class
278
+ when :integer
279
+ proc = Proc.new {|v| v.to_i}
280
+ when :datetime
281
+ proc = Proc.new {|v| Time.at(v.to_f)}
282
+ when :float
283
+ proc = Proc.new {|v| v.to_f}
284
+ when :symbol
285
+ proc = Proc.new {|v| v.to_sym}
286
+ when :string
287
+ proc = Proc.new {|v| v}
288
+ else
289
+ raise 'Unsupported data type: ' + format_class.to_s
290
+ end
291
+
292
+ define_method(column) do
293
+ if read_attribute(column)
294
+ proc.call(read_attribute(column))
295
+ else
296
+ options[:default] || nil
297
+ end
298
+ end
299
+ end
300
+
301
+ def self.define_handled_setter(column)
302
+ setter_symbol = (column.to_s + '=').to_sym
303
+
304
+ define_method(setter_symbol) do |value|
305
+ write_attribute(column, value)
306
+ end
307
+ end
308
+
262
309
  def respond_to_missing?(name, include_private=false)
263
310
  @attributes.keys.include?(name) || write_method?(name) || super
264
311
  end
265
312
 
266
- def write_attribute(attribute, new_value)
313
+ def write_attribute(attribute, new_value, record_change=true)
267
314
  old_value = @attributes[attribute]
268
- @attributes[attribute] = new_value
269
- record_change(attribute, new_value, old_value)
315
+ @attributes[attribute] = (new_value.nil? ? nil : new_value.to_s)
316
+ record_change(attribute, new_value.to_s, old_value) if record_change
270
317
  end
271
318
 
272
319
  def record_change(attribute, new_value, old_value)
@@ -276,5 +323,10 @@ module Dynamini
276
323
  def read_attribute(name)
277
324
  @attributes[name]
278
325
  end
326
+
327
+ #### Default class macros
328
+
329
+ handle :updated_at, :datetime
330
+ handle :created_at, :datetime
279
331
  end
280
332
  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.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Greg Ward
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2015-09-18 00:00:00.000000000 Z
15
+ date: 2015-10-09 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activemodel