tokyo_wrapper 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -43,6 +43,416 @@
43
43
 
44
44
  sudo gem install tokyo_wrapper --source http://gemcutter.org
45
45
 
46
+ == Usage
47
+
48
+ 1. Requiring 'tokyo_wrapper' in Ruby code.
49
+
50
+ require 'tokyo_wrapper'
51
+
52
+ 2. Tokyo Cabinet table.
53
+
54
+ 2-1. Getting table object.
55
+
56
+ Tokyo Cabinet table stores data in a file with extension .tct e.g. table.tct
57
+
58
+ TokyoWrapper::Table class has three factory methods to create an instance of itself:
59
+
60
+ create_with_create_write_non_blocking_lock(file)
61
+ Use when table file doesn't exist. This creates the table file with the given name and
62
+ opens it for writing data.
63
+ It locks the table.
64
+ Only non_locking table (instantiated by create_with_read_non_locking)
65
+ in a different thread can access the table when the instance from this method is in use
66
+ (i.e. when it's not closed yet).
67
+
68
+ create_with_write_non_blocking_lock(file)
69
+ Use when table file already exists and you want to write data in it.
70
+ It locks the table.
71
+ Only non_locking table (instantiated by create_with_read_non_locking)
72
+ in a different thread can access the table when the instance from this method is in use
73
+ (i.e. when it's not closed yet).
74
+
75
+ create_with_read_non_locking(file)
76
+ Use when table file already exists and you want to read data from it.
77
+ It doesn't lock the table.
78
+
79
+ 2-2. Table object must be closed when all the operations are done by calling close method.
80
+
81
+ e.g.
82
+
83
+ begin
84
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
85
+
86
+ ensure
87
+ write_table.close unless write_table.nil?
88
+ end
89
+
90
+ 2-3. Basic operations. (Defined in TokyoWrapper::Table class itself.)
91
+
92
+ 2-3-1. Adding data.
93
+
94
+ Call add method on table object with write mode
95
+ (instantiated by create_with_create_write_non_blocking_lock or
96
+ create_with_write_non_blocking_lock).
97
+ It expects a hash, which corresponds to one record (row) in a table.
98
+ It returns the id (pk in rufus/tokyo term) with type integer.
99
+
100
+ e.g.
101
+
102
+ begin
103
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
104
+
105
+ data_hash = {"street" => "1111 Main",
106
+ "city" => "Montreal",
107
+ "notes" => "Some notes"}
108
+ id = write_table.add(data_hash)
109
+ ensure
110
+ write_table.close unless write_table.nil?
111
+ end
112
+
113
+ 2-3-2. Updating data.
114
+
115
+ Call update method on table object with write mode
116
+ (instantiated by create_with_create_write_non_blocking_lock or
117
+ create_with_write_non_blocking_lock).
118
+ It expects the id of the table record and a hash of updating data.
119
+
120
+ e.g.
121
+
122
+ begin
123
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
124
+
125
+ id = "3"
126
+ update_data_hash = {"street" => "1112 Main",
127
+ "notes" => "Recently situation has been changed."}
128
+ write_table.update(id, update_data_hash)
129
+ ensure
130
+ write_table.close unless write_table.nil?
131
+ end
132
+
133
+ 2-3-3. Deleting data.
134
+
135
+ Call delete method on table object with write mode
136
+ (instantiated by create_with_create_write_non_blocking_lock or
137
+ create_with_write_non_blocking_lock).
138
+ It expects the id of the table record.
139
+
140
+ e.g.
141
+
142
+ begin
143
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
144
+
145
+ id = "3"
146
+ write_table.delete(id)
147
+ ensure
148
+ write_table.close unless write_table.nil?
149
+ end
150
+
151
+ 2-3-4. Getting all the records (rows).
152
+
153
+ Call all method on table object with read mode (instantiated by create_with_read_non_locking).
154
+ It returns an array of hashes. (The same object returned by rufus/tokyo.)
155
+
156
+ e.g.
157
+
158
+ begin
159
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
160
+
161
+ records = read_table.all
162
+ ensure
163
+ read_table.close unless read_table.nil?
164
+ end
165
+
166
+ -> records: [{:pk => 1, "street" => "1111 Main", "city" => "Montreal", "notes" => "Some notes"},
167
+ {:pk => 2, "street" => "2222 Main", "city" => "Quebec", "notes" => "Different notes"}]
168
+
169
+ 2-3-5. Finding a record (rows).
170
+
171
+ Call find method on table object with read mode (instantiated by create_with_read_non_locking).
172
+ It expects the id of the table record.
173
+ If :pk_included => true is specified as the last argument (options),
174
+ returned hash contains :pk (id).
175
+ (Note: This method is the only one that requires this option.
176
+ All the other query methods includes :pk by default just like rufus/tokyo.)
177
+ It returns a hash.
178
+
179
+ e.g.1.
180
+
181
+ begin
182
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
183
+
184
+ id = "3"
185
+ record = read_table.find(id)
186
+ ensure
187
+ read_table.close unless read_table.nil?
188
+ end
189
+
190
+ -> record: {"street" => "1111 Main", "city" => "Montreal", "notes" => "Some notes"}
191
+
192
+ e.g.2.
193
+
194
+ begin
195
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
196
+
197
+ id = "3"
198
+ record = read_table.find(id, :pk_included => true)
199
+ ensure
200
+ read_table.close unless read_table.nil?
201
+ end
202
+
203
+ -> record: {:pk => 1, "street" => "1111 Main", "city" => "Montreal", "notes" => "Some notes"}
204
+
205
+ 2-4. Query operations.
206
+ (Defined in TokyoWrapper::TableMethods::Query module and included in TokyoWrapper::Table class.)
207
+
208
+ 2-4-1. Getting all the records (rows) with the given key-value.
209
+
210
+ Call all_by_key_value method on table object with read mode
211
+ (instantiated by create_with_read_non_locking).
212
+ It expects key and value.
213
+ It returns an array of hashes. (The same object returned by rufus/tokyo.)
214
+
215
+ e.g.
216
+
217
+ begin
218
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
219
+
220
+ records = read_table.all_by_key_value("city", "Montreal")
221
+ ensure
222
+ read_table.close unless read_table.nil?
223
+ end
224
+
225
+ -> records: [{:pk => 1, "street" => "1111 Main", "city" => "Montreal", "notes" => "Some notes"}]
226
+
227
+ 2-4-2. Getting all the records (rows) with the given multiple key-values.
228
+
229
+ Call all_by_multiple_key_values method on table object with read mode
230
+ (instantiated by create_with_read_non_locking).
231
+ It expects a hash of key values.
232
+ It returns an array of hashes. (The same object returned by rufus/tokyo.)
233
+
234
+ e.g.
235
+
236
+ begin
237
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
238
+
239
+ records = read_table.all_by_multiple_key_values({"city" => "Montreal",
240
+ "register_id" => "45"})
241
+ ensure
242
+ read_table.close unless read_table.nil?
243
+ end
244
+
245
+ -> records: [{:pk => 1, "street" => "1111 Main", "city" => "Montreal",
246
+ "notes" => "Some notes", "register_id" => "45"}]
247
+
248
+ 2-5. Associations.
249
+ (Defined in TokyoWrapper::TableMethods::Associations module
250
+ and included in TokyoWrapper::Table class.)
251
+
252
+ It is for achieving one-to-many and many-to-many associations.
253
+
254
+ 2-5-1. Adding data with has_many association ids.
255
+ (A record of the table is associated with multiple records of another table.)
256
+
257
+ Call add method on table object with write mode
258
+ (instantiated by create_with_create_write_non_blocking_lock or
259
+ create_with_write_non_blocking_lock).
260
+ It expects a hash with has_many association ids to be array.
261
+ It returns the id (pk in rufus/tokyo term) with type integer.
262
+
263
+ e.g.
264
+
265
+ begin
266
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
267
+
268
+ data_hash = {"street" => "1111 Main",
269
+ "city" => "Montreal",
270
+ "notes" => "Some notes",
271
+ "sector_ids" => ["2","5","34","8"]}
272
+ id = write_table.add(data_hash)
273
+ ensure
274
+ write_table.close unless write_table.nil?
275
+ end
276
+
277
+ 2-5-2. Updating data with has_many association ids.
278
+ (A record of the table is associated with multiple records of another table.)
279
+
280
+ Call update method on table object with write mode
281
+ (instantiated by create_with_create_write_non_blocking_lock or
282
+ create_with_write_non_blocking_lock).
283
+ It expects the id of the table record and a hash of updating data
284
+ with has_many association ids to be array.
285
+
286
+ e.g.
287
+
288
+ begin
289
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
290
+
291
+ id = "3"
292
+ update_data_hash = {"street" => "1112 Main",
293
+ "sector_ids" => ["2","5","40","8","12"]}
294
+ write_table.update(id, update_data_hash)
295
+ ensure
296
+ write_table.close unless write_table.nil?
297
+ end
298
+
299
+ 2-5-3. Adding has_many association id.
300
+ (A record of the table is associated with multiple records of another table.)
301
+
302
+ Call add_has_many_association_id method on table object with write mode
303
+ (instantiated by create_with_create_write_non_blocking_lock or
304
+ create_with_write_non_blocking_lock).
305
+ It expects the id of the table record, association id name and association id.
306
+
307
+ e.g.1.
308
+
309
+ begin
310
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
311
+
312
+ id = "3"
313
+ write_table.add_has_many_association_id(id, "sector_id", 78)
314
+ ensure
315
+ write_table.close unless write_table.nil?
316
+ end
317
+
318
+ e.g.2.
319
+
320
+ begin
321
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
322
+
323
+ id = "3"
324
+ write_table.add_has_many_association_id(id, "sector_id", "78")
325
+ ensure
326
+ write_table.close unless write_table.nil?
327
+ end
328
+
329
+ 2-5-4. Getting all the records (rows) with the given has_many association id.
330
+ (A record of the table is associated with multiple records of another table.)
331
+
332
+ Call all_by_has_many_association_id method on table object with read mode
333
+ (instantiated by create_with_read_non_locking).
334
+ It expects association id name and association id.
335
+ It returns an array of hashes.
336
+ Note: If there is another has_many association id, it must to indicated by
337
+ :keys_for_has_many_association in order to convert its value to array.
338
+ (See section 2-5-6. for the reason due to rufus/tokyo.)
339
+
340
+ e.g.1.
341
+
342
+ begin
343
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
344
+
345
+ records = read_table.all_by_has_many_association_id("sector_id", "2")
346
+ ensure
347
+ read_table.close unless read_table.nil?
348
+ end
349
+
350
+ -> records: [{:pk => 1, "street" => "1111 Main", "sector_ids" => ["2","5","32","8"]},
351
+ {:pk => 2, "street" => "1122 Main", "sector_ids" => ["1","2","3458","9"]}]
352
+
353
+ e.g.2.1. Other has_many association id value is not converted to array by default.
354
+
355
+ begin
356
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
357
+
358
+ records = read_table.all_by_has_many_association_id("sector_id", "2")
359
+ ensure
360
+ read_table.close unless read_table.nil?
361
+ end
362
+
363
+ -> records: [{:pk => 1, "street" => "1111 Main",
364
+ "sector_ids" => ["2","5","32","8"], "department_ids" => "4,43"},
365
+ {:pk => 2, "street" => "1122 Main",
366
+ "sector_ids" => ["1","2","3458","9"], "department_ids" => "8,5"}]
367
+
368
+ e.g.2.2. Other has_many association id value is converted to array
369
+ by specifying :keys_for_has_many_association option.
370
+
371
+ begin
372
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
373
+
374
+ records = read_table.all_by_has_many_association_id("sector_id", "2",
375
+ :keys_for_has_many_association => ["department_ids"])
376
+ ensure
377
+ read_table.close unless read_table.nil?
378
+ end
379
+
380
+ -> records: [{:pk => 1, "street" => "1111 Main",
381
+ "sector_ids" => ["2","5","32","8"], "department_ids" => ["4","43"]},
382
+ {:pk => 2, "street" => "1122 Main",
383
+ "sector_ids" => ["1","2","3458","9"], "department_ids" => ["8","5"]}]
384
+
385
+ 2-5-5. Setting a belongs_to association id.
386
+ (Multiple records of the table is associated with a single record of another table.)
387
+
388
+ Call set_belongs_to_association_id method on table object with write mode
389
+ (instantiated by create_with_create_write_non_blocking_lock or
390
+ create_with_write_non_blocking_lock).
391
+ It expects the id of the table record, association id name and association id.
392
+
393
+ e.g.1.
394
+
395
+ begin
396
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
397
+
398
+ id = "3"
399
+ write_table.set_belongs_to_association_id(id, "register_id", 78)
400
+ ensure
401
+ write_table.close unless write_table.nil?
402
+ end
403
+
404
+ e.g.2.
405
+
406
+ begin
407
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
408
+
409
+ id = "3"
410
+ write_table.set_belongs_to_association_id(id, "register_id", "78")
411
+ ensure
412
+ write_table.close unless write_table.nil?
413
+ end
414
+
415
+ 2-5-6. :keys_for_has_many_association option for query methods.
416
+
417
+ All the query methods accepts :keys_for_has_many_association option as the last argument
418
+ (options). When it is set, the value for the specified has_many association keys are converted
419
+ to array in the returned hash.
420
+ The query methods: all, find, all_by_key_value, and all_by_multiple_key_values
421
+
422
+ This is because has_many association id is stored as a string with ids separated by comma.
423
+ And simply returning the records from rufus/tokyo keeps that format.
424
+ Note: When an array is passed as a value to rufus/tokyo, it concatenates the elements of
425
+ the array without any delimiter and stores the resulting string value. Hence, this gem
426
+ converts an array to a string with comma separated elements.
427
+
428
+ e.g.1. has_many association id value is not converted to array by default.
429
+
430
+ begin
431
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
432
+
433
+ records = read_table.all
434
+ ensure
435
+ read_table.close unless read_table.nil?
436
+ end
437
+
438
+ -> records: [{:pk => 1, "street" => "1111 Main", "sector_ids" => "2,5,32,8"]},
439
+ {:pk => 2, "street" => "1122 Main", "sector_ids" => "1,2,3458,9"]}]
440
+
441
+ e.g.2. has_many association id value is converted to array
442
+ by specifying :keys_for_has_many_association option.
443
+
444
+ begin
445
+ read_table = TokyoWrapper::Table.create_with_read_non_locking("table.txt")
446
+
447
+ records = read_table.all(:keys_for_has_many_association => ["sector_ids"])
448
+ ensure
449
+ read_table.close unless read_table.nil?
450
+ end
451
+
452
+ -> records: [{:pk => 1, "street" => "1111 Main", "sector_ids" => ["2","5","32","8"]},
453
+ {:pk => 2, "street" => "1122 Main", "sector_ids" => ["1","2","3458","9"]}]
454
+
455
+
46
456
  == Licence
