ocean-dynamo 0.2.1 → 0.2.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: c1d14c8cad14fe20abd48a743d192899d015e88f
4
- data.tar.gz: 931402dbece516985faaede7388447e3d656bc9a
3
+ metadata.gz: 38c9f3db161b07ef4486141afad09f05af00b752
4
+ data.tar.gz: 92a73810cedf7aceb64e31fde8582afd2e56fe18
5
5
  SHA512:
6
- metadata.gz: 572e3087a3b6e2ab6767838dd896386c2f8c66c8f6d111da6d4baf4e2d492120da79699c4bd372a961a452adedcfd7bbe81287fd5716440189fe749556e2e2ef
7
- data.tar.gz: 80d7a0713f3c6bf3d41b057977936df937c617a577845ae450fa7d38b0913a65cf8ace523fefe00367e578b3858676e209daf9b8d7e41dc00391e8bb34792034
6
+ metadata.gz: 0116191369abb7f9124034727ea50dc32995cfbebd55a4c030ded15a963c3f5fd7a1ed244ce85971606af4e329cf78f942b84a2d81e270af94bc4eddd135c3b1
7
+ data.tar.gz: 95861f8c29cd86c2018633549c21ce45b61adf65e8c2742496da04321ef6e2dd40447b8438c7d70ce36c40b35bcc87874bb65a50279ae835ef0f9a4d84b7cd5b
@@ -1,7 +1,7 @@
1
1
  module OceanDynamo
2
2
  class Base
3
- include ActiveModel::DeprecatedMassAssignmentSecurity
4
- include ActiveModel::ForbiddenAttributesProtection
3
+ #include ActiveModel::DeprecatedMassAssignmentSecurity
4
+ #include ActiveModel::ForbiddenAttributesProtection
5
5
 
6
6
  attr_reader :attributes
7
7
  attr_reader :destroyed
@@ -161,16 +161,6 @@ module OceanDynamo
161
161
  end
162
162
 
163
163
 
164
- def deserialized_attributes(consistent_read: false, hash: nil)
165
- hash ||= dynamo_item.attributes.to_hash(consistent_read: consistent_read)
166
- result = {}
167
- fields.each do |attribute, metadata|
168
- result[attribute] = deserialize_attribute(hash[attribute], metadata)
169
- end
170
- result
171
- end
172
-
173
-
174
164
  def deserialize_attribute(value, metadata, type: metadata[:type])
175
165
  case type
176
166
  when :string
@@ -34,5 +34,14 @@ module OceanDynamo
34
34
  class_attribute :fields, instance_writer: false
35
35
  self.fields = nil
36
36
 
37
+ class_attribute :table_connected, instance_writer: false
38
+ self.table_connected = false
39
+
40
+ class_attribute :table_connect_policy, instance_writer: false
41
+ self.table_connect_policy = :late
42
+
43
+ class_attribute :table_create_policy, instance_writer: false
44
+ self.table_create_policy = false
45
+
37
46
  end
38
47
  end
@@ -2,6 +2,8 @@ module OceanDynamo
2
2
 
3
3
  class DynamoError < StandardError; end
4
4
 
5
+ class TableNotFound < DynamoError; end
6
+
5
7
  class UnknownPrimaryKey < DynamoError; end
6
8
 
7
9
  class UnknownTableStatus < DynamoError; end
@@ -148,6 +148,7 @@ module OceanDynamo
148
148
 
149
149
 
150
150
  def delete
151
+ _connect_late?
151
152
  if persisted?
152
153
  @dynamo_item.delete
153
154
  end
@@ -166,6 +167,7 @@ module OceanDynamo
166
167
 
167
168
  def touch(name=nil)
168
169
  raise DynamoError, "can not touch on a new record object" unless persisted?
170
+ _connect_late?
169
171
  run_callbacks :touch do
170
172
  attrs = ['updated_at']
171
173
  attrs << name if name
@@ -185,27 +187,55 @@ module OceanDynamo
185
187
 
186
188
  protected
187
189
 
