ocean-dynamo 0.3.8 → 0.3.9

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: a8d0a72119872cc64002656244d0340796eac76b
4
- data.tar.gz: febbde29e54a570653a6ada2f55a92c272491335
3
+ metadata.gz: d36ffc88503b1fb28acf30eb86d8b5e709561e72
4
+ data.tar.gz: 126589875287f2df88701c60aea134918e0b6c55
5
5
  SHA512:
6
- metadata.gz: b7f8dd862bfa88185538352545016d18345fe7eb160338a16dd959b0290c7be9ccdd255742886e69ebd2556b53bb4cd7bf9595d4f97cf583e9770bc087b23243
7
- data.tar.gz: c1aa7ec59bbe3990e9c2ee981ebea6d4742e95b750128227e05d10f897330f172a131e5400cede0d82ce71ff300423bf49d032991032b0b8422c81e952029b54
6
+ metadata.gz: 54c12079e7262327235bd3bae2fd40151c817ea7950ecbd1a22a61ac11f288c9bdec67a31f1e256b39aef30699aa56a1de14211ca29b2588f6a789d75a4e0d2a
7
+ data.tar.gz: 67e51cfa1e840deca03e0119b897b20a2a5de29b3f4b23df8b2669b375493efa728c914172820f1e160ff902cb0e4253bb2fe3b84384e049475fb76348db94b7
@@ -6,9 +6,9 @@ module OceanDynamo
6
6
  #
7
7
  attr_reader :attributes
8
8
 
9
- attr_reader :destroyed # :nodoc:
10
- attr_reader :new_record # :nodoc:
11
- attr_reader :dynamo_item # :nodoc:
9
+ attr_reader :destroyed # :nodoc:
10
+ attr_reader :new_record # :nodoc:
11
+ attr_reader :dynamo_item # :nodoc:
12
12
 
13
13
 
14
14
  def initialize(attrs={})
@@ -132,82 +132,6 @@ module OceanDynamo
132
132
  end
133
133
 
134
134
 
135
- def serialized_attributes
136
- result = Hash.new
137
- fields.each do |attribute, metadata|
138
- serialized = serialize_attribute(attribute, read_attribute(attribute), metadata)
139
- result[attribute] = serialized unless serialized == nil
140
- end
141
- result
142
- end
143
-
144
-
145
- def serialize_attribute(attribute, value, metadata=fields[attribute],
146
- target_class: metadata[:target_class],
147
- type: metadata[:type],
148
- no_save: metadata[:no_save]
149
- )
150
- return nil if value == nil
151
- case type
152
- when :reference
153
- return nil if no_save
154
- raise DynamoError, ":reference must always have a :target_class" unless target_class
155
- return value if value.is_a?(String)
156
- return value.id if value.is_a?(target_class)
157
- raise AssociationTypeMismatch, "can't save a #{value.class} in a #{target_class} :reference"
158
- when :string
159
- return nil if ["", []].include?(value)
160
- value
161
- when :integer
162
- value == [] ? nil : value
163
- when :float
164
- value == [] ? nil : value
165
- when :boolean
166
- value ? "true" : "false"
167
- when :datetime
168
- value.to_i
169
- when :serialized
170
- value.to_json
171
- else
172
- raise UnsupportedType.new(type.to_s)
173
- end
174
- end
175
-
176
-
177
- def deserialize_attribute(value, metadata, type: metadata[:type])
178
- case type
179
- when :reference
180
- return value
181
- when :string
182
- return "" if value == nil
183
- value.is_a?(Set) ? value.to_a : value
184
- when :integer
185
- return nil if value == nil
186
- value.is_a?(Set) || value.is_a?(Array) ? value.collect(&:to_i) : value.to_i
187
- when :float
188
- return nil if value == nil
189
- value.is_a?(Set) || value.is_a?(Array) ? value.collect(&:to_f) : value.to_f
190
- when :boolean
191
- case value
192
- when "true"
193
- true
194
- when "false"
195
- false
196
- else
197
- nil
198
- end
199
- when :datetime
200
- return nil if value == nil
201
- Time.zone.at(value.to_i)
202
- when :serialized
203
- return nil if value == nil
204
- JSON.parse(value)
205
- else
206
- raise UnsupportedType.new(type.to_s)
207
- end
208
- end
209
-
210
-
211
135
  private
