dynamoid 1.3.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +3 -0
  4. data/.travis.yml +32 -7
  5. data/Appraisals +7 -0
  6. data/CHANGELOG.md +69 -2
  7. data/Gemfile +2 -0
  8. data/README.md +108 -28
  9. data/Rakefile +0 -24
  10. data/docker-compose.yml +7 -0
  11. data/dynamoid.gemspec +2 -3
  12. data/gemfiles/rails_4_0.gemfile +2 -3
  13. data/gemfiles/rails_4_1.gemfile +2 -3
  14. data/gemfiles/rails_4_2.gemfile +2 -3
  15. data/gemfiles/rails_5_0.gemfile +1 -1
  16. data/gemfiles/rails_5_1.gemfile +7 -0
  17. data/lib/dynamoid.rb +31 -31
  18. data/lib/dynamoid/adapter.rb +5 -5
  19. data/lib/dynamoid/adapter_plugin/aws_sdk_v2.rb +84 -57
  20. data/lib/dynamoid/associations.rb +21 -12
  21. data/lib/dynamoid/associations/association.rb +19 -3
  22. data/lib/dynamoid/associations/belongs_to.rb +26 -16
  23. data/lib/dynamoid/associations/has_and_belongs_to_many.rb +0 -16
  24. data/lib/dynamoid/associations/has_many.rb +2 -17
  25. data/lib/dynamoid/associations/has_one.rb +0 -14
  26. data/lib/dynamoid/associations/many_association.rb +19 -6
  27. data/lib/dynamoid/associations/single_association.rb +25 -7
  28. data/lib/dynamoid/config.rb +18 -18
  29. data/lib/dynamoid/config/options.rb +1 -1
  30. data/lib/dynamoid/criteria/chain.rb +29 -21
  31. data/lib/dynamoid/dirty.rb +2 -2
  32. data/lib/dynamoid/document.rb +17 -5
  33. data/lib/dynamoid/errors.rb +4 -1
  34. data/lib/dynamoid/fields.rb +6 -6
  35. data/lib/dynamoid/finders.rb +19 -9
  36. data/lib/dynamoid/identity_map.rb +0 -1
  37. data/lib/dynamoid/indexes.rb +41 -54
  38. data/lib/dynamoid/persistence.rb +54 -24
  39. data/lib/dynamoid/railtie.rb +1 -1
  40. data/lib/dynamoid/validations.rb +4 -3
  41. data/lib/dynamoid/version.rb +1 -1
  42. metadata +14 -29
  43. data/gemfiles/rails_4_0.gemfile.lock +0 -150
  44. data/gemfiles/rails_4_1.gemfile.lock +0 -154
  45. data/gemfiles/rails_4_2.gemfile.lock +0 -175
  46. data/gemfiles/rails_5_0.gemfile.lock +0 -180
@@ -21,7 +21,7 @@ module Dynamoid
21
21
  table_base_name = options[:name] || base_class.name.split('::').last
22
22
  .downcase.pluralize
23
23
 
24
- @table_name ||= [Dynamoid::Config.namespace.to_s,table_base_name].reject(&:empty?).join("_")
24
+ @table_name ||= [Dynamoid::Config.namespace.to_s, table_base_name].reject(&:empty?).join('_')
25
25
  end
26
26
 
27
27
  # Creates a table.
@@ -41,14 +41,14 @@ module Dynamoid
41
41
  range_key_hash = nil
42
42
  end
43
43
  options = {
44
- :id => self.hash_key,
45
- :table_name => self.table_name,
46
- :write_capacity => self.write_capacity,
47
- :read_capacity => self.read_capacity,
48
- :range_key => range_key_hash,
49
- :hash_key_type => dynamo_type(attributes[self.hash_key][:type]),
50
- :local_secondary_indexes => self.local_secondary_indexes.values,
51
- :global_secondary_indexes => self.global_secondary_indexes.values
44
+ id: self.hash_key,
45
+ table_name: self.table_name,
46
+ write_capacity: self.write_capacity,
47
+ read_capacity: self.read_capacity,
48
+ range_key: range_key_hash,
49
+ hash_key_type: dynamo_type(attributes[self.hash_key][:type]),
50
+ local_secondary_indexes: self.local_secondary_indexes.values,
51
+ global_secondary_indexes: self.global_secondary_indexes.values
52
52
  }.merge(options)