47
457
 
48
458
  Copyright (c) 2010, Tadatoshi Takahashi
@@ -64,13 +64,6 @@ module TokyoWrapper
64
64
  end
65
65
  convert_values_to_array_for_keys(result, options[:keys_for_has_many_association])
66
66
  end
67
-
68
- def all_by_key_value(key, value, options = {})
69
- result = @table.query do |query|
70
- query.add key, :equals, value
71
- end
72
- convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
73
- end
74
67
 
75
68
  end
76
69
 
@@ -22,7 +22,10 @@ module TokyoWrapper
22
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
+ keys_for_has_many_association = ["#{association_id_name}s"]
27
+ keys_for_has_many_association.concat(options[:keys_for_has_many_association]).uniq! if !options[:keys_for_has_many_association].nil? && !options[:keys_for_has_many_association].empty?
28
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, keys_for_has_many_association)
26
29
  end
27
30
 
28
31
  def set_belongs_to_association_id(id, association_id_name, association_id)
@@ -4,6 +4,13 @@ module TokyoWrapper
4
4
 
5
5
  module Query
6
6
 
7
+ def all_by_key_value(key, value, options = {})
8
+ result = @table.query do |query|
9
+ query.add key, :equals, value
10
+ end
11
+ convert_values_to_array_for_keys_for_multiple_key_value_hashes(result, options[:keys_for_has_many_association])
12
+ end
13
+
7
14
  def all_by_multiple_key_values(key_value_hash = {}, options = {})
8
15
  result = @table.query do |query|
9
16
  key_value_hash.each do |key, value|
@@ -279,12 +279,12 @@ describe TokyoWrapper::TableMethods::Associations do
279
279
  "street" => "1111 Main",
280
280
  "city" => "Montreal",
281
281
  "notes" => "Some notes",
282
- "sector_ids" => "2,5,32,8"},
282
+ "sector_ids" => ["2","5","32","8"]},
283
283
  {:pk => id_2.to_s,
284
284
  "street" => "1111 Maisonneuve",
285
285
  "city" => "Montreal",
286
286
  "notes" => "Another notes",
287
- "sector_ids" => "1,2,3458,9"}]
287
+ "sector_ids" => ["1","2","3458","9"]}]
288
288
 
289
289
  read_table.all_by_has_many_association_id("sector_id", "2",
290
290
  :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_1.to_s,
@@ -302,7 +302,7 @@ describe TokyoWrapper::TableMethods::Associations do
302
302
  "street" => "1111 Desjardins",
303
303
  "city" => "Quebec",
304
304
  "notes" => "Different notes",
305
- "sector_ids" => "87,45,1,727"}]
305
+ "sector_ids" => ["87","45","1","727"]}]
306
306
 
307
307
  read_table.all_by_has_many_association_id("sector_id", "45",
308
308
  :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_3.to_s,
@@ -315,7 +315,7 @@ describe TokyoWrapper::TableMethods::Associations do
315
315
  "street" => "1111 Maisonneuve",
316
316
  "city" => "Montreal",
317
317
  "notes" => "Another notes",
318
- "sector_ids" => "1,2,3458,9"}]
318
+ "sector_ids" => ["1","2","3458","9"]}]
319
319
 