212
136
 
213
137
  def _assign_attribute(k, v) # :nodoc:
@@ -180,6 +180,43 @@ module OceanDynamo
180
180
  end
181
181
 
182
182
 
183
+ #
184
+ # Sets the dynamo_item and deserialises and assigns all its defined
185
+ # attributes. Skips undeclared attributes.
186
+ #
187
+ # The arg may be either an Item or an ItemData. If Item, a request will be
188
+ # made for the attributes from DynamoDB. If ItemData, no DB access will
189
+ # be made and the existing data will be used.
190
+ #
191
+ # The :consistent keyword may only be used when the arg is an Item.
192
+ #
193
+ def _setup_from_dynamo(arg, consistent: false)
194
+ case arg
195
+ when AWS::DynamoDB::Item
196
+ item = arg
197
+ item_data = nil
198
+ when AWS::DynamoDB::ItemData
199
+ item = arg.item
200
+ item_data = arg
201
+ raise ArgumentError, ":consistent may not be specified when passing an ItemData" if consistent
202
+ else
203
+ raise ArgumentError, "arg must be an AWS::DynamoDB::Item or an AWS::DynamoDB::ItemData"
204
+ end
205
+
206
+ @dynamo_item = item
207
+
208
+ if !item_data
209
+ raw_attrs = item.attributes.to_hash(consistent_read: consistent)
210
+ else
211
+ raw_attrs = item_data.attributes
212
+ end
213
+
214
+ dynamo_deserialize_attributes(raw_attrs)
215
+ @new_record = false
216
+ self
217
+ end
218
+
219
+
183
220
 
184
221
  protected
185
222
 
@@ -196,11 +233,6 @@ module OceanDynamo
196
233
  end
197
234
 
198
235
 
199
- def perform_validations(options={}) # :nodoc:
200
- options[:validate] == false || valid?(options[:context])
201
- end
202
-
203
-
204
236
  def dynamo_persist(lock: nil) # :nodoc:
205
237
  _late_connect?
206
238
  begin
@@ -209,7 +241,6 @@ module OceanDynamo
209
241
  rescue AWS::DynamoDB::Errors::ConditionalCheckFailedException
210
242
  raise OceanDynamo::StaleObjectError.new(self)
211
243
  end
212
-
213
244
  @new_record = false
214
245
  true
215
246
  end
@@ -226,28 +257,94 @@ module OceanDynamo
226
257
  end
227
258
 
228
259
 
229
- def dynamo_unpersist(item, consistent) # :nodoc:
230
- _late_connect?
231
- @dynamo_item = item
232
- @new_record = false
233
- assign_attributes(_dynamo_read_attributes(consistent_read: consistent))
234
- self
260
+ def serialized_attributes
261
+ result = Hash.new
262
+ fields.each do |attribute, metadata|
263
+ serialized = serialize_attribute(attribute, read_attribute(attribute), metadata)
264
+ result[attribute] = serialized unless serialized == nil
265
+ end
266
+ result
235
267
  end
236
268
 
237
269
 
238
- def _dynamo_read_attributes(consistent_read: false) # :nodoc:
239
- hash = _dynamo_read_raw_attributes(consistent_read)
270
+ def dynamo_deserialize_attributes(hash) # :nodoc:
240
271
  result = Hash.new
241
272
  fields.each do |attribute, metadata|
242
273
  next if metadata[:no_save]
243
274
  result[attribute] = deserialize_attribute(hash[attribute], metadata)
244
275
  end