53
53
 
54
54
  Dynamoid.adapter.create_table(options[:table_name], options[:id], options)
@@ -74,9 +74,7 @@ module Dynamoid
74
74
  if incoming.has_key?(attribute)
75
75
  hash[attribute] = undump_field(incoming[attribute], options)
76
76
  elsif options.has_key?(:default)
77
- default_value = options[:default]
78
- value = default_value.respond_to?(:call) ? default_value.call : default_value.dup
79
- hash[attribute] = value
77
+ hash[attribute] = evaluate_default_value(options[:default])
80
78
  else
81
79
  hash[attribute] = nil
82
80
  end
@@ -140,13 +138,12 @@ module Dynamoid
140
138
  UNIX_EPOCH_DATE + value.to_i
141
139
  end
142
140
  when :boolean
143
- # persisted as 't', but because undump is called during initialize it can come in as true
144
141
  if value == 't' || value == true
145
142
  true
146
143
  elsif value == 'f' || value == false
147
144
  false
148
145
  else
149
- raise ArgumentError, "Boolean column neither true nor false"
146
+ raise ArgumentError, 'Boolean column neither true nor false'
150
147
  end
151
148
  else
152
149
  raise ArgumentError, "Unknown type #{options[:type]}"
@@ -185,7 +182,15 @@ module Dynamoid
185
182
  when :raw
186
183
  !value.nil? ? value : nil
187
184
  when :boolean
188
- !value.nil? ? value.to_s[0] : nil
185
+ if !value.nil?
186
+ if options[:store_as_native_boolean]
187
+ !!value # native boolean type
188
+ else
189
+ value.to_s[0] # => "f" or "t"
190
+ end
191
+ else
192
+ nil
193
+ end
189
194
  else
190
195
  raise ArgumentError, "Unknown type #{options[:type]}"
191
196
  end
@@ -207,6 +212,19 @@ module Dynamoid
207
212
  end
208
213
  end
209
214
 
215
+ def import(objects)
216
+ documents = objects.map { |attrs|
217
+ self.build(attrs).tap { |item|
218
+ item.hash_key = SecureRandom.uuid if item.hash_key.blank?
219
+ }
220
+ }
221
+
222
+ Dynamoid.adapter.batch_write_item(self.table_name, documents.map(&:dump))
223
+
224
+ documents.each { |d| d.new_record = false }
225
+ documents
226
+ end
227
+
210
228
  private
211
229
 
212
230
  def undump_hash(hash)
@@ -231,6 +249,20 @@ module Dynamoid
231
249
  val
232
250
  end
233
251
  end
252
+
253
+ # Evaluates the default value given, this is used by undump
254
+ # when determining the value of the default given for a field options.
255
+ #
256
+ # @param [Object] :value the attribute's default value
257
+ def evaluate_default_value(val)
258
+ if val.respond_to?(:call)
259
+ val.call
260
+ elsif val.duplicable?
261
+ val.dup
262
+ else
263
+ val
264
+ end
265
+ end
234
266
  end
235
267
 
236
268
  # Set updated_at and any passed in field to current DateTime. Useful for things like last_login_at, etc.
@@ -256,15 +288,13 @@ module Dynamoid
256
288
  self.class.create_table
257
289
 
258
290
  if new_record?
259
- conditions = { :unless_exists => [self.class.hash_key]}
291
+ conditions = { unless_exists: [self.class.hash_key]}
260
292
  conditions[:unless_exists] << range_key if(range_key)
261
293
 
262
294
  run_callbacks(:create) { persist(conditions) }
263
295
  else
264
296
  persist
265
297
  end
266
-
267
- self
268
298
  end
269
299
 
270
300
  #
@@ -274,10 +304,10 @@ module Dynamoid
274
304
  #
275
305
  def update!(conditions = {}, &block)
276
306
  run_callbacks(:update) do