320
320
  read_table.all_by_has_many_association_id("sector_id", "3458",
321
321
  :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_2.to_s,
@@ -329,6 +329,151 @@ describe TokyoWrapper::TableMethods::Associations do
329
329
 
330
330
  end
331
331
 
332
+ it "should find all the rows with the given has_many association id" do
333
+
334
+ begin
335
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
336
+
337
+ data_hash_1 = {"street" => "1111 Main",
338
+ "city" => "Montreal",
339
+ "notes" => "Some notes",
340
+ "sector_ids" => ["2","5","32","8"]}
341
+ id_1 = write_table.add(data_hash_1)
342
+ data_hash_2 = {"street" => "1111 Maisonneuve",
343
+ "city" => "Montreal",
344
+ "notes" => "Another notes",
345
+ "sector_ids" => ["1","2","3458","9"]}
346
+ id_2 = write_table.add(data_hash_2)
347
+ data_hash_3 = {"street" => "1111 Desjardins",
348
+ "city" => "Quebec",
349
+ "notes" => "Different notes",
350
+ "sector_ids" => ["87","45","1","727"]}
351
+ id_3 = write_table.add(data_hash_3)
352
+ ensure
353
+ write_table.close unless write_table.nil?
354
+ end
355
+
356
+ begin
357
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
358
+
359
+ read_table.all_by_has_many_association_id("sector_id", "2").should == [{:pk => id_1.to_s,
360
+ "street" => "1111 Main",
361
+ "city" => "Montreal",
362
+ "notes" => "Some notes",
363
+ "sector_ids" => ["2","5","32","8"]},
364
+ {:pk => id_2.to_s,
365
+ "street" => "1111 Maisonneuve",
366
+ "city" => "Montreal",
367
+ "notes" => "Another notes",
368
+ "sector_ids" => ["1","2","3458","9"]}]
369
+
370
+ read_table.all_by_has_many_association_id("sector_id", "45").should == [{:pk => id_3.to_s,
371
+ "street" => "1111 Desjardins",
372
+ "city" => "Quebec",
373
+ "notes" => "Different notes",
374
+ "sector_ids" => ["87","45","1","727"]}]
375
+
376
+ read_table.all_by_has_many_association_id("sector_id", "3458").should == [{:pk => id_2.to_s,
377
+ "street" => "1111 Maisonneuve",
378
+ "city" => "Montreal",
379
+ "notes" => "Another notes",
380
+ "sector_ids" => ["1","2","3458","9"]}]
381
+
382
+ ensure
383
+ read_table.close unless read_table.nil?
384
+ end
385
+
386
+ end
387
+
388
+ it "should find all the rows with the given has_many association id converting the all the has_many association ids values to array" do
389
+
390
+ begin
391
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
392
+
393
+ data_hash_1 = {"street" => "1111 Main",
394
+ "city" => "Montreal",
395
+ "notes" => "Some notes",
396
+ "sector_ids" => ["2","5","32","8"],
397
+ "department_ids" => ["4", "43"]}
398
+ id_1 = write_table.add(data_hash_1)
399
+ data_hash_2 = {"street" => "1111 Maisonneuve",
400
+ "city" => "Montreal",
401
+ "notes" => "Another notes",
402
+ "sector_ids" => ["1","2","3458","9"],
403
+ "department_ids" => ["8", "5"]}
404
+ id_2 = write_table.add(data_hash_2)
405
+ data_hash_3 = {"street" => "1111 Desjardins",
406
+ "city" => "Quebec",
407
+ "notes" => "Different notes",
408
+ "sector_ids" => ["87","45","1","727"]}
409
+ id_3 = write_table.add(data_hash_3)
410
+ ensure
411
+ write_table.close unless write_table.nil?
412
+ end
413
+
414
+ begin
415
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
416
+
417
+ read_table.all_by_has_many_association_id("sector_id", "2").should == [{:pk => id_1.to_s,
418
+ "street" => "1111 Main",
419
+ "city" => "Montreal",
420
+ "notes" => "Some notes",
421
+ "sector_ids" => ["2","5","32","8"],
422
+ "department_ids" => "4,43"},
423
+ {:pk => id_2.to_s,
424
+ "street" => "1111 Maisonneuve",
425
+ "city" => "Montreal",
426
+ "notes" => "Another notes",
427
+ "sector_ids" => ["1","2","3458","9"],
428
+ "department_ids" => "8,5"}]
429
+
430
+ read_table.all_by_has_many_association_id("sector_id", "2",
431
+ :keys_for_has_many_association => ["department_ids"]).should == [{:pk => id_1.to_s,
432
+ "street" => "1111 Main",
433
+ "city" => "Montreal",
434
+ "notes" => "Some notes",
435
+ "sector_ids" => ["2","5","32","8"],
436
+ "department_ids" => ["4","43"]},
437
+ {:pk => id_2.to_s,
438
+ "street" => "1111 Maisonneuve",
439
+ "city" => "Montreal",
440
+ "notes" => "Another notes",
441
+ "sector_ids" => ["1","2","3458","9"],
442
+ "department_ids" => ["8","5"]}]
443
+
444
+ read_table.all_by_has_many_association_id("sector_id", "45").should == [{:pk => id_3.to_s,
445
+ "street" => "1111 Desjardins",
446
+ "city" => "Quebec",
447
+ "notes" => "Different notes",
448
+ "sector_ids" => ["87","45","1","727"]}]
449
+
450
+ read_table.all_by_has_many_association_id("sector_id", "45",
451
+ :keys_for_has_many_association => ["department_ids"]).should == [{:pk => id_3.to_s,
452
+ "street" => "1111 Desjardins",
453
+ "city" => "Quebec",
454
+ "notes" => "Different notes",
455
+ "sector_ids" => ["87","45","1","727"]}]
456
+
457
+ read_table.all_by_has_many_association_id("sector_id", "3458").should == [{:pk => id_2.to_s,
458
+ "street" => "1111 Maisonneuve",
459
+ "city" => "Montreal",
460
+ "notes" => "Another notes",
461
+ "sector_ids" => ["1","2","3458","9"],
462
+ "department_ids" => "8,5"}]
463
+
464
+ read_table.all_by_has_many_association_id("sector_id", "3458",
465
+ :keys_for_has_many_association => ["department_ids"]).should == [{:pk => id_2.to_s,
466
+ "street" => "1111 Maisonneuve",
467
+ "city" => "Montreal",
468
+ "notes" => "Another notes",
469
+ "sector_ids" => ["1","2","3458","9"],
470
+ "department_ids" => ["8","5"]}]
471
+ ensure
472
+ read_table.close unless read_table.nil?
473
+ end
474
+
475
+ end
476
+
332
477
  end
333
478
 
334
479
  context "belongs_to associations" do
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
2
2
 
3
- describe TokyoWrapper::TableMethods::Associations do
3
+ describe TokyoWrapper::TableMethods::Query do
4
4
  include RufusTokyoMacros
5
5
 
6
6
  before(:each) do
@@ -14,6 +14,110 @@ describe TokyoWrapper::TableMethods::Associations do
14
14
 
15
15
  context "Find all" do
16
16
 
17
+ it "should find all the rows that have a given value" do
18
+
19
+ begin
20
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
21
+
22
+ data_hash_1 = {"street" => "1111 Main",
23
+ "city" => "Montreal",
24
+ "province" => "Quebec",
25
+ "country" => "Canada",
26
+ "notes" => "Some notes"}
27
+ id_1 = write_table.add(data_hash_1)
28
+ data_hash_2 = {"street" => "1111 Maisonneuve",
29
+ "city" => "Montreal",
30
+ "province" => "Quebec",
31
+ "country" => "Canada",
32
+ "notes" => "Another notes"}
33
+ id_2 = write_table.add(data_hash_2)
34
+ data_hash_3 = {"street" => "1111 Main",
35
+ "city" => "Quebec",
36
+ "province" => "Quebec",
37
+ "country" => "Canada",
38
+ "notes" => "Different notes"}
39
+ id_3 = write_table.add(data_hash_3)
40
+
41
+ ensure
42
+ write_table.close unless write_table.nil?
43
+ end
44
+
45
+ begin
46
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
47
+
48
+ read_table.all_by_key_value("city", "Montreal").should == [{:pk => id_1.to_s,
49
+ "street" => "1111 Main",
50
+ "city" => "Montreal",
51
+ "province" => "Quebec",
52
+ "country" => "Canada",
53
+ "notes" => "Some notes"},
54
+ {:pk => id_2.to_s,
55
+ "street" => "1111 Maisonneuve",
56
+ "city" => "Montreal",
57
+ "province" => "Quebec",
58
+ "country" => "Canada",
59
+ "notes" => "Another notes"}]
60
+ ensure
61
+ read_table.close unless read_table.nil?
62
+ end
63
+
64
+ end
65
+
66
+ it "should find all the rows that have a given value with has_many association ids converted to array" do
67
+
68
+ begin
69
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
70
+
71
+ data_hash_1 = {"street" => "1111 Main",
72
+ "city" => "Montreal",
73
+ "province" => "Quebec",
74
+ "country" => "Canada",
75
+ "notes" => "Some notes",
76
+ "sector_ids" => "2,5,34,8"}
77
+ id_1 = write_table.add(data_hash_1)
78
+ data_hash_2 = {"street" => "1111 Maisonneuve",
79
+ "city" => "Montreal",
80
+ "province" => "Quebec",
81
+ "country" => "Canada",
82
+ "notes" => "Another notes",
83
+ "sector_ids" => "3,5,6,8"}
84
+ id_2 = write_table.add(data_hash_2)
85
+ data_hash_3 = {"street" => "1111 Main",
86
+ "city" => "Quebec",
87
+ "province" => "Quebec",
88
+ "country" => "Canada",
89
+ "notes" => "Different notes",
90
+ "sector_ids" => "2,1,34,7"}
91
+ id_3 = write_table.add(data_hash_3)
92
+
93
+ ensure
94
+ write_table.close unless write_table.nil?
95
+ end
96
+
97
+ begin
98
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
99
+
100
+ read_table.all_by_key_value("city", "Montreal",
101
+ :keys_for_has_many_association => ["sector_ids"]).should == [{:pk => id_1.to_s,
102
+ "street" => "1111 Main",
103
+ "city" => "Montreal",
104
+ "province" => "Quebec",
105
+ "country" => "Canada",
106
+ "notes" => "Some notes",
107
+ "sector_ids" => ["2","5","34","8"]},
108
+ {:pk => id_2.to_s,
109
+ "street" => "1111 Maisonneuve",
110
+ "city" => "Montreal",
111
+ "province" => "Quebec",
112
+ "country" => "Canada",
113
+ "notes" => "Another notes",
114
+ "sector_ids" => ["3","5","6","8"]}]
115
+ ensure
116
+ read_table.close unless read_table.nil?
117
+ end
118
+
119
+ end
120
+
17
121
  it "should find all the rows that have given values" do
18
122
 
19
123
  begin
@@ -263,110 +263,6 @@ describe TokyoWrapper::Table do
263
263
  read_table.close unless read_table.nil?
264
264
  end
265
265
 
266
- end
267
-
268
- it "should find all the rows that have a given value" do
269
-
270
- begin
271
- write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
272
-
273
- data_hash_1 = {"street" => "1111 Main",
274
- "city" => "Montreal",
275
- "province" => "Quebec",
276
- "country" => "Canada",
277
- "notes" => "Some notes"}
278
- id_1 = write_table.add(data_hash_1)
279
- data_hash_2 = {"street" => "1111 Maisonneuve",
280
- "city" => "Montreal",
281
- "province" => "Quebec",
282
- "country" => "Canada",
283
- "notes" => "Another notes"}
284
- id_2 = write_table.add(data_hash_2)
285
- data_hash_3 = {"street" => "1111 Main",
286
- "city" => "Quebec",
287
- "province" => "Quebec",
288
- "country" => "Canada",
289
- "notes" => "Different notes"}
290
- id_3 = write_table.add(data_hash_3)
291
-
292
- ensure
293
- write_table.close unless write_table.nil?
294
- end
295
-
296
- begin
297
- read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
298
-
299
- read_table.all_by_key_value("city", "Montreal").should == [{:pk => id_1.to_s,
300
- "street" => "1111 Main",
301
- "city" => "Montreal",
302
- "province" => "Quebec",
303
- "country" => "Canada",
304
- "notes" => "Some notes"},
305
- {:pk => id_2.to_s,
306
- "street" => "1111 Maisonneuve",
307
- "city" => "Montreal",
308
- "province" => "Quebec",
309
- "country" => "Canada",
310
- "notes" => "Another notes"}]
311
- ensure
312
- read_table.close unless read_table.nil?
313
- end
314
-
315
- end
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
266
  end
371
267
 
372
268
  end
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.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tadatoshi Takahashi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-31 00:00:00 -05:00
12
+ date: 2010-02-05 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15