dynamosaurus 0.0.7 → 0.0.8

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: 274b730e40fab7105a116ae009d17a1407bb5e90
4
- data.tar.gz: 7e8d5b5d78047e5eb5eeeb2cee5278ec2c5735fd
3
+ metadata.gz: 61f38932ba6096baa0ae0952bb548637b2829640
4
+ data.tar.gz: dc8455607eaad41f4c7ad2f1a8657c0f8b9770c0
5
5
  SHA512:
6
- metadata.gz: caa0fb68c56a7bfa4fb7ac73f667089891975fdb7cfeeaebc05f92268d7afa2596138537da4675bfbd26e7c6e2d2258ad348f2dc310e8b0e30ba78d791c76b54
7
- data.tar.gz: a5d5d146c0399aaa0e3e869be0e41d9d385e44d25399aea762d0646d7f09aced7239baebc1f284be90bbc93c968aaf065dc5c5672d855e7dae026b46ba5df645
6
+ metadata.gz: 981993c41a99915f41d1b610d19cba3af6ccb944b8f255bea5ce3965ad190a38d96aa8f4b5ba5b730f6225fa8c8dd4b6a76703d24b40f87d866bb266f5a367af
7
+ data.tar.gz: 839c4eaa14d3adc3bf7d406cf8791284e5c66950313c2b25d794b8b8f0ea0275233f76e2c75a3e88def82a4f206558d17fc77b7c47a58a027ac94ecf07950c12
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ # Unix-style newlines with a newline ending every file
4
+ [*]
5
+ indent_style = space
6
+ indent_size = 2
7
+ end_of_line = lf
8
+ charset = utf-8
9
+ trim_trailing_whitespace = true
10
+ insert_final_newline = true
11
+
12
+
13
+ tab_width = 4
@@ -6,11 +6,11 @@ module Dynamosaurus
6
6
  :string => :s,
7
7
  :number => :n
8
8
  }
9
-
9
+
10
10
  def dynamo_db
11
11
  self.class.dynamo_db
12
12
  end
13
-
13
+
14
14
  def initialize params
15
15
  data = params[:data] if params[:data]
16
16
  @data = Dynamosaurus::DynamoBase.res2hash(data)
@@ -19,7 +19,7 @@ module Dynamosaurus
19
19
  def table_name
20
20
  self.class.table_name
21
21
  end
22
-
22
+
23
23
  def [] key
24
24
  @data[key]
25
25
  end
@@ -27,7 +27,7 @@ module Dynamosaurus
27
27
  def []=(key,value)
28
28
  @data[key] = value
29
29
  end
30
-
30
+
31
31
  def data
32
32
  @data
33
33
  end
@@ -35,7 +35,7 @@ module Dynamosaurus
35
35
  def exist?
36
36
  ! @data.empty?
37
37
  end
38
-
38
+
39
39
  def empty?
40
40
  @data.empty?
41
41
  end
@@ -43,7 +43,7 @@ module Dynamosaurus
43
43
  def try name
44
44
  @data[name.to_s]
45
45
  end
46
-
46
+
47
47
  def method_missing(name, *params)
48
48
  name = name.to_s[0..-2] if name.to_s[-1] == "="
49
49
  if exist? and @data.has_key?(name.to_s)
@@ -56,7 +56,7 @@ module Dynamosaurus
56
56
  super
57
57
  end
58
58
  end
59
-
59
+
60
60
  def key
61
61
  return @key if @key
62
62
  @key = self.class.get_key
@@ -88,7 +88,7 @@ module Dynamosaurus
88
88
  :action => "PUT"
89
89
  }
90
90
  end
91
-
91
+
92
92
  query = {
93
93
  :table_name => self.class.table_name,
94
94
  :key => keys,
@@ -126,7 +126,7 @@ module Dynamosaurus
126
126
  }
127
127
  @data.delete(k.to_s)
128
128
  end
129
-
129
+
130
130
  res = dynamo_db.update_item(
131
131
  :table_name => self.class.table_name,
132
132
  :key => keys,
@@ -32,9 +32,13 @@ module Dynamosaurus
32
32
  end
33
33
 
34
34
  def table_name
35
- (@table_prefix || name.downcase.split("::").last) + (ENV['DYNAMODB_SUFFIX'] || "_local")
35
+ if @table && @table.has_key?(:name) && !@table[:name].empty?
36
+ @table[:name].to_s
37
+ else
38
+ (@table_prefix || name.downcase.split("::").last) + (ENV['DYNAMODB_SUFFIX'] || "_local")
39
+ end
36
40
  end
37
-
41
+
38
42
  def dynamo_db
39
43
  return @dynamo_db if @dynamo_db
40
44
  if Aws::config.empty?
@@ -47,11 +51,15 @@ module Dynamosaurus
47
51
  end
48
52
  end
49
53
 
54
+ def table options = {}
55
+ @table ||= options
56
+ end
57
+
50
58
  def key k, type, range_key=nil, range_key_type=nil
51
59
  @key = [k, Dynamosaurus::DynamoBase::TYPES[type]]
52
60
  @key << range_key << Dynamosaurus::DynamoBase::TYPES[range_key_type] if range_key
53
61
  end
54
-
62
+
55
63
  def get_key
56
64
  @key
57
65
  end
@@ -67,20 +75,20 @@ module Dynamosaurus
67
75
  :write_capacity_units => 10
68
76
  }
69
77
 
70
- @schema[:key_schema] << {
78
+ @schema[:key_schema] << {
71
79
  :key_type => "HASH",
72
80
  :attribute_name => get_key[0].to_s
73
81
  }
74
82
  @schema[:attribute_definitions] << {:attribute_name => get_key[0].to_s, :attribute_type => get_key[1].to_s.upcase}
75
83
 
76
84
  if get_key.size == 4
77
- @schema[:key_schema] << {
85
+ @schema[:key_schema] << {
78
86
  :key_type => "RANGE",
79
87
  :attribute_name => get_key[2].to_s
80
88
  }
81
89
  @schema[:attribute_definitions] << {:attribute_name => get_key[2].to_s, :attribute_type => get_key[3].to_s.upcase}
82
90
  end
83
-
91
+
84
92
  unless get_global_indexes.empty?
85
93
  @schema[:global_secondary_indexes] = []
86
94
  get_global_indexes.each do |g_index|
@@ -107,7 +115,7 @@ module Dynamosaurus
107
115
  }
108
116
  @schema[:attribute_definitions] << {:attribute_name => g_index[1][2].to_s, :attribute_type => g_index[1][3].to_s.upcase}
109
117
  end
110
-
118
+
111
119
  @schema[:global_secondary_indexes] << index_schema
112
120
  end
113
121
  end
@@ -121,7 +129,7 @@ module Dynamosaurus
121
129
  :projection_type => "KEYS_ONLY",
122
130
  },
123
131
  }
