dymos 0.1.4 → 0.2.0

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.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +71 -2
  3. data/lib/dymos/client.rb +16 -0
  4. data/lib/dymos/config.rb +34 -0
  5. data/lib/dymos/model.rb +109 -26
  6. data/lib/dymos/persistence.rb +75 -16
  7. data/lib/dymos/query/base.rb +23 -0
  8. data/lib/dymos/query/create_table.rb +72 -0
  9. data/lib/dymos/query/delete_item.rb +60 -32
  10. data/lib/dymos/query/describe.rb +3 -5
  11. data/lib/dymos/query/get_item.rb +30 -9
  12. data/lib/dymos/query/put_item.rb +67 -41
  13. data/lib/dymos/query/query.rb +112 -26
  14. data/lib/dymos/query/scan.rb +85 -12
  15. data/lib/dymos/query/update_item.rb +97 -50
  16. data/lib/dymos/version.rb +1 -1
  17. data/lib/dymos.rb +6 -4
  18. data/spec/lib/dymos/client_spec.rb +4 -0
  19. data/spec/lib/dymos/config_spec.rb +11 -0
  20. data/spec/lib/dymos/data.yml +154 -0
  21. data/spec/lib/dymos/model_spec.rb +23 -29
  22. data/spec/lib/dymos/query/create_table_spec.rb +56 -0
  23. data/spec/lib/dymos/query/delete_item_spec.rb +22 -79
  24. data/spec/lib/dymos/query/describe_spec.rb +6 -40
  25. data/spec/lib/dymos/query/get_item_spec.rb +8 -47
  26. data/spec/lib/dymos/query/put_item_spec.rb +45 -301
  27. data/spec/lib/dymos/query/query_spec.rb +45 -95
  28. data/spec/lib/dymos/query/scan_spec.rb +49 -43
  29. data/spec/lib/dymos/query/update_item_spec.rb +36 -113
  30. data/spec/lib/dymos/query_spec.rb +150 -0
  31. data/spec/spec_helper.rb +33 -0
  32. metadata +16 -12
  33. data/lib/dymos/command.rb +0 -36
  34. data/lib/dymos/query/attribute.rb +0 -14
  35. data/lib/dymos/query/builder.rb +0 -79
  36. data/lib/dymos/query/expect.rb +0 -65
  37. data/spec/lib/dymos/query/attribute_spec.rb +0 -15
  38. data/spec/lib/dymos/query/builder_spec.rb +0 -23
  39. data/spec/lib/dymos/query/expect_spec.rb +0 -5
@@ -1,119 +1,42 @@
1
1
  describe Dymos::Query::UpdateItem do
2
- before :all do
3
- Aws.config[:region] = 'us-west-1'
4
- Aws.config[:endpoint] = 'http://localhost:4567'
5
- Aws.config[:access_key_id] = 'XXX'
6
- Aws.config[:secret_access_key] = 'XXX'
7
-
8
- client = Aws::DynamoDB::Client.new
9
- client.delete_table(table_name: 'test_update_item') if client.list_tables[:table_names].include?('test_update_item')
10
- client.create_table(
11
- table_name: 'test_update_item',
12
- attribute_definitions: [
13
- {attribute_name: 'id', attribute_type: 'S'},
14
- ],
15
- key_schema: [
16
- {attribute_name: 'id', key_type: 'HASH'}
17
- ],
18
- provisioned_throughput: {
19
- read_capacity_units: 1,
20
- write_capacity_units: 1,
21
- })
22
- client.put_item(table_name: 'test_update_item', item: {id: 'hoge', name: '太郎'})
23
- client.put_item(table_name: 'test_update_item', item: {id: 'fuga', count: 0})
24
- client.put_item(table_name: 'test_update_item', item: {id: 'poyo', name: '可奈'})
25
- client.put_item(table_name: 'test_update_item', item: {id: 'piyo', name: '杏奈', count: 10})
26
-
27
- class TestItem < Dymos::Model
28
- table :test_update_item
29
- field :id, :string
30
- field :name, :string
31
- field :count, :string
32
- end
2
+ before do
3
+ Dymos::Config.default[:update_item]={
4
+ return_values: 'ALL_NEW'
5
+ }
33
6
  end
34
7
 
35
- let(:client) { Aws::DynamoDB::Client.new }
36
- describe :put_item do
37
- describe "クエリ生成" do
38
- describe "新しいアイテムを追加" do
39
- let(:query) { TestItem.update.key(id: "foo").attribute_updates({name: "Sam"}, 'PUT') }
40
- it :query do
41
- expect(query.query).to eq({
42
- table_name: "test_update_item",
43
- key: {id: "foo"},
44
- attribute_updates: {name: {value: "Sam", action: "PUT"}},
45
- return_values: "ALL_NEW",
46
- })
47
- end
48
- it "成功すると新しいアイテムを返す" do
49
- res = query.execute client
50
- expect(res.id).to eq("foo")
51
- end
52
- end
53
-
54
- describe "既存のアイテムを更新" do
55
- describe "return_values :ALL_OLD" do
56
- it "更新した場合は更新前のアイテムを返す" do
57
- query = TestItem.update.key(id: "hoge").attribute_updates({name: "次郎"}, 'PUT').return_values(:ALL_OLD)
58
- res = query.execute client
59
- expect(res.name).to eq("太郎")
60
- end
61
- it "追加した場合はnilを返す" do
62
- query = TestItem.update.key(id: "bar").attribute_updates({name: "Sam"}, 'PUT').return_values(:ALL_OLD)
63
- res = query.execute client
64
- expect(res).to eq(nil)
65
- end
66
- end
67
- it "アイテムに加算する" do
68
- query = TestItem.update.key(id: "fuga").attribute_updates({count: 1}, 'add')
69
- res = query.execute client
70
- expect(res.count).to eq(1)
71
- end
72
-
73
- describe "条件付き更新" do
74
- describe :== do
75
- let(:query) { TestItem.update.key(id: "poyo").attribute_updates({name: "志保"}, 'PUT').expected(name: "== 可奈") }
76
-
77
- it :query do
78
- expect(query.query).to eq({
79
- table_name: "test_update_item",
80
- key: {id: "poyo"},
81
- attribute_updates: {name: {value: "志保", action: "PUT"}},
82
- expected: ({name: {value: "可奈", comparison_operator: "EQ"}}),
83
- return_values: "ALL_NEW",
84
- })
85
- end
86
- it "成功すると新しいアイテムを返す" do
87
- res = query.execute client
88
- expect(res.name).to eq("志保")
89
- end
90
- end
91
- describe "複数条件" do
92
- let(:query) { TestItem.update.key(id: "piyo").attribute_updates({name: "百合子"}, 'PUT')
93
- .expected(count: "between 9 12", name: "== 杏奈", hoge:"is_null")}
94
-
95
- it :query do
96
- expect(query.query).to eq({
97
- table_name: "test_update_item",
98
- key: {id: "piyo"},
99
- attribute_updates: {name: {value: "百合子", action: "PUT"}},
100
- expected: ({
101
- name: {value: "杏奈", comparison_operator: "EQ"},
102
- count: {attribute_value_list: [9, 12], comparison_operator: "BETWEEN"},
103
- hoge: {comparison_operator: "NULL"},
104
- }),
105
- return_values: "ALL_NEW",
106
- conditional_operator: "AND"
107
- })
108
- end
109
- it "成功すると新しいアイテムを返す" do
110
- res = query.execute client
111
- expect(res.name).to eq("百合子")
112
- expect(res.metadata[:consumed_capacity]).to eq(nil)
113
- end
114
- end
115
- end
116
- end
8
+ describe 'build query', order: :defined do
9
+ let(:builder) { Dymos::Query::UpdateItem.new }
10
+ table='test_update_item_table'
11
+
12
+ before :all do
13
+ @client.delete_table(table_name: table) if @client.list_tables[:table_names].include?(table)
14
+ query=Dymos::Query::CreateTable.new.name(table).attributes(id: 'S').keys(id: 'HASH').build
15
+ @client.create_table(query)
16
+ @client.put_item(table_name: table, item: {id: 'hoge', param1: 101, param2: 201, param3: "hoge", param4: [1, 0, 1]})
17
+ @client.put_item(table_name: table, item: {id: 'fuga', param1: 102, param2: 202, param3: "fuga", param4: [1, 0, 2]})
18
+ @client.put_item(table_name: table, item: {id: 'piyo', param1: 103, param2: 203, param3: "piyo", param4: [1, 0, 3]})
19
+ end
20
+ it :attribute_updates do
21
+ builder.name(table).key(id: 'hoge').attribute_updates([:param1, :add, 1000], [:param2, :put, 1201]).return_values(:all_new)
22
+ result = @client.update_item(builder.build)
23
+ expect(result.error).to eq(nil)
24
+ expect(result.attributes["param1"]).to eq(1101)
25
+ expect(result.attributes["param2"]).to eq(1201)
26
+ end
27
+ it 'put, add' do
28
+ builder.name(table).key(id: 'hoge').add(:param1, 1).put(:param2, 1202)
29
+ result = @client.update_item(builder.build)
30
+ expect(result.error).to eq(nil)
31
+ expect(result.attributes["param1"]).to eq(1102)
32
+ expect(result.attributes["param2"]).to eq(1202)
33
+ end
34
+ it 'expected' do
35
+ builder.name(table).key(id: 'hoge').add(:param1, 1).put(:param2, 1203).expected(param3: "hoge", param4: [1, 0, 1])
36
+ result = @client.update_item(builder.build)
37
+ expect(result.error).to eq(nil)
38
+ expect(result.attributes["param1"]).to eq(1103)
39
+ expect(result.attributes["param2"]).to eq(1203)
117
40
  end
118
41
  end
119
42
  end
@@ -0,0 +1,150 @@
1
+ describe 'query' do
2
+ class ProductCatalogModel < Dymos::Model
3
+ table 'ProductCatalog'
4
+ field :Id, :integer
5
+ field :Title, :string
6
+ field :ISBN, :string
7
+ field :Price, :integer
8
+ field :Authors, :array
9
+ field :Dimensions, :string
10
+ field :PageCount, :integer
11
+ field :InPublication, :integer
12
+ field :ProductCategory, :string
13
+ end
14
+ class ForumModel < Dymos::Model
15
+ table 'Forum'
16
+ field :Name, :string
17
+ field :Category, :string
18
+ field :Threads, :integer
19
+ field :Messages, :string
20
+ field :Views, :integer
21
+ field :LastPostBy, :string
22
+ field :LastPostDateTime, :time
23
+ end
24
+ class ThreadModel < Dymos::Model
25
+ table 'Thread'
26
+ field :ForumName, :string
27
+ field :Subject, :string
28
+ field :Message, :string
29
+ field :LastPostedBy, :string
30
+ field :Views, :integer
31
+ field :Replies, :integer
32
+ field :Views, :integer
33
+ field :Answered, :string
34
+ field :Tags, :array
35
+ field :LastPostDateTime, :time
36
+ end
37
+ class ReplyModel < Dymos::Model
38
+ table 'Reply'
39
+ field :Id, :string
40
+ field :ReplyDateTime, :time
41
+ field :Message, :string
42
+ field :ReplyDateTime, :string
43
+ end
44
+
45
+ describe :find do
46
+ it 'only' do
47
+ expect(ProductCatalogModel.find(101).Id).to eq(101)
48
+ end
49
+ end
50
+
51
+ describe :all do
52
+ it :only do
53
+ result = ForumModel.all
54
+ expect(result.count).to eq(2)
55
+ end
56
+ it :where do
57
+ result = ThreadModel.where(ForumName: "DynamoDB").all
58
+ expect(result.count).to eq(2)
59
+ end
60
+
61
+ it :add_filter do
62
+ result = ThreadModel.where(ForumName: "DynamoDB").add_filter(:Tags, :contains, 'table').all
63
+ expect(result.count).to eq(1)
64
+ end
65
+
66
+ it :index do
67
+ result = ReplyModel.index(:PostedByIndex).where(Id: "DynamoDB#DynamoDB Thread 1").all
68
+ expect(result.count).to eq(3)
69
+ result = ReplyModel.index(:PostedByIndex).where(Id: "DynamoDB#DynamoDB Thread 1", PostedBy: "User A").all
70
+ expect(result.count).to eq(2)
71
+ result = ReplyModel.index(:PostedByIndex).
72
+ where(Id: "DynamoDB#DynamoDB Thread 1", PostedBy: "User A").all
73
+ expect(result.count).to eq(2)
74
+ end
75
+ end
76
+ it :one do
77
+ result = ThreadModel.where(ForumName: "DynamoDB").desc.one
78
+ expect(result.Subject).to eq("DynamoDB Thread 2")
79
+ result = ThreadModel.where(ForumName: "DynamoDB").asc.one
80
+ expect(result.Subject).to eq("DynamoDB Thread 1")
81
+ end
82
+
83
+ describe :save do
84
+ table='test_model_save_item_table'
85
+
86
+ class TestModelSaveItem < Dymos::Model
87
+ table 'test_model_save_item_table'
88
+ field :id, :string
89
+ field :param1, :integer
90
+ field :param2, :integer
91
+ field :param3, :string
92
+ field :param4, :array
93
+ end
94
+ before :all do
95
+ @client.delete_table(table_name: table) if @client.list_tables[:table_names].include?(table)
96
+ query=Dymos::Query::CreateTable.new.name(table).attributes(id: 'S').keys(id: 'HASH').build
97
+ @client.create_table(query)
98
+ @client.put_item(table_name: table, item: {id: 'hoge', param1: 101, param2: 201, param3: "hoge", param4: [1, 0, 1]})
99
+ @client.put_item(table_name: table, item: {id: 'fuga', param1: 102, param2: 202, param3: "fuga", param4: [1, 0, 2]})
100
+ @client.put_item(table_name: table, item: {id: 'piyo', param1: 103, param2: 203, param3: "piyo", param4: [1, 0, 3]})
101
+ end
102
+
103
+ describe :new do
104
+ it '通常のsave' do
105
+ data={id: 'abc', param1: 104, param2: 204, param3: 'abc', param4: [1, 0, 4]}
106
+ model = TestModelSaveItem.new(data)
107
+ expect(model.save!).to eq(true)
108
+ expect(TestModelSaveItem.find('abc').attributes).to eq(data)
109
+ end
110
+
111
+ it 'itemメソッドからパラメータを追加するとモデルの値は影響を受けない' do
112
+ model = TestModelSaveItem.new
113
+ data={id: 'efg', param1: 105, param2: 205, param3: 'efg', param4: [1, 0, 5]}
114
+ expect(model.item(data).save!).to eq(true)
115
+ expect(model.id).to eq(nil)
116
+ expect(TestModelSaveItem.find('efg').attributes).to eq(data)
117
+ end
118
+ end
119
+
120
+ describe :update do
121
+ it 'addメソッドから値を変更' do
122
+ model = TestModelSaveItem.find('hoge')
123
+ expect(model.param1).to eq(101)
124
+ expect(model.add(:param1, 1).update!).to eq(true)
125
+ expect(model.last_execute_query[:query][:attribute_updates]).to eq({"param1" => {:value => 1, :action => "ADD"}})
126
+ expect(model.param1).to eq(101)
127
+ expect(TestModelSaveItem.find('hoge').param1).to eq(102)
128
+ end
129
+
130
+
131
+ it 'モデルの値の変更されたものだけアップデートする' do
132
+ model = TestModelSaveItem.find('fuga')
133
+ expect(model.param1).to eq(102)
134
+ model.param1+=1
135
+ expect(model.update!).to eq(true)
136
+ expect(model.last_execute_query[:query][:attribute_updates]).to eq({"param1" => {:value => 103, :action => "PUT"}})
137
+ model = TestModelSaveItem.find('fuga')
138
+ expect(model.param1).to eq(103)
139
+ end
140
+ end
141
+ describe :delete do
142
+ it 'del' do
143
+ model = TestModelSaveItem.find('piyo')
144
+ expect(model.add_expected(:param1,:eq,103).delete.class).to eq(TestModelSaveItem)
145
+ expect(TestModelSaveItem.find('piyo')).to eq(nil)
146
+ end
147
+ end
148
+ end
149
+ end
150
+
data/spec/spec_helper.rb CHANGED
@@ -7,3 +7,36 @@ require 'timecop'
7
7
  require 'dymos'
8
8
 
9
9
  I18n.enforce_available_locales = false
10
+
11
+
12
+ RSpec.configure do |config|
13
+ config.before :all do
14
+ Aws.config[:region] = 'us-west-1'
15
+ Aws.config[:endpoint] = 'http://localhost:4567'
16
+ Aws.config[:access_key_id] = 'dymos_test'
17
+ Aws.config[:secret_access_key] = 'dymos_test'
18
+ @client=Aws::DynamoDB::Client.new
19
+
20
+ list_tables = @client.list_tables.table_names
21
+ %w(ProductCatalog Forum Thread Reply).each do |name|
22
+ @client.delete_table(table_name: name) if list_tables.include? name
23
+ end
24
+ @client.create_table(table_name: 'ProductCatalog', attribute_definitions: [{attribute_name: 'Id', attribute_type: 'N'}], key_schema: [{attribute_name: 'Id', key_type: 'HASH'}], provisioned_throughput: {read_capacity_units: 10, write_capacity_units: 5})
25
+ @client.create_table(table_name: 'Forum', attribute_definitions: [{attribute_name: 'Name', attribute_type: 'S'}], key_schema: [{attribute_name: 'Name', key_type: 'HASH'}], provisioned_throughput: {read_capacity_units: 10, write_capacity_units: 5})
26
+ @client.create_table(table_name: 'Thread', attribute_definitions: [{attribute_name: 'ForumName', attribute_type: 'S'}, {attribute_name: 'Subject', attribute_type: 'S'}], key_schema: [{attribute_name: 'ForumName', key_type: 'HASH'}, {attribute_name: 'Subject', key_type: 'RANGE'}], provisioned_throughput: {read_capacity_units: 10, write_capacity_units: 5})
27
+ @client.create_table(table_name: 'Reply', attribute_definitions: [{attribute_name: 'Id', attribute_type: 'S'}, {attribute_name: 'ReplyDateTime', attribute_type: 'S'}, {attribute_name: 'PostedBy', attribute_type: 'S'}], key_schema: [{attribute_name: 'Id', key_type: 'HASH'}, {attribute_name: 'ReplyDateTime', key_type: 'RANGE'}], provisioned_throughput: {read_capacity_units: 10, write_capacity_units: 5}, local_secondary_indexes: [{index_name: 'PostedByIndex', key_schema: [{attribute_name: 'Id', key_type: 'HASH'}, {attribute_name: 'PostedBy', key_type: 'RANGE'}], projection: {projection_type: 'KEYS_ONLY'}}])
28
+ YAML.load_file('spec/lib/dymos/data.yml').each do |table_name, data|
29
+ data.each do |datum|
30
+ @client.put_item(table_name: table_name, item: datum)
31
+ end
32
+ end
33
+
34
+ Dymos::Config.default[:create_table]={
35
+ provisioned_throughput: {
36
+ read_capacity_units: 10,
37
+ write_capacity_units: 5
38
+ }
39
+ }
40
+
41
+ end
42
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dymos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - hoshina85
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-05 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -139,31 +139,33 @@ files:
139
139
  - dymos.gemspec
140
140
  - lib/dymos.rb
141
141
  - lib/dymos/attribute.rb
142
- - lib/dymos/command.rb
142
+ - lib/dymos/client.rb
143
+ - lib/dymos/config.rb
143
144
  - lib/dymos/model.rb
144
145
  - lib/dymos/persistence.rb
145
- - lib/dymos/query/attribute.rb
146
- - lib/dymos/query/builder.rb
146
+ - lib/dymos/query/base.rb
147
+ - lib/dymos/query/create_table.rb
147
148
  - lib/dymos/query/delete_item.rb
148
149
  - lib/dymos/query/describe.rb
149
- - lib/dymos/query/expect.rb
150
150
  - lib/dymos/query/get_item.rb
151
151
  - lib/dymos/query/put_item.rb
152
152
  - lib/dymos/query/query.rb
153
153
  - lib/dymos/query/scan.rb
154
154
  - lib/dymos/query/update_item.rb
155
155
  - lib/dymos/version.rb
156
+ - spec/lib/dymos/client_spec.rb
157
+ - spec/lib/dymos/config_spec.rb
158
+ - spec/lib/dymos/data.yml
156
159
  - spec/lib/dymos/model_spec.rb
157
- - spec/lib/dymos/query/attribute_spec.rb
158
- - spec/lib/dymos/query/builder_spec.rb
160
+ - spec/lib/dymos/query/create_table_spec.rb
159
161
  - spec/lib/dymos/query/delete_item_spec.rb
160
162
  - spec/lib/dymos/query/describe_spec.rb
161
- - spec/lib/dymos/query/expect_spec.rb
162
163
  - spec/lib/dymos/query/get_item_spec.rb
163
164
  - spec/lib/dymos/query/put_item_spec.rb
164
165
  - spec/lib/dymos/query/query_spec.rb