190
+ def self._connect_late?
191
+ return false if table_connected
192
+ return false unless table_connect_policy
193
+ establish_db_connection
194
+ true
195
+ end
196
+
197
+
198
+ def _connect_late?
199
+ self.class._connect_late?
200
+ end
201
+
202
+
188
203
  def perform_validations(options={}) # :nodoc:
189
204
  options[:validate] == false || valid?(options[:context])
190
205
  end
191
206
 
192
207
 
193
208
  def dynamo_persist # :nodoc:
209
+ _connect_late?
194
210
  @dynamo_item = dynamo_items.put(serialized_attributes)
195
211
  @new_record = false
212
+ true
196
213
  end
197
214
 
198
215
 
199
- def post_instantiate(item, consistent) # :nodoc:
216
+ def dynamo_unpersist(item, consistent) # :nodoc:
217
+ _connect_late?
200
218
  @dynamo_item = item
201
219
  @new_record = false
202
- assign_attributes(deserialized_attributes(
203
- hash: nil,
204
- consistent_read: consistent)
205
- )
220
+ assign_attributes(_dynamo_read_attributes(consistent_read: consistent))
206
221
  self
207
222
  end
208
223
 
209
224
 
225
+ def _dynamo_read_attributes(consistent_read: false) # :nodoc:
226
+ hash = _dynamo_read_raw_attributes(consistent_read)
227
+ result = {}
228
+ fields.each do |attribute, metadata|
229
+ result[attribute] = deserialize_attribute(hash[attribute], metadata)
230
+ end
231
+ result
232
+ end
233
+
234
+
235
+ def _dynamo_read_raw_attributes(consistent) # :nodoc:
236
+ dynamo_item.attributes.to_hash(consistent_read: consistent)
237
+ end
238
+
239
+
210
240
  end
211
241
  end
@@ -2,13 +2,15 @@ module OceanDynamo
2
2
  class Base
3
3
 
4
4
  def self.find(hash, range=nil, consistent: false)
5
+ _connect_late?
5
6
  item = dynamo_items[hash, range]
6
7
  raise RecordNotFound unless item.exists?
7
- new.send(:post_instantiate, item, consistent)
8
+ new.send(:dynamo_unpersist, item, consistent)
8
9
  end
9
10
 
10
11
 
11
12
  def self.count
13
+ _connect_late?
12
14
  dynamo_table.item_count || -1 # The || -1 is for fake_dynamo specs.
13
15
  end
14
16
 
@@ -8,12 +8,16 @@ module OceanDynamo
8
8
  table_name_suffix: nil,
9
9
  read_capacity_units: 10,
10
10
  write_capacity_units: 5,
11
- connect: true,
11
+ connect: :late,
12
+ create: false,
12
13
  &block)
13
14
  # Set class vars
14
15
  self.dynamo_client = nil
15
16
  self.dynamo_table = nil
16
17
  self.dynamo_items = nil
18
+ self.table_connected = false
19
+ self.table_connect_policy = connect
20
+ self.table_create_policy = create
17
21
  self.table_hash_key = table_hash_key
18
22
  self.table_range_key = table_range_key
19
23
  self.table_name = table_name
@@ -27,7 +31,7 @@ module OceanDynamo
27
31
  DEFAULT_ATTRIBUTES.each { |name, type, **pairs| attribute name, type, **pairs }
28
32
  block.call
29
33
  # Connect to AWS
30
- establish_db_connection if connect
34
+ establish_db_connection if connect == true
31
35
  # Finally return the full table name
32
36
  table_full_name
33
37
  end
@@ -5,7 +5,9 @@ module OceanDynamo
5
5
  setup_dynamo
6
6
  if dynamo_table.exists?
7
7
  wait_until_table_is_active
8
+ self.table_connected = true
8
9
  else
10
+ raise(TableNotFound, table_full_name) unless table_create_policy
9
11
  create_table
10
12
  end
11
13
  set_dynamo_table_keys
@@ -1,3 +1,3 @@
1
1
  module OceanDynamo
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Bengtson