245
- result
276
+ assign_attributes(result)
277
+ end
278
+
279
+
280
+ def serialize_attribute(attribute, value, metadata=fields[attribute],
281
+ target_class: metadata[:target_class],
282
+ type: metadata[:type],
283
+ no_save: metadata[:no_save]
284
+ )
285
+ return nil if value == nil
286
+ case type
287
+ when :reference
288
+ return nil if no_save
289
+ raise DynamoError, ":reference must always have a :target_class" unless target_class
290
+ return value if value.is_a?(String)
291
+ return value.id if value.is_a?(target_class)
292
+ raise AssociationTypeMismatch, "can't save a #{value.class} in a #{target_class} :reference"
293
+ when :string
294
+ return nil if ["", []].include?(value)
295
+ value
296
+ when :integer
297
+ value == [] ? nil : value
298
+ when :float
299
+ value == [] ? nil : value
300
+ when :boolean
301
+ value ? "true" : "false"
302
+ when :datetime
303
+ value.to_i
304
+ when :serialized
305
+ value.to_json
306
+ else
307
+ raise UnsupportedType.new(type.to_s)
308
+ end
246
309
  end
247
310
 
248
311
 
249
- def _dynamo_read_raw_attributes(consistent) # :nodoc:
250
- dynamo_item.attributes.to_hash(consistent_read: consistent)
312
+ def deserialize_attribute(value, metadata, type: metadata[:type])
313
+ case type
314
+ when :reference
315
+ return value
316
+ when :string
317
+ return "" if value == nil
318
+ value.is_a?(Set) ? value.to_a : value
319
+ when :integer
320
+ return nil if value == nil
321
+ value.is_a?(Set) || value.is_a?(Array) ? value.collect(&:to_i) : value.to_i
322
+ when :float
323
+ return nil if value == nil
324
+ value.is_a?(Set) || value.is_a?(Array) ? value.collect(&:to_f) : value.to_f
325
+ when :boolean
326
+ case value
327
+ when "true"
328
+ true
329
+ when "false"
330
+ false
331
+ else
332
+ nil
333
+ end
334
+ when :datetime
335
+ return nil if value == nil
336
+ Time.zone.at(value.to_i)
337
+ when :serialized
338
+ return nil if value == nil
339
+ JSON.parse(value)
340
+ else
341
+ raise UnsupportedType.new(type.to_s)
342
+ end
343
+ end
344
+
345
+
346
+ def perform_validations(options={}) # :nodoc:
347
+ options[:validate] == false || valid?(options[:context])
251
348
  end
252
349
 
253
350
 
@@ -5,7 +5,7 @@ module OceanDynamo
5
5
  _late_connect?
6
6
  item = dynamo_items[hash, range]
7
7
  raise RecordNotFound, "can't find a #{self} with primary key ['#{hash}', #{range.inspect}]" unless item.exists?
8
- new.send(:dynamo_unpersist, item, consistent)
8
+ new._setup_from_dynamo(item, consistent: consistent)
9
9
  end
10
10
 
11
11
 
@@ -21,9 +21,25 @@ module OceanDynamo
21
21
  end
22
22
 
23
23
 
24
- def self.count
24
+ #
25
+ # The number of records in the table.
26
+ #
27
+ def self.count(**options)
25
28
  _late_connect?
26
- dynamo_table.item_count || -1 # The || -1 is for fake_dynamo specs.
29
+ dynamo_items.count(options)
30
+ end
31
+
32
+
33
+ #
34
+ # Returns all records in the table.
35
+ #
36
+ def self.all(**options)
37
+ _late_connect?
38
+ result = []
39
+ dynamo_items.select(options) do |item_data|
40
+ result << new._setup_from_dynamo(item_data)
41
+ end
42
+ result
27
43
  end
28
44
 
29
45
  end
@@ -32,7 +32,11 @@ module OceanDynamo
32
32
  self.timestamp_attributes = timestamps
33
33
  # Init
34
34
  self.fields = HashWithIndifferentAccess.new
35
- attribute table_hash_key, :string, default: ""
35
+ attribute(table_hash_key, :string, default: "")
36
+ if table_range_key
37
+ attribute(table_range_key, :string, default: "")
38
+ self.validates(table_range_key, presence: true)
39
+ end
36
40
  timestamp_attributes.each { |name| attribute name, :datetime } if timestamp_attributes
37
41
  attribute(lock_attribute, :integer, default: 0) if locking
38
42
  block.call
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "0.3.8"
2
+ VERSION = "0.3.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocean-dynamo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson