tokyo_wrapper 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ module TokyoWrapper
2
+
3
+ module HelperMethods
4
+
5
+ module ArrayConverter
6
+
7
+ #
8
+ # rufus-tokyo converts array to string without a delimiter e.g. It converts [1,2,3,4] to "1234".
9
+ # So the array needs to be converted to the comma separated string before being added. e.g. "1,2,3,4"
10
+ #
11
+ def convert_params(params)
12
+ params.each do |key, value|
13
+ if value.instance_of?(Array)
14
+ params[key] = value.join(",")
15
+ end
16
+ end
17
+ params
18
+ end
19
+
20
+ def convert_comma_separated_values_string_to_array(comman_separated_values_string)
21
+ comman_separated_values_string.split(',').compact
22
+ end
23
+
24
+ def convert_values_to_array_for_keys(key_value_hash, keys)
25
+ if !keys.nil? && !keys.empty?
26
+ keys.each do |key|
27
+ if key_value_hash.has_key?(key)
28
+ key_value_hash[key] = convert_comma_separated_values_string_to_array(key_value_hash[key])
29
+ end
30
+ end
31
+ end
32
+ key_value_hash
33
+ end
34
+
35
+ def convert_values_to_array_for_keys_for_multiple_key_value_hashes(key_value_hashes, keys)
36
+ key_value_hashes.each do |key_value_hash|
37
+ convert_values_to_array_for_keys(key_value_hash, keys)
38
+ end
39
+ key_value_hashes
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+
46
+ end
@@ -1,9 +1,11 @@
1
+ require 'tokyo_wrapper/helper_methods/array_converter'
1
2
  require 'tokyo_wrapper/table_methods/associations'
2
3
  require 'tokyo_wrapper/table_methods/query'
3
4
 
4
5
  module TokyoWrapper
5
6
 
6
7
  class Table
8
+ include TokyoWrapper::HelperMethods::ArrayConverter
7
9
  include TokyoWrapper::TableMethods::Associations
8
10
  include TokyoWrapper::TableMethods::Query
9
11
 
@@ -49,36 +51,26 @@ module TokyoWrapper
49
51
  @table.delete(id.to_s)
50
52
  end
51
53
 
52
- def all
53
- @table.query
54
- end
55
-
54
+ def all(options = {})
55
+ result = @table.query
56
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
57
+ end
58
+
56
59
  def find(id, options = {})
57
60
  if !options.empty? && options[:pk_included] == true
58
- @table[id.to_s].merge({:pk => id.to_s})
61
+ result = @table[id.to_s].merge({:pk => id.to_s})
59
62
  else
60
- @table[id.to_s]
63
+ result = @table[id.to_s]
61
64
  end
65
+ convert_values_to_array_for_keys(result, options[:keys_for_has_many_association])
62
66
  end
63
67
 
64
- def all_by_key_value(key, value)
65
- @table.query do |query|
68
+ def all_by_key_value(key, value, options = {})
69
+ result = @table.query do |query|
66
70
  query.add key, :equals, value
67
71
  end
72
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
68
73
  end
69
-
70
- private
71
- #
72
- # rufus-tokyo converts array to string without a delimiter e.g. It converts [1,2,3,4] to "1234".
73
- # So the array needs to be converted to the comma separated string before being added. e.g. "1,2,3,4"
74
- #
75
- def convert_params(params)
76
- params.each do |key, value|
77
- if value.instance_of?(Array)
78
- params[key] = value.join(",")
79
- end
80
- end
81
- end
82
74
 
83
75
  end
84
76
 
@@ -18,10 +18,11 @@ module TokyoWrapper
18
18
  end
19
19
  end
20
20
 
21
- def all_by_has_many_association_id(association_id_name, association_id)
22
- @table.query do | query |
21
+ def all_by_has_many_association_id(association_id_name, association_id, options = {})
22
+ result = @table.query do | query |
23
23
  query.add "#{association_id_name}s", :stror, association_id.to_s
24
24
  end
25
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
25
26
  end
26
27
 
27
28
  def set_belongs_to_association_id(id, association_id_name, association_id)
@@ -4,12 +4,13 @@ module TokyoWrapper
4
4
 
5
5
  module Query
6
6
 
7
- def all_by_multiple_key_values(key_value_hash = {})
8
- @table.query do |query|
7
+ def all_by_multiple_key_values(key_value_hash = {}, options = {})
8
+ result = @table.query do |query|
9
9
  key_value_hash.each do |key, value|
10
10
  query.add key, :equals, value
11
11
  end
12
12
  end
13
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
13
14
  end