165
166
  - spec/lib/dymos/query/scan_spec.rb
166
167
  - spec/lib/dymos/query/update_item_spec.rb
168
+ - spec/lib/dymos/query_spec.rb
167
169
  - spec/spec_helper.rb
168
170
  homepage: ''
169
171
  licenses:
@@ -190,15 +192,17 @@ signing_key:
190
192
  specification_version: 4
191
193
  summary: aws-sdk-core-ruby dynamodb client wrapper
192
194
  test_files:
195
+ - spec/lib/dymos/client_spec.rb
196
+ - spec/lib/dymos/config_spec.rb
197
+ - spec/lib/dymos/data.yml
193
198
  - spec/lib/dymos/model_spec.rb
194
- - spec/lib/dymos/query/attribute_spec.rb
195
- - spec/lib/dymos/query/builder_spec.rb
199
+ - spec/lib/dymos/query/create_table_spec.rb
196
200
  - spec/lib/dymos/query/delete_item_spec.rb
197
201
  - spec/lib/dymos/query/describe_spec.rb
198
- - spec/lib/dymos/query/expect_spec.rb
199
202
  - spec/lib/dymos/query/get_item_spec.rb
200
203
  - spec/lib/dymos/query/put_item_spec.rb
201
204
  - spec/lib/dymos/query/query_spec.rb
202
205
  - spec/lib/dymos/query/scan_spec.rb
203
206
  - spec/lib/dymos/query/update_item_spec.rb
207
+ - spec/lib/dymos/query_spec.rb
204
208
  - spec/spec_helper.rb