277
- options = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
307
+ options = range_key ? {range_key: dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
278
308
 
279
309
  begin
280
- new_attrs = Dynamoid.adapter.update_item(self.class.table_name, self.hash_key, options.merge(:conditions => conditions)) do |t|
310
+ new_attrs = Dynamoid.adapter.update_item(self.class.table_name, self.hash_key, options.merge(conditions: conditions)) do |t|
281
311
  if(self.class.attributes[:lock_version])
282
312
  t.add(lock_version: 1)
283
313
  end
@@ -316,11 +346,11 @@ module Dynamoid
316
346
  #
317
347
  # @since 0.2.0
318
348
  def delete
319
- options = range_key ? {:range_key => dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
349
+ options = range_key ? {range_key: dump_field(self.read_attribute(range_key), self.class.attributes[range_key])} : {}
320
350
 
321
351
  # Add an optimistic locking check if the lock_version column exists
322
352
  if(self.class.attributes[:lock_version])
323
- conditions = {:if => {}}
353
+ conditions = {if: {}}
324
354
  conditions[:if][:lock_version] =
325
355
  if changes[:lock_version].nil?
326
356
  self.lock_version
@@ -360,7 +390,7 @@ module Dynamoid
360
390
  # @since 0.2.0
361
391
  def persist(conditions = nil)
362
392
  run_callbacks(:save) do
363
- self.hash_key = SecureRandom.uuid if self.hash_key.nil? || self.hash_key.blank?
393
+ self.hash_key = SecureRandom.uuid if self.hash_key.blank?
364
394
 
365
395
  # Add an exists check to prevent overwriting existing records with new ones
366
396
  if(new_record?)
@@ -372,7 +402,7 @@ module Dynamoid
372
402
  if(self.class.attributes[:lock_version])
373
403
  conditions ||= {}
374
404
  self.lock_version = (lock_version || 0) + 1
375
- #Uses the original lock_version value from ActiveModel::Dirty in case user changed lock_version manually
405
+ # Uses the original lock_version value from ActiveModel::Dirty in case user changed lock_version manually
376
406
  (conditions[:if] ||= {})[:lock_version] = changes[:lock_version][0] if(changes[:lock_version][0])
377
407
  end
378
408
 
@@ -3,7 +3,7 @@ if defined? (Rails)
3
3
  module Dynamoid
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
- Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
6
+ Dir[File.join(File.dirname(__FILE__), 'tasks/*.rake')].each { |f| load f }
7
7
  end
8
8
  end
9
9
  end
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Dynamoid
3
-
3
+
4
4
  # Provide ActiveModel validations to Dynamoid documents.
5
5
  module Validations
6
6
  extend ActiveSupport::Concern
@@ -12,7 +12,7 @@ module Dynamoid
12
12
  #
13
13
  # @since 0.2.0
14
14
  def save(options = {})
15
- options.reverse_merge!(:validate => true)
15
+ options.reverse_merge!(validate: true)
16
16
  return false if options[:validate] and (not valid?)
17
17
  super
18
18
  end
@@ -30,7 +30,8 @@ module Dynamoid
30
30
  # @since 0.2.0
31
31
  def save!
32
32
  raise Dynamoid::Errors::DocumentNotValid.new(self) unless valid?
33
- save(:validate => false)
33
+ save(validate: false)
34
+ self
34
35
  end
35
36
 
36
37
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module Dynamoid
2
- VERSION = "1.3.4"
2
+ VERSION = '2.0.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Symonds
@@ -20,7 +20,7 @@ authors:
20
20
  autorequire:
21
21
  bindir: exe
22
22
  cert_chain: []
23
- date: 2017-09-06 00:00:00.000000000 Z
23
+ date: 2017-12-20 00:00:00.000000000 Z
24
24
  dependencies:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
@@ -82,16 +82,16 @@ dependencies:
82
82
  name: pry
83
83
  requirement: !ruby/object:Gem::Requirement
84
84
  requirements:
85
- - - "~>"
85
+ - - ">="
86
86
  - !ruby/object:Gem::Version
87
- version: '0.10'
87
+ version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
90
  version_requirements: !ruby/object:Gem::Requirement
91
91
  requirements:
92
- - - "~>"
92
+ - - ">="
93
93
  - !ruby/object:Gem::Version
94
- version: '0.10'
94
+ version: '0'
95
95
  - !ruby/object:Gem::Dependency
96
96
  name: bundler
97
97
  requirement: !ruby/object:Gem::Requirement
@@ -180,30 +180,16 @@ dependencies:
180
180
  name: coveralls
181
181
  requirement: !ruby/object:Gem::Requirement
182
182
  requirements:
183
- - - ">="
184
- - !ruby/object:Gem::Version
185
- version: '0'
186
- type: :development
187
- prerelease: false
188
- version_requirements: !ruby/object:Gem::Requirement
189
- requirements:
190
- - - ">="
191
- - !ruby/object:Gem::Version
192
- version: '0'
193
- - !ruby/object:Gem::Dependency
194
- name: rspec-retry
195
- requirement: !ruby/object:Gem::Requirement
196
- requirements:
197
- - - ">="
183
+ - - "~>"
198
184
  - !ruby/object:Gem::Version
199
- version: '0'
185
+ version: '0.8'
200
186
  type: :development
201
187
  prerelease: false
202
188
  version_requirements: !ruby/object:Gem::Requirement
203
189
  requirements:
204
- - - ">="
190
+ - - "~>"
205
191
  - !ruby/object:Gem::Version
206
- version: '0'
192
+ version: '0.8'
207
193
  description: Dynamoid is an ORM for Amazon's DynamoDB that supports offline development,
208
194
  associations, querying, and everything else you'd expect from an ActiveRecord-style
209
195
  replacement.
@@ -216,6 +202,7 @@ extra_rdoc_files:
216
202
  - LICENSE.txt
217
203
  - README.md
218
204
  files:
205
+ - ".coveralls.yml"
219
206
  - ".document"
220
207
  - ".gitignore"
221
208
  - ".rspec"
@@ -227,15 +214,13 @@ files:
227
214
  - README.md
228
215
  - Rakefile
229
216
  - Vagrantfile
217
+ - docker-compose.yml
230
218
  - dynamoid.gemspec
231
219
  - gemfiles/rails_4_0.gemfile
232
- - gemfiles/rails_4_0.gemfile.lock
233
220
  - gemfiles/rails_4_1.gemfile
234
- - gemfiles/rails_4_1.gemfile.lock
235
221
  - gemfiles/rails_4_2.gemfile
236
- - gemfiles/rails_4_2.gemfile.lock
237
222
  - gemfiles/rails_5_0.gemfile
238
- - gemfiles/rails_5_0.gemfile.lock
223
+ - gemfiles/rails_5_1.gemfile
239
224
  - lib/dynamoid.rb
240
225
  - lib/dynamoid/adapter.rb
241
226
  - lib/dynamoid/adapter_plugin/aws_sdk_v2.rb
@@ -286,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
286
271
  version: '0'
287
272
  requirements: []
288
273
  rubyforge_project:
289
- rubygems_version: 2.6.12
274
+ rubygems_version: 2.6.14
290
275
  signing_key:
291
276
  specification_version: 4
292
277
  summary: Dynamoid is an ORM for Amazon's DynamoDB
@@ -1,150 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- dynamoid (1.3.0)
5
- activemodel (>= 4)
6
- aws-sdk-resources (~> 2)
7
- concurrent-ruby (>= 1.0)
8
-
9
- GEM
10
- remote: https://rubygems.org/
11
- specs:
12
- actionmailer (4.0.13)
13
- actionpack (= 4.0.13)
14
- mail (~> 2.5, >= 2.5.4)
15
- actionpack (4.0.13)
16
- activesupport (= 4.0.13)
17
- builder (~> 3.1.0)
18
- erubis (~> 2.7.0)
19
- rack (~> 1.5.2)
20
- rack-test (~> 0.6.2)
21
- activemodel (4.0.13)
22
- activesupport (= 4.0.13)
23
- builder (~> 3.1.0)
24
- activerecord (4.0.13)
25
- activemodel (= 4.0.13)
26
- activerecord-deprecated_finders (~> 1.0.2)
27
- activesupport (= 4.0.13)
28
- arel (~> 4.0.0)
29
- activerecord-deprecated_finders (1.0.4)
30
- activesupport (4.0.13)
31
- i18n (~> 0.6, >= 0.6.9)
32
- minitest (~> 4.2)
33
- multi_json (~> 1.3)
34
- thread_safe (~> 0.1)
35
- tzinfo (~> 0.3.37)
36
- appraisal (2.1.0)
37
- bundler
38
- rake
39
- thor (>= 0.14.0)
40
- arel (4.0.2)
41
- aws-sdk-core (2.8.11)
42
- aws-sigv4 (~> 1.0)
43
- jmespath (~> 1.0)
44
- aws-sdk-resources (2.8.11)
45
- aws-sdk-core (= 2.8.11)
46
- aws-sigv4 (1.0.0)
47
- builder (3.1.4)
48
- coderay (1.1.1)
49
- concurrent-ruby (1.0.5)
50
- coveralls (0.8.19)
51
- json (>= 1.8, < 3)
52
- simplecov (~> 0.12.0)
53
- term-ansicolor (~> 1.3)
54
- thor (~> 0.19.1)
55
- tins (~> 1.6)
56
- diff-lcs (1.3)
57
- docile (1.1.5)
58
- erubis (2.7.0)
59
- i18n (0.8.1)
60
- jmespath (1.3.1)
61
- json (2.0.3)
62
- mail (2.6.4)
63
- mime-types (>= 1.16, < 4)
64
- method_source (0.8.2)
65
- mime-types (3.1)
66
- mime-types-data (~> 3.2015)
67
- mime-types-data (3.2016.0521)
68
- mini_portile2 (2.1.0)
69
- minitest (4.7.5)
70
- multi_json (1.12.1)
71
- nokogiri (1.6.8.1)
72
- mini_portile2 (~> 2.1.0)
73
- pry (0.10.4)
74
- coderay (~> 1.1.0)
75
- method_source (~> 0.8.1)
76
- slop (~> 3.4)
77
- rack (1.5.5)
78
- rack-test (0.6.3)
79
- rack (>= 1.0)
80
- rails (4.0.13)
81
- actionmailer (= 4.0.13)
82
- actionpack (= 4.0.13)
83
- activerecord (= 4.0.13)
84
- activesupport (= 4.0.13)
85
- bundler (>= 1.3.0, < 2.0)
86
- railties (= 4.0.13)
87
- sprockets-rails (~> 2.0)
88
- railties (4.0.13)
89
- actionpack (= 4.0.13)
90
- activesupport (= 4.0.13)
91
- rake (>= 0.8.7)
92
- thor (>= 0.18.1, < 2.0)
93
- rake (12.0.0)
94
- rspec (3.5.0)
95
- rspec-core (~> 3.5.0)
96
- rspec-expectations (~> 3.5.0)
97
- rspec-mocks (~> 3.5.0)
98
- rspec-core (3.5.4)
99
- rspec-support (~> 3.5.0)
100
- rspec-expectations (3.5.0)
101
- diff-lcs (>= 1.2.0, < 2.0)
102
- rspec-support (~> 3.5.0)
103
- rspec-mocks (3.5.0)
104
- diff-lcs (>= 1.2.0, < 2.0)
105
- rspec-support (~> 3.5.0)
106
- rspec-retry (0.5.3)
107
- rspec-core (> 3.3, < 3.6)
108
- rspec-support (3.5.0)
109
- simplecov (0.12.0)
110
- docile (~> 1.1.0)
111
- json (>= 1.8, < 3)
112
- simplecov-html (~> 0.10.0)
113
- simplecov-html (0.10.0)
114
- slop (3.6.0)
115
- sprockets (3.7.1)
116
- concurrent-ruby (~> 1.0)
117
- rack (> 1, < 3)
118
- sprockets-rails (2.3.3)
119
- actionpack (>= 3.0)
120
- activesupport (>= 3.0)
121
- sprockets (>= 2.8, < 4.0)
122
- term-ansicolor (1.4.1)
123
- tins (~> 1.0)
124
- thor (0.19.4)
125
- thread_safe (0.3.6)
126
- tins (1.13.2)
127
- tzinfo (0.3.53)
128
- wwtd (1.3.0)
129
- yard (0.9.8)
130
-
131
- PLATFORMS
132
- ruby
133
-
134
- DEPENDENCIES
135
- activesupport (>= 4)
136
- appraisal (~> 2.1)
137
- bundler (~> 1.14)
138
- coveralls
139
- dynamoid!
140
- nokogiri (~> 1.6.8.1)
141
- pry (~> 0.10)
142
- rails (~> 4.0.0)
143
- rake (~> 12.0)
144
- rspec (~> 3.0)
145
- rspec-retry
146
- wwtd (~> 1.3)
147
- yard
148
-
149
- BUNDLED WITH
150
- 1.14.6