14
15
 
15
16
  end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
+ require 'tokyo_wrapper/helper_methods/array_converter'
3
+
4
+ describe TokyoWrapper::HelperMethods::ArrayConverter do
5
+
6
+ class ArrayConverterIncluder
7
+ include TokyoWrapper::HelperMethods::ArrayConverter
8
+ end
9
+
10
+ before(:each) do
11
+ @array_converter_includer = ArrayConverterIncluder.new
12
+ end
13
+
14
+ after(:each) do
15
+ end
16
+
17
+ context "Convert between array and string" do
18
+
19
+ it "should convert array to string with comma separated elements" do
20
+
21
+ params_1 = { "name" => "Temp name", "sector_ids" => [2,5,34,8] }
22
+
23
+ result_1 = @array_converter_includer.convert_params(params_1)
24
+
25
+ result_1.should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
26
+ params_1.should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
27
+
28
+ params_2 = { "name" => "Temp name", "sector_ids" => ["2","5","34","8"] }
29
+
30
+ result_2 = @array_converter_includer.convert_params(params_2)
31
+
32
+ result_2.should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
33
+ params_2.should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
34
+
35
+ end
36
+
37
+ it "should convert string with comma separated elements to array" do
38
+
39
+ @array_converter_includer.convert_comma_separated_values_string_to_array("2,5,32,8").should == ["2","5","32","8"]
40
+ @array_converter_includer.convert_comma_separated_values_string_to_array("2,5,,8").should == ["2","5","","8"]
41
+
42
+ end
43
+
44
+ it "should convert string with comma separated elements to array for the given key" do
45
+
46
+ key_value_hash_1 = {"name" => "Temp name", "sector_ids" => "2,5,34,8", "some_ids" => "8,32,9"}
47
+ keys_1 = ["sector_ids", "some_ids"]
48
+
49
+ result_1 = @array_converter_includer.convert_values_to_array_for_keys(key_value_hash_1, keys_1)
50
+
51
+ result_1.should == {"name" => "Temp name", "sector_ids" => ["2","5","34","8"], "some_ids" => ["8","32","9"]}
52
+ key_value_hash_1.should == {"name" => "Temp name", "sector_ids" => ["2","5","34","8"], "some_ids" => ["8","32","9"]}
53
+
54
+ key_value_hash_2 = {"name" => "Temp name", "sector_ids" => "2,5,34,8", "some_ids" => "8,32,9"}
55
+
56
+ result_2 = @array_converter_includer.convert_values_to_array_for_keys(key_value_hash_2, nil)
57
+
58
+ result_2.should == {"name" => "Temp name", "sector_ids" => "2,5,34,8", "some_ids" => "8,32,9"}
59
+ key_value_hash_2.should == {"name" => "Temp name", "sector_ids" => "2,5,34,8", "some_ids" => "8,32,9"}
60
+
61
+ end
62
+
63
+ it "should convert string with comma separated elements to array for the given key for multiple key value hashes" do
64
+
65
+ key_value_hashes_1 = [{"name" => "Temp name 1", "sector_ids" => "2,5,34,8", "some_ids" => "8,32,9"},
66
+ {"name" => "Temp name 1", "sector_ids" => "5,43,34,6"}]
67
+ keys_1 = ["sector_ids", "some_ids"]
68
+
69
+ result_1 = @array_converter_includer.convert_values_to_array_for_keys_for_multiple_key_value_hashes(key_value_hashes_1, keys_1)
70
+
71
+ result_1.should == [{"name" => "Temp name 1", "sector_ids" => ["2","5","34","8"], "some_ids" => ["8","32","9"]},
72
+ {"name" => "Temp name 1", "sector_ids" => ["5","43","34","6"]}]
73
+ key_value_hashes_1.should == [{"name" => "Temp name 1", "sector_ids" => ["2","5","34","8"], "some_ids" => ["8","32","9"]},
74
+ {"name" => "Temp name 1", "sector_ids" => ["5","43","34","6"]}]
75
+
76
+ end
77
+
78
+ end
79
+
80
+ end
@@ -14,22 +14,6 @@ describe TokyoWrapper::TableMethods::Associations do
14
14
 
15
15
  context "has_many associations" do
16
16
 
17
- it "should convert array parameter value to joined comma-separated string" do
18
-
19
- write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
20
-
21
- params_1 = { "name" => "Temp name", "sector_ids" => [2,5,34,8] }
22
-
23
- write_table.send(:convert_params, params_1).should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
24
-
25
- params_2 = { "name" => "Temp name", "sector_ids" => ["2","5","34","8"] }
26
-
27
- write_table.send(:convert_params, params_2).should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
28
-
29
- write_table.close
30
-
31
- end
32
-
33
17
  it "should add data with has_many association" do