data/lib/dymos/command.rb DELETED
@@ -1,36 +0,0 @@
1
- module Dymos
2
- module Command
3
-
4
- # @return [PutItem]
5
- def put
6
- Dymos::Query::PutItem.new(:put_item, table_name, class_name)
7
- end
8
-
9
- # @return [UpdateItem]
10
- def update
11
- Dymos::Query::UpdateItem.new(:update_item, table_name, class_name)
12
- end
13
-
14
- # @return [GetItem]
15
- def get
16
- Dymos::Query::GetItem.new(:get_item, table_name, class_name)
17
- end
18
-
19
- # @return [Query]
20
- def query
21
- Dymos::Query::Query.new(:query, table_name, class_name)
22
- end
23
-
24
- def describe
25
- Dymos::Query::Describe.new(:describe_table, table_name, class_name)
26
- end
27
-
28
- def scan
29
- Dymos::Query::Scan.new(:scan, table_name, class_name)
30
- end
31
-
32
- def delete
33
- Dymos::Query::DeleteItem.new(:delete_item, table_name, class_name)
34
- end
35
- end
36
- end
@@ -1,14 +0,0 @@
1
- module Dymos
2
- module Query
3
- class Attribute
4
- def initialize(value)
5
- value =value.to_i if /^[0-9]+$/ =~ value
6
- @value = value
7
- end
8
-
9
- def data
10
- @value
11
- end
12
- end
13
- end
14
- end
@@ -1,79 +0,0 @@
1
- module Dymos
2
- module Query
3
- class Builder
4
- attr_accessor :table_name, :command, :query
5
-
6
- def initialize(command, table_name=nil, class_name=nil)
7
- @class_name = class_name
8
- @command = command
9
- @table_name = table_name if table_name.present?
10
- self
11
- end
12
-
13
- def query
14
-
15
- end
16
-
17
- def before_send_query(command, query)
18
-
19
- end
20
-
21
- def after_send_query(command, query)
22
-
23
- end
24
-
25
- def raw_execute(client=nil)
26
- client ||= Aws::DynamoDB::Client.new
27
- begin
28
- before_send_query command, query
29
- res = client.send command, query
30
- after_send_query command, query
31
- rescue Aws::DynamoDB::Errors::ConditionalCheckFailedException
32
- return false
33
- end
34
- res
35
- end
36
-
37
- def execute(client = nil)
38
- res = raw_execute client
39
-
40
- return false unless res
41
-
42
- if @class_name.present?
43
- if res.data.respond_to? :items # scan, query
44
- metadata = extract(res, :items)
45
- res.data[:items].map do |datum|
46
- obj = Object.const_get(@class_name).new(datum)
47
- obj.metadata = metadata
48
- obj.new_record = false
49
- obj
50
- end
51
- elsif res.data.respond_to? :attributes # put_item, update_item
52
- return nil if res.attributes.nil?
53
- obj = Object.const_get(@class_name).new(res.attributes)
54
- obj.metadata = extract(res, :attributes)
55
- obj
56
- elsif res.respond_to? :data
57
- if res.data.respond_to? :item # get_item, delete_item
58
- return nil if res.data.item.nil?
59
- obj = Object.const_get(@class_name).new(res.data.item)
60
- obj.metadata = extract(res, :item)
61
- obj.new_record = false
62
- obj
63
- else
64
- res.data.to_hash # describe
65
- end
66
- end
67
- else
68
- res.data.to_hash #list_tables
69
- end
70
-
71
- end
72
-
73
- def extract(res, ignore_key)
74
- keys = res.data.members.reject { |a| a == ignore_key }
75
- keys.map { |k| [k, res.data[k]] }.to_h
76
- end
77
- end
78
- end
79
- end
@@ -1,65 +0,0 @@
1
- module Dymos
2
- module Query
3
- class Expect < Attribute
4
- def initialize
5
- end
6
-
7
- def condition(operator, value)
8
- if value.present?
9
- value1, value2 = value.split(" ")
10
- value1 =value1.to_i if /^[0-9]+$/.match(value1)
11
- value2 =value2.to_i if /^[0-9]+$/.match(value2)
12
- @value = value1
13
- end
14
-
15
- case operator.to_sym
16
- when :==, :eq
17
- @operator = 'EQ'
18
- when :!=, :nq
19
- @operator = 'NE'
20
- when :<=, :le
21
- @operator = 'LE'
22
- when :<, :lt
23
- @operator = 'LT'
24
- when :>=, :ge
25
- @operator = 'GE'
26
- when :>, :gt
27
- @operator = 'GT'
28
- when :between
29
- @operator = 'BETWEEN'
30
- @value =[value1, value2]
31
- when :is_null
32
- @operator = 'NULL'
33
- when :is_not_null
34
- @operator = 'NOT_NULL'
35
- when :contains
36
- @operator = 'CONTAINS'
37
- when :not_contains
38
- @operator = 'NOT_CONTAINS'
39
- when :begins_with
40
- @operator = 'BEGINS_WITH'
41
- else
42
- raise ArgumentError, '%s is not defined ' % operator
43
- end
44
- self
45
- end
46
-
47
- def data(is_force_array=false)
48
- value = super()
49
- if is_force_array or value.is_a? Array
50
- data = {
51
- attribute_value_list: (is_force_array and !(value.is_a? Array)) ? [value] : value,
52
- comparison_operator: @operator
53
- }
54
- else
55
- data = {}
56
- data[:value]= value if value.present?
57
- data[:comparison_operator]= @operator
58
- end
59
-
60
- data[:exists] = true if @exists.present?
61
- data
62
- end
63
- end
64
- end
65
- end
@@ -1,15 +0,0 @@
1
- describe Dymos::Query::Attribute do
2
- describe :new do
3
- describe "DynamoDBClientで取り扱う値の形式に変換" do
4
- it "文字列" do
5
- attribute = Dymos::Query::Attribute.new('10')
6
- expect(attribute.data).to eq(10)
7
- end
8
- it "数値に変換可能な文字列はintになる" do
9
- attribute = Dymos::Query::Attribute.new('10')
10
- expect(attribute.data).to eq(10)
11
- end
12
- end
13
- end
14
- end
15
-
@@ -1,23 +0,0 @@
1
- describe Dymos::Query::Builder do
2
- before :all do
3
- Aws.config[:region] = 'us-west-1'
4
- Aws.config[:endpoint] = 'http://localhost:4567'
5
- Aws.config[:access_key_id] = 'XXX'
6
- Aws.config[:secret_access_key] = 'XXX'
7
- end
8
-
9
- describe :list_tables do
10
- let(:query) { Dymos::Query::Builder.new(:list_tables) }
11
-
12
- it "command" do
13
- expect(query.command).to eq(:list_tables)
14
- end
15
-
16
- it "execute" do
17
- result = query.execute
18
- expect(result.include? :table_names).to eq(true)
19
- end
20
- end
21
-
22
- end
23
-
@@ -1,5 +0,0 @@
1
- describe Dymos::Query::Expect do
2
- describe :new do
3
- end
4
- end
5
-