124
- index_schema[:key_schema] =[
132
+ index_schema[:key_schema] =[
125
133
  {
126
134
  :key_type => "HASH",
127
135
  :attribute_name => s_index_value[0]
@@ -138,13 +146,13 @@ module Dynamosaurus
138
146
 
139
147
  @schema
140
148
  end
141
-
149
+
142
150
  def global_index index_name, key, key_type, range_key=nil, range_key_type=nil
143
151
  @global_index = {} if @global_index.nil?
144
152
  @global_index[index_name] = [key, Dynamosaurus::DynamoBase::TYPES[key_type]]
145
153
  @global_index[index_name] << range_key << Dynamosaurus::DynamoBase::TYPES[range_key_type] if range_key
146
154
  end
147
-
155
+
148
156
  def get_global_indexes
149
157
  (@global_index) ? @global_index : {}
150
158
 
@@ -189,7 +197,7 @@ module Dynamosaurus
189
197
  def table_prefix name
190
198
  @table_prefix = name
191
199
  end
192
-
200
+
193
201
  def key_list
194
202
  keys = get_key + get_secondary_indexes.values.flatten
195
203
  list = {}
@@ -205,7 +213,7 @@ module Dynamosaurus
205
213
  return new_hash if hash.nil?
206
214
  hash
207
215
  end
208
-
216
+
209
217
 
210
218
  def query_without_index value, option
211
219
  keys = {}
@@ -238,7 +246,7 @@ module Dynamosaurus
238
246
 
239
247
  item_key = get_item_key(value)
240
248
  Dynamosaurus.logger << "get_item #{table_name} #{item_key}"
241
-
249
+
242
250
  res = dynamo_db.get_item(
243
251
  :table_name => table_name,
244
252
  :key => item_key
@@ -246,14 +254,14 @@ module Dynamosaurus
246
254
  if res.item
247
255
  new :data => res.item
248
256
  else
249
- nil
257
+ nil
250
258
  end
251
259
  end
252
-
260
+
253
261
  def get_from_index hash, option={}
254
262
  if index = get_index(hash)
255
263
  keys = {}
256
-
264
+
257
265
  index[:keys].each do |key|
258
266
  keys[key] = {
259
267
  :comparison_operator => "EQ",
@@ -273,7 +281,7 @@ module Dynamosaurus
273
281
  :comparison_operator => "EQ",
274
282
  :attribute_value_list => [ v.to_s ]
275
283
  }
276
- end
284
+ end
277
285
  Dynamosaurus.logger << "query local_index #{table_name} #{keys}"
278
286
  query keys, index, option
279
287
  end
@@ -335,7 +343,7 @@ module Dynamosaurus
335
343
  my_keys = []
336
344
  keys[get_key[0]].each do |key1|
337
345
  keys[get_key[2]].each do |key2|
338
- my_keys << {
346
+ my_keys << {
339
347
  get_key[0].to_s => key1,
340
348
  get_key[2].to_s => key2,
341
349
  }
@@ -349,7 +357,7 @@ module Dynamosaurus
349
357
  :keys => my_keys
350
358
  }
351
359
  })
352
-
360
+
353
361
  if res.responses[table_name]
354
362
  return res.responses[table_name].map{|item|
355
363
  new :data => item
@@ -382,9 +390,9 @@ module Dynamosaurus
382
390
  }
383
391
  end
384
392
  end
385
-
393
+
386
394
  def put hash, num_hash={}, return_values=nil
387
-
395
+
388
396
  new_hash = {}
389
397
  hash.each{|key, value|
390
398
  new_hash[key] = value unless value.nil?
@@ -392,7 +400,7 @@ module Dynamosaurus
392
400
  num_hash.merge({:updated_at => Time.now.to_i}).each{|key, value|
393
401
  new_hash[key] = value.to_i unless value.nil?
394
402
  } if num_hash
395
-
403
+
396
404
  res = dynamo_db.put_item(
397
405
  :table_name => table_name,
398
406
  :item => new_hash,
@@ -403,17 +411,17 @@ module Dynamosaurus
403
411
  def save hash, num_hash={}, return_values=nil
404
412
  put(hash, num_hash, return_values)
405
413
  my_keys = get_key
406
-
414
+
407
415
  if my_keys.size == 4
408
416
  get([hash[my_keys[0]], hash[my_keys[2]]])
409
417
  else
410
418
  get(hash[my_keys[0]])
411
419
  end
412
420
  end
413
-
421
+
414
422
  def add key=[], attribute_nums={}, options={}
415
423
  Dynamosaurus.logger << "update"
416
-
424
+
417
425
  attribute_updates = {}
418
426
  attribute_nums.each do |k, v|
419
427
  attribute_updates[k.to_s] = {
@@ -421,10 +429,10 @@ module Dynamosaurus
421
429
  :action => "ADD"
422
430
  }
423
431
  end
424
-
432
+
425
433
  class_key = get_key
426
434
  keys = {
427
- class_key[0].to_sym => key.is_a?(Array) ? key[0] : key
435
+ class_key[0].to_sym => key.is_a?(Array) ? key[0] : key
428
436
  }
429
437
  keys[class_key[2].to_sym] = key[1] if class_key.size > 2
430
438
 
@@ -435,7 +443,7 @@ module Dynamosaurus
435
443
  }
436
444
  query = query.merge(options)
437
445
  res = dynamo_db.update_item(query)
438
-
446
+
439
447
  end
440
448
 
441
449
  def delete_item value
@@ -1,3 +1,3 @@
1
1
  module Dynamosaurus
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Dynamosaurus do
4
- before(:all) do
4
+ before(:all) do
5
5
  ENV['DYNAMODB_SUFFIX'] = "_local"
6
6
 
7
7
  Aws.config = {
@@ -12,7 +12,7 @@ describe Dynamosaurus do
12
12
  Dynamosaurus::DynamoBase.create_tables
13
13
  end
14
14
 
15
- after(:all) do
15
+ after(:all) do
16
16
  connect = Dynamosaurus::DynamoBase.dynamo_db
17
17
  Dynamosaurus::DynamoBase.tables.each do |table_name|
18
18
  connect.delete_table(:table_name => table_name)
@@ -39,7 +39,7 @@ describe Dynamosaurus do
39
39
  SimpleOrderedKVS.put({:simple_key => "key", :simple_id => "3"})
40
40
  sleep 1
41
41
  SimpleOrderedKVS.put({:simple_key => "key", :simple_id => "2"})
42
-
42
+
43
43
  orderd_items = SimpleOrderedKVS.get({
44
44
  :index => "updated_at_index",
45
45
  :simple_key => "key"
@@ -123,29 +123,67 @@ describe Dynamosaurus do
123
123
  kvs.delete
124
124
  kvs = SimpleKVS.get("key3")
125
125
  expect(kvs).to be_nil
126
-
126
+
127
127
  end
128
128
 
129
- it 'comment test' do
130
- expect(Comment.first).to be_nil
131
- expect(Comment.get(["1", "1"])).to be_nil
129
+ shared_examples_for 'Basic CRUD' do
130
+ it 'should work in a sequence' do
131
+ expect(Object.const_get(model_name.to_s).first).to be_nil
132
+ expect(Object.const_get(model_name.to_s).get(["1", "1"])).to be_nil
132
133
 
133
- Comment.put({:content_id => "1", :message_id => "1", :user_id => "abc"})
134
- expect(Comment.first.content_id).to eq "1"
134
+ Object.const_get(model_name.to_s).put({:content_id => "1", :message_id => "1", :user_id => "abc"})
135
+ expect(Object.const_get(model_name.to_s).first.content_id).to eq "1"
135
136
 
136
- kvs = Comment.get(["1", "1"])
137
- expect(kvs.content_id).to eq "1"
137
+ kvs = Object.const_get(model_name.to_s).get(["1", "1"])
138
+ expect(kvs.content_id).to eq "1"
138
139
 
139
- kvs = Comment.get(["2", "2"])
140
- expect(kvs).to be_nil
140
+ kvs = Object.const_get(model_name.to_s).get(["2", "2"])
141
+ expect(kvs).to be_nil
142
+ end
143
+ end
144
+
145
+ describe 'global secondary index' do
146
+ it_should_behave_like 'Basic CRUD' do
147
+ let!(:model_name) { 'Comment' }
148
+ end
149
+
150
+ context 'when 2 records exist' do
151
+ before do
152
+ Comment.put({:content_id => "1", :message_id => "2", :user_id => "abc"})
153
+ Comment.put({:content_id => "1", :message_id => "3", :user_id => "xyz"})
154
+ end
155
+
156
+ subject { Comment.get({:user_id => "abc"}).size }
141
157
 
142
- Comment.put({:content_id => "1", :message_id => "2", :user_id => "abc"})
143
- Comment.put({:content_id => "1", :message_id => "3", :user_id => "xyz"})
144
-
145
- # global secondary index
146
- comments = Comment.get({:user_id => "abc"})
147
- expect(comments.size).to eq 2
158
+ it{ is_expected.to eq 2 }
159
+ end
160
+ end
161
+
162
+ describe 'table options' do
163
+ context 'when table-name is given' do
164
+ class DynamoModelWithTableField < Dynamosaurus::DynamoBase
165
+ table name: 'table_name'
166
+ key :content_id, :string, :message_id, :string
167
+ end
148
168
 
169
+ it_should_behave_like 'Basic CRUD' do
170
+ let!(:model_name) { 'DynamoModelWithTableField' }
171
+ end
172
+
173
+ it { expect(DynamoModelWithTableField.table_name).to eq 'table_name' }
174
+ end
175
+
176
+ context 'when table-name is not given' do
177
+ class DynamoModelWithoutTableField < Dynamosaurus::DynamoBase
178
+ key :content_id, :string, :message_id, :string
179
+ end
180
+
181
+ it_should_behave_like 'Basic CRUD' do
182
+ let!(:model_name) { 'DynamoModelWithoutTableField' }
183
+ end
184
+
185
+ it { expect(DynamoModelWithoutTableField.table_name).to eq 'dynamomodelwithouttablefield_local' }
186
+ end
149
187
  end
150
188
 
151
189
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamosaurus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Isamu Arimoto
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-05 00:00:00.000000000 Z
11
+ date: 2016-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - .editorconfig
76
77
  - .gitignore
77
78
  - .rspec
78
79
  - .travis.yml