34
18
 
35
19
  begin
@@ -52,11 +36,22 @@ describe TokyoWrapper::TableMethods::Associations do
52
36
  "city" => "Montreal",
53
37
  "notes" => "Some notes",
54
38
  "sector_ids" => "2,5,34,8"}]
55
-
39
+
40
+ read_table.all(:keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id.to_s,
41
+ "street" => "1111 Main",
42
+ "city" => "Montreal",
43
+ "notes" => "Some notes",
44
+ "sector_ids" => ["2","5","34","8"]}]
45
+
56
46
  read_table.find(id).should == {"street" => "1111 Main",
57
47
  "city" => "Montreal",
58
48
  "notes" => "Some notes",
59
49
  "sector_ids" => "2,5,34,8"}
50
+
51
+ read_table.find(id, :keys_for_has_many_association => ["sector_ids"]).should == {"street" => "1111 Main",
52
+ "city" => "Montreal",
53
+ "notes" => "Some notes",
54
+ "sector_ids" => ["2","5","34","8"]}
60
55
  ensure
61
56
  read_table.close unless read_table.nil?
62
57
  end
@@ -290,16 +285,44 @@ describe TokyoWrapper::TableMethods::Associations do
290
285
  "city" => "Montreal",
291
286
  "notes" => "Another notes",
292
287
  "sector_ids" => "1,2,3458,9"}]
288
+
289
+ read_table.all_by_has_many_association_id("sector_id", "2",
290
+ :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_1.to_s,
291
+ "street" => "1111 Main",
292
+ "city" => "Montreal",
293
+ "notes" => "Some notes",
294
+ "sector_ids" => ["2","5","32","8"]},
295
+ {:pk => id_2.to_s,
296
+ "street" => "1111 Maisonneuve",
297
+ "city" => "Montreal",
298
+ "notes" => "Another notes",
299
+ "sector_ids" => ["1","2","3458","9"]}]
300
+
293
301
  read_table.all_by_has_many_association_id("sector_id", "45").should == [{:pk => id_3.to_s,
294
302
  "street" => "1111 Desjardins",
295
303
  "city" => "Quebec",
296
304
  "notes" => "Different notes",
297
305
  "sector_ids" => "87,45,1,727"}]
306
+
307
+ read_table.all_by_has_many_association_id("sector_id", "45",
308
+ :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_3.to_s,
309
+ "street" => "1111 Desjardins",
310
+ "city" => "Quebec",
311
+ "notes" => "Different notes",
312
+ "sector_ids" => ["87","45","1","727"]}]
313
+
298
314
  read_table.all_by_has_many_association_id("sector_id", "3458").should == [{:pk => id_2.to_s,
299
315
  "street" => "1111 Maisonneuve",
300
316
  "city" => "Montreal",
301
317
  "notes" => "Another notes",
302
- "sector_ids" => "1,2,3458,9"}]
318
+ "sector_ids" => "1,2,3458,9"}]
319
+
320
+ read_table.all_by_has_many_association_id("sector_id", "3458",
321
+ :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_2.to_s,
322
+ "street" => "1111 Maisonneuve",
323
+ "city" => "Montreal",
324
+ "notes" => "Another notes",
325
+ "sector_ids" => ["1","2","3458","9"]}]
303
326
  ensure
304
327
  read_table.close unless read_table.nil?
305
328
  end
@@ -24,28 +24,32 @@ describe TokyoWrapper::TableMethods::Associations do
24
24
  "province" => "Quebec",
25
25
  "country" => "Canada",
26
26
  "notes" => "Some notes",
27
- "register_id" => "45"}
27
+ "register_id" => "45",
28
+ "sector_ids" => ["2","5","32","8"]}
28
29
  id_1 = write_table.add(data_hash_1)
29
30
  data_hash_2 = {"street" => "1111 Maisonneuve",
30
31
  "city" => "Montreal",
31
32
  "province" => "Quebec",
32
33
  "country" => "Canada",
33
34
  "notes" => "Another notes",
34
- "register_id" => "8"}
35
+ "register_id" => "8",
36
+ "sector_ids" => ["1","2","3458","9"]}
35
37
  id_2 = write_table.add(data_hash_2)
36
38
  data_hash_3 = {"street" => "1111 Main",
37
39
  "city" => "Quebec",
38
40
  "province" => "Quebec",
39
41
  "country" => "Canada",
40
42
  "notes" => "Different notes",
41
- "register_id" => "8"}
43
+ "register_id" => "8",
44
+ "sector_ids" => ["87","45","1","727"]}
42
45
  id_3 = write_table.add(data_hash_3)
43
46
  data_hash_4 = {"street" => "1112 Main",
44
47
  "city" => "Montreal",
45
48
  "province" => "Quebec",
46
49
  "country" => "Canada",
47
50
  "notes" => "One more note",
48
- "register_id" => "45"}
51
+ "register_id" => "45",
52
+ "sector_ids" => ["87","45","1","727"]}
49
53
  id_4 = write_table.add(data_hash_4)
50
54
 
51
55
  ensure
@@ -62,14 +66,35 @@ describe TokyoWrapper::TableMethods::Associations do
62
66
  "province" => "Quebec",
63
67
  "country" => "Canada",
64
68
  "notes" => "Some notes",
65
- "register_id" => "45"},
69
+ "register_id" => "45",
70
+ "sector_ids" => "2,5,32,8"},
66
71
  {:pk => id_4.to_s,
67
72
  "street" => "1112 Main",
68
73
  "city" => "Montreal",
69
74
  "province" => "Quebec",
70
75
  "country" => "Canada",
71
76
  "notes" => "One more note",
72
- "register_id" => "45"}]
77
+ "register_id" => "45",
78
+ "sector_ids" => "87,45,1,727"}]
79
+
80
+ read_table.all_by_multiple_key_values({"city" => "Montreal",
81
+ "register_id" => "45"},
82
+ {:keys_for_has_many_association => ["sector_ids"]}).should == [{:pk => id_1.to_s,
83
+ "street" => "1111 Main",
84
+ "city" => "Montreal",
85
+ "province" => "Quebec",
86
+ "country" => "Canada",
87
+ "notes" => "Some notes",
88
+ "register_id" => "45",
89
+ "sector_ids" => ["2","5","32","8"]},
90
+ {:pk => id_4.to_s,
91
+ "street" => "1112 Main",
92
+ "city" => "Montreal",
93
+ "province" => "Quebec",
94
+ "country" => "Canada",
95
+ "notes" => "One more note",
96
+ "register_id" => "45",
97
+ "sector_ids" => ["87","45","1","727"]}]
73
98
  ensure
74
99
  read_table.close unless read_table.nil?
75
100
  end
@@ -171,10 +171,100 @@ describe TokyoWrapper::Table do
171
171
 
172
172
  end
173
173
 
174
+ it "should find a row with a given id with has_many association ids converted to array" do
175
+
176
+ begin
177
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
178
+
179
+ data_hash = {"street" => "1111 Main",
180
+ "city" => "Montreal",
181
+ "notes" => "Some notes",
182
+ "sector_ids" => "2,5,34,8"}
183
+ id = write_table.add(data_hash)
184
+ ensure
185
+ write_table.close unless write_table.nil?
186
+ end
187
+
188
+ begin
189
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
190
+
191
+ read_table.find(id, :pk_included => true, :keys_for_has_many_association => ["sector_ids"]).should == {:pk => id.to_s,
192
+ "street" => "1111 Main",
193
+ "city" => "Montreal",
194
+ "notes" => "Some notes",
195
+ "sector_ids" => ["2","5","34","8"]}
196
+
197
+ ensure
198
+ read_table.close unless read_table.nil?
199
+ end
200
+
201
+ end
202
+
174
203
  end
175
204
 
176
205
  context "Find all" do
177
206
 
207
+ it "should find all the rows with has_many association ids converted to array" do
208
+
209
+ begin
210
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
211
+
212
+ data_hash_1 = {"street" => "1111 Main",
213
+ "city" => "Montreal",
214
+ "province" => "Quebec",
215
+ "country" => "Canada",
216
+ "notes" => "Some notes",
217
+ "sector_ids" => "2,5,34,8"}
218
+ id_1 = write_table.add(data_hash_1)
219
+ data_hash_2 = {"street" => "1111 Maisonneuve",
220
+ "city" => "Montreal",
221
+ "province" => "Quebec",
222
+ "country" => "Canada",
223
+ "notes" => "Another notes",
224
+ "sector_ids" => "3,5,6,8"}
225
+ id_2 = write_table.add(data_hash_2)
226
+ data_hash_3 = {"street" => "1111 Main",
227
+ "city" => "Quebec",
228
+ "province" => "Quebec",
229
+ "country" => "Canada",
230
+ "notes" => "Different notes",
231
+ "sector_ids" => "2,1,34,7"}
232
+ id_3 = write_table.add(data_hash_3)
233
+
234
+ ensure
235
+ write_table.close unless write_table.nil?
236
+ end
237
+
238
+ begin
239
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
240
+
241
+ read_table.all(:keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_1.to_s,
242
+ "street" => "1111 Main",
243
+ "city" => "Montreal",
244
+ "province" => "Quebec",
245
+ "country" => "Canada",
246
+ "notes" => "Some notes",
247
+ "sector_ids" => ["2","5","34","8"]},
248
+ {:pk => id_2.to_s,
249
+ "street" => "1111 Maisonneuve",
250
+ "city" => "Montreal",
251
+ "province" => "Quebec",
252
+ "country" => "Canada",
253
+ "notes" => "Another notes",
254
+ "sector_ids" => ["3","5","6","8"]},
255
+ {:pk => id_3.to_s,
256
+ "street" => "1111 Main",
257
+ "city" => "Quebec",
258
+ "province" => "Quebec",
259
+ "country" => "Canada",
260
+ "notes" => "Different notes",
261
+ "sector_ids" => ["2","1","34","7"]}]
262
+ ensure
263
+ read_table.close unless read_table.nil?
264
+ end
265
+
266
+ end
267
+
178
268
  it "should find all the rows that have a given value" do
179
269
 
180
270
  begin
@@ -224,6 +314,61 @@ describe TokyoWrapper::Table do
224
314
 
225
315
  end
226
316
 
317
+ it "should find all the rows that have a given value with has_many association ids converted to array" do
318
+
319
+ begin
320
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
321
+
322
+ data_hash_1 = {"street" => "1111 Main",
323
+ "city" => "Montreal",
324
+ "province" => "Quebec",
325
+ "country" => "Canada",
326
+ "notes" => "Some notes",
327
+ "sector_ids" => "2,5,34,8"}
328
+ id_1 = write_table.add(data_hash_1)
329
+ data_hash_2 = {"street" => "1111 Maisonneuve",
330
+ "city" => "Montreal",
331
+ "province" => "Quebec",
332
+ "country" => "Canada",
333
+ "notes" => "Another notes",
334
+ "sector_ids" => "3,5,6,8"}
335
+ id_2 = write_table.add(data_hash_2)
336
+ data_hash_3 = {"street" => "1111 Main",
337
+ "city" => "Quebec",
338
+ "province" => "Quebec",
339
+ "country" => "Canada",
340
+ "notes" => "Different notes",
341
+ "sector_ids" => "2,1,34,7"}
342
+ id_3 = write_table.add(data_hash_3)
343
+
344
+ ensure
345
+ write_table.close unless write_table.nil?
346
+ end
347
+
348
+ begin
349
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
350
+
351
+ read_table.all_by_key_value("city", "Montreal",
352
+ :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_1.to_s,
353
+ "street" => "1111 Main",
354
+ "city" => "Montreal",
355
+ "province" => "Quebec",
356
+ "country" => "Canada",
357
+ "notes" => "Some notes",
358
+ "sector_ids" => ["2","5","34","8"]},
359
+ {:pk => id_2.to_s,
360
+ "street" => "1111 Maisonneuve",
361
+ "city" => "Montreal",
362
+ "province" => "Quebec",
363
+ "country" => "Canada",
364
+ "notes" => "Another notes",
365
+ "sector_ids" => ["3","5","6","8"]}]
366
+ ensure
367
+ read_table.close unless read_table.nil?
368
+ end
369
+
370
+ end
371
+
227
372
  end
228
373
 
229
374
  context "store file content" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tokyo_wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadatoshi Takahashi
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - lib/tokyo_wrapper.rb
25
25
  files:
26
26
  - init.rb
27
+ - lib/tokyo_wrapper/helper_methods/array_converter.rb
27
28
  - lib/tokyo_wrapper/table.rb
28
29
  - lib/tokyo_wrapper/table_methods/associations.rb
29
30
  - lib/tokyo_wrapper/table_methods/query.rb
@@ -35,6 +36,7 @@ files:
35
36
  - spec/macros/rufus_tokyo_macros.rb
36
37
  - spec/spec_helper.rb
37
38
  - spec/tokyo_cabinet_files/table.tct
39
+ - spec/tokyo_wrapper/helper_methods/array_converter_spec.rb
38
40
  - spec/tokyo_wrapper/table_methods/associations_spec.rb
39
41
  - spec/tokyo_wrapper/table_methods/query_spec.rb
40
42
  - spec/tokyo_wrapper/table_spec.rb