tokyo_wrapper 0.1.1 → 0.1.2
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.
data/README.rdoc
CHANGED
@@ -4,9 +4,11 @@
|
|
4
4
|
|
5
5
|
It is extracted from the code I frequently use when using rufus-tokyo to access Tokyo Cabinet.
|
6
6
|
|
7
|
-
For example, for table, I always add a row with unique id so I always use generate_unique_id method.
|
7
|
+
For example, for table, I always add a row with unique id so I always use generate_unique_id method.
|
8
|
+
By wrapping the call to this method, the code that is using the rufus/tokyo doesn't get cluttered.
|
8
9
|
|
9
|
-
rufus-tokyo is "ffi based ruby library to access Tokyo Cabinet and Tokyo Tyrant" by John Mettraux
|
10
|
+
rufus-tokyo is "ffi based ruby library to access Tokyo Cabinet and Tokyo Tyrant" by John Mettraux
|
11
|
+
(Copyright (c) 2009-2010, see http://github.com/jmettraux/rufus-tokyo/blob/master/LICENSE.txt).
|
10
12
|
|
11
13
|
== Required Ruby Gems
|
12
14
|
|
data/lib/tokyo_wrapper/table.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
+
require 'tokyo_wrapper/table_methods/associations'
|
2
|
+
|
1
3
|
module TokyoWrapper
|
2
4
|
|
3
5
|
class Table
|
6
|
+
include TokyoWrapper::TableMethods::Associations
|
4
7
|
|
5
8
|
def initialize(table)
|
6
9
|
@table = table
|
@@ -38,19 +41,11 @@ module TokyoWrapper
|
|
38
41
|
else
|
39
42
|
false
|
40
43
|
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def
|
44
|
-
|
45
|
-
|
46
|
-
unless association_ids_string.split(",").include?(association_id.to_s)
|
47
|
-
@table[id.to_s] = @table[id.to_s].merge({"#{association_id_name}s" => "#{association_ids_string},#{association_id}"})
|
48
|
-
end
|
49
|
-
true
|
50
|
-
else
|
51
|
-
false
|
52
|
-
end
|
53
|
-
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(id)
|
47
|
+
@table.delete(id.to_s)
|
48
|
+
end
|
54
49
|
|
55
50
|
def all
|
56
51
|
@table.query
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module TokyoWrapper
|
2
|
+
|
3
|
+
module TableMethods
|
4
|
+
|
5
|
+
module Associations
|
6
|
+
|
7
|
+
def add_has_many_association_id(id, association_id_name, association_id)
|
8
|
+
if !@table[id.to_s].nil? && !@table[id.to_s].empty?
|
9
|
+
association_ids_string = @table[id.to_s]["#{association_id_name}s"]
|
10
|
+
if association_ids_string.nil?
|
11
|
+
@table[id.to_s] = @table[id.to_s].merge({"#{association_id_name}s" => association_id.to_s})
|
12
|
+
elsif !association_ids_string.split(",").include?(association_id.to_s)
|
13
|
+
@table[id.to_s] = @table[id.to_s].merge({"#{association_id_name}s" => "#{association_ids_string},#{association_id}"})
|
14
|
+
end
|
15
|
+
true
|
16
|
+
else
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def all_by_has_many_association_id(association_id_name, association_id)
|
22
|
+
@table.query do | query |
|
23
|
+
query.add "#{association_id_name}s", :stror, association_id.to_s
|
24
|
+
query.no_pk
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def set_belongs_to_association_id(id, association_id_name, association_id)
|
29
|
+
if !@table[id.to_s].nil? && !@table[id.to_s].empty?
|
30
|
+
@table[id.to_s] = @table[id.to_s].merge({association_id_name => association_id.to_s})
|
31
|
+
true
|
32
|
+
else
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def keys_for_belongs_to_association_id(association_id_name, association_id)
|
38
|
+
table_result_set = @table.query do |query|
|
39
|
+
query.add association_id_name, :equals, association_id.to_s
|
40
|
+
query.no_pk
|
41
|
+
end
|
42
|
+
keys = []
|
43
|
+
table_result_set.each do |row|
|
44
|
+
keys += row.keys
|
45
|
+
keys.uniq!
|
46
|
+
end
|
47
|
+
keys.delete(association_id_name)
|
48
|
+
keys.sort
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
@@ -0,0 +1,451 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe TokyoWrapper::TableMethods::Associations do
|
4
|
+
include RufusTokyoMacros
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
@table_file = File.expand_path(File.dirname(__FILE__) + '/../../tokyo_cabinet_files/table.tct')
|
8
|
+
clear_table(@table_file)
|
9
|
+
end
|
10
|
+
|
11
|
+
after(:each) do
|
12
|
+
clear_table(@table_file)
|
13
|
+
end
|
14
|
+
|
15
|
+
context "has_many associations" do
|
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
|
+
it "should add data with has_many association" do
|
34
|
+
|
35
|
+
begin
|
36
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
37
|
+
|
38
|
+
data_hash = {"street" => "1111 Main",
|
39
|
+
"city" => "Montreal",
|
40
|
+
"notes" => "Some notes",
|
41
|
+
"sector_ids" => ["2","5","34","8"]}
|
42
|
+
id = write_table.add(data_hash)
|
43
|
+
ensure
|
44
|
+
write_table.close unless write_table.nil?
|
45
|
+
end
|
46
|
+
|
47
|
+
begin
|
48
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
49
|
+
|
50
|
+
read_table.all.should == [{:pk => id.to_s,
|
51
|
+
"street" => "1111 Main",
|
52
|
+
"city" => "Montreal",
|
53
|
+
"notes" => "Some notes",
|
54
|
+
"sector_ids" => "2,5,34,8"}]
|
55
|
+
|
56
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
57
|
+
"city" => "Montreal",
|
58
|
+
"notes" => "Some notes",
|
59
|
+
"sector_ids" => "2,5,34,8"}
|
60
|
+
ensure
|
61
|
+
read_table.close unless read_table.nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should update data with has_many association" do
|
67
|
+
|
68
|
+
begin
|
69
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
70
|
+
|
71
|
+
data_hash = {"street" => "1111 Main",
|
72
|
+
"city" => "Montreal",
|
73
|
+
"notes" => "Some notes",
|
74
|
+
"sector_ids" => ["2","5","34","8"]}
|
75
|
+
id = write_table.add(data_hash)
|
76
|
+
ensure
|
77
|
+
write_table.close unless write_table.nil?
|
78
|
+
end
|
79
|
+
|
80
|
+
begin
|
81
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
82
|
+
|
83
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
84
|
+
"city" => "Montreal",
|
85
|
+
"notes" => "Some notes",
|
86
|
+
"sector_ids" => "2,5,34,8"}
|
87
|
+
ensure
|
88
|
+
read_table.close unless read_table.nil?
|
89
|
+
end
|
90
|
+
|
91
|
+
begin
|
92
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
93
|
+
|
94
|
+
result = write_table.update(id, "notes" => "Recently situation has been changed.", "sector_ids" => ["2","5","40","8","12"])
|
95
|
+
result.should be_true
|
96
|
+
ensure
|
97
|
+
write_table.close unless write_table.nil?
|
98
|
+
end
|
99
|
+
|
100
|
+
begin
|
101
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
102
|
+
|
103
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
104
|
+
"city" => "Montreal",
|
105
|
+
"notes" => "Recently situation has been changed.",
|
106
|
+
"sector_ids" => "2,5,40,8,12"}
|
107
|
+
ensure
|
108
|
+
read_table.close unless read_table.nil?
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should add a has_many association id" do
|
114
|
+
|
115
|
+
begin
|
116
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
117
|
+
|
118
|
+
data_hash = {"street" => "1111 Main",
|
119
|
+
"city" => "Montreal",
|
120
|
+
"notes" => "Some notes",
|
121
|
+
"sector_ids" => ["2","5","34","8"]}
|
122
|
+
id = write_table.add(data_hash)
|
123
|
+
ensure
|
124
|
+
write_table.close unless write_table.nil?
|
125
|
+
end
|
126
|
+
|
127
|
+
begin
|
128
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
129
|
+
|
130
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
131
|
+
"city" => "Montreal",
|
132
|
+
"notes" => "Some notes",
|
133
|
+
"sector_ids" => "2,5,34,8"}
|
134
|
+
ensure
|
135
|
+
read_table.close unless read_table.nil?
|
136
|
+
end
|
137
|
+
|
138
|
+
begin
|
139
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
140
|
+
|
141
|
+
result_1 = write_table.add_has_many_association_id(id, "sector_id", 78)
|
142
|
+
result_1.should be_true
|
143
|
+
ensure
|
144
|
+
write_table.close unless write_table.nil?
|
145
|
+
end
|
146
|
+
|
147
|
+
begin
|
148
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
149
|
+
|
150
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
151
|
+
"city" => "Montreal",
|
152
|
+
"notes" => "Some notes",
|
153
|
+
"sector_ids" => "2,5,34,8,78"}
|
154
|
+
ensure
|
155
|
+
read_table.close unless read_table.nil?
|
156
|
+
end
|
157
|
+
|
158
|
+
begin
|
159
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
160
|
+
|
161
|
+
result_2 = write_table.add_has_many_association_id(id, "sector_id", "3")
|
162
|
+
result_2.should be_true
|
163
|
+
ensure
|
164
|
+
write_table.close unless write_table.nil?
|
165
|
+
end
|
166
|
+
|
167
|
+
begin
|
168
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
169
|
+
|
170
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
171
|
+
"city" => "Montreal",
|
172
|
+
"notes" => "Some notes",
|
173
|
+
"sector_ids" => "2,5,34,8,78,3"}
|
174
|
+
ensure
|
175
|
+
read_table.close unless read_table.nil?
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should not add a has_many association id if it's already set" do
|
181
|
+
|
182
|
+
begin
|
183
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
184
|
+
|
185
|
+
data_hash = {"street" => "1111 Main",
|
186
|
+
"city" => "Montreal",
|
187
|
+
"notes" => "Some notes",
|
188
|
+
"sector_ids" => ["2","5","34","8"]}
|
189
|
+
id = write_table.add(data_hash)
|
190
|
+
ensure
|
191
|
+
write_table.close unless write_table.nil?
|
192
|
+
end
|
193
|
+
|
194
|
+
begin
|
195
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
196
|
+
|
197
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
198
|
+
"city" => "Montreal",
|
199
|
+
"notes" => "Some notes",
|
200
|
+
"sector_ids" => "2,5,34,8"}
|
201
|
+
ensure
|
202
|
+
read_table.close unless read_table.nil?
|
203
|
+
end
|
204
|
+
|
205
|
+
begin
|
206
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
207
|
+
|
208
|
+
result_1 = write_table.add_has_many_association_id(id, "sector_id", "8")
|
209
|
+
result_1.should be_true
|
210
|
+
ensure
|
211
|
+
write_table.close unless write_table.nil?
|
212
|
+
end
|
213
|
+
|
214
|
+
begin
|
215
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
216
|
+
|
217
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
218
|
+
"city" => "Montreal",
|
219
|
+
"notes" => "Some notes",
|
220
|
+
"sector_ids" => "2,5,34,8"}
|
221
|
+
ensure
|
222
|
+
read_table.close unless read_table.nil?
|
223
|
+
end
|
224
|
+
|
225
|
+
end
|
226
|
+
|
227
|
+
it "should add a has_many association id when no association id is set for the association" do
|
228
|
+
|
229
|
+
begin
|
230
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
231
|
+
|
232
|
+
data_hash = {"street" => "1111 Main",
|
233
|
+
"city" => "Montreal",
|
234
|
+
"notes" => "Some notes"}
|
235
|
+
id = write_table.add(data_hash)
|
236
|
+
|
237
|
+
result_1 = write_table.add_has_many_association_id(id, "sector_id", "8")
|
238
|
+
result_1.should be_true
|
239
|
+
ensure
|
240
|
+
write_table.close unless write_table.nil?
|
241
|
+
end
|
242
|
+
|
243
|
+
begin
|
244
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
245
|
+
|
246
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
247
|
+
"city" => "Montreal",
|
248
|
+
"notes" => "Some notes",
|
249
|
+
"sector_ids" => "8"}
|
250
|
+
ensure
|
251
|
+
read_table.close unless read_table.nil?
|
252
|
+
end
|
253
|
+
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should find all the rows with the given has_many association id" do
|
257
|
+
|
258
|
+
begin
|
259
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
260
|
+
|
261
|
+
data_hash_1 = {"street" => "1111 Main",
|
262
|
+
"city" => "Montreal",
|
263
|
+
"notes" => "Some notes",
|
264
|
+
"sector_ids" => ["2","5","32","8"]}
|
265
|
+
id_1 = write_table.add(data_hash_1)
|
266
|
+
data_hash_2 = {"street" => "1111 Masonneuve",
|
267
|
+
"city" => "Montreal",
|
268
|
+
"notes" => "Another notes",
|
269
|
+
"sector_ids" => ["1","2","3458","9"]}
|
270
|
+
id_2 = write_table.add(data_hash_2)
|
271
|
+
data_hash_3 = {"street" => "1111 Desjardins",
|
272
|
+
"city" => "Quebec",
|
273
|
+
"notes" => "Different notes",
|
274
|
+
"sector_ids" => ["87","45","1","727"]}
|
275
|
+
id_3 = write_table.add(data_hash_3)
|
276
|
+
ensure
|
277
|
+
write_table.close unless write_table.nil?
|
278
|
+
end
|
279
|
+
|
280
|
+
begin
|
281
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
282
|
+
|
283
|
+
read_table.all_by_has_many_association_id("sector_id", "2").should == [{"street" => "1111 Main",
|
284
|
+
"city" => "Montreal",
|
285
|
+
"notes" => "Some notes",
|
286
|
+
"sector_ids" => "2,5,32,8"},
|
287
|
+
{"street" => "1111 Masonneuve",
|
288
|
+
"city" => "Montreal",
|
289
|
+
"notes" => "Another notes",
|
290
|
+
"sector_ids" => "1,2,3458,9"}]
|
291
|
+
read_table.all_by_has_many_association_id("sector_id", "45").should == [{"street" => "1111 Desjardins",
|
292
|
+
"city" => "Quebec",
|
293
|
+
"notes" => "Different notes",
|
294
|
+
"sector_ids" => "87,45,1,727"}]
|
295
|
+
read_table.all_by_has_many_association_id("sector_id", "3458").should == [{"street" => "1111 Masonneuve",
|
296
|
+
"city" => "Montreal",
|
297
|
+
"notes" => "Another notes",
|
298
|
+
"sector_ids" => "1,2,3458,9"}]
|
299
|
+
ensure
|
300
|
+
read_table.close unless read_table.nil?
|
301
|
+
end
|
302
|
+
|
303
|
+
end
|
304
|
+
|
305
|
+
end
|
306
|
+
|
307
|
+
context "belongs_to associations" do
|
308
|
+
|
309
|
+
it "should set belongs_to association id (integer)" do
|
310
|
+
|
311
|
+
begin
|
312
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
313
|
+
|
314
|
+
data_hash = {"street" => "1111 Main",
|
315
|
+
"city" => "Montreal",
|
316
|
+
"notes" => "Some notes"}
|
317
|
+
id = write_table.add(data_hash)
|
318
|
+
ensure
|
319
|
+
write_table.close unless write_table.nil?
|
320
|
+
end
|
321
|
+
|
322
|
+
begin
|
323
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
324
|
+
|
325
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
326
|
+
"city" => "Montreal",
|
327
|
+
"notes" => "Some notes"}
|
328
|
+
ensure
|
329
|
+
read_table.close unless read_table.nil?
|
330
|
+
end
|
331
|
+
|
332
|
+
begin
|
333
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
334
|
+
|
335
|
+
result_1 = write_table.set_belongs_to_association_id(id, "register_id", 78)
|
336
|
+
result_1.should be_true
|
337
|
+
ensure
|
338
|
+
write_table.close unless write_table.nil?
|
339
|
+
end
|
340
|
+
|
341
|
+
begin
|
342
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
343
|
+
|
344
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
345
|
+
"city" => "Montreal",
|
346
|
+
"notes" => "Some notes",
|
347
|
+
"register_id" => "78"}
|
348
|
+
ensure
|
349
|
+
read_table.close unless read_table.nil?
|
350
|
+
end
|
351
|
+
|
352
|
+
end
|
353
|
+
|
354
|
+
it "should set belongs_to association id (string)" do
|
355
|
+
|
356
|
+
begin
|
357
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
358
|
+
|
359
|
+
data_hash = {"street" => "1111 Main",
|
360
|
+
"city" => "Montreal",
|
361
|
+
"notes" => "Some notes"}
|
362
|
+
id = write_table.add(data_hash)
|
363
|
+
ensure
|
364
|
+
write_table.close unless write_table.nil?
|
365
|
+
end
|
366
|
+
|
367
|
+
begin
|
368
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
369
|
+
|
370
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
371
|
+
"city" => "Montreal",
|
372
|
+
"notes" => "Some notes"}
|
373
|
+
ensure
|
374
|
+
read_table.close unless read_table.nil?
|
375
|
+
end
|
376
|
+
|
377
|
+
begin
|
378
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
379
|
+
|
380
|
+
result_1 = write_table.set_belongs_to_association_id(id, "register_id", "78")
|
381
|
+
result_1.should be_true
|
382
|
+
ensure
|
383
|
+
write_table.close unless write_table.nil?
|
384
|
+
end
|
385
|
+
|
386
|
+
begin
|
387
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
388
|
+
|
389
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
390
|
+
"city" => "Montreal",
|
391
|
+
"notes" => "Some notes",
|
392
|
+
"register_id" => "78"}
|
393
|
+
ensure
|
394
|
+
read_table.close unless read_table.nil?
|
395
|
+
end
|
396
|
+
|
397
|
+
end
|
398
|
+
|
399
|
+
it "should get all the keys for the rows associated with the belongs_to association_id" do
|
400
|
+
|
401
|
+
begin
|
402
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
403
|
+
|
404
|
+
data_hash_1 = {"street" => "1111 Main",
|
405
|
+
"city" => "Montreal",
|
406
|
+
"notes" => "Some notes",
|
407
|
+
"register_id" => "45"}
|
408
|
+
id_1 = write_table.add(data_hash_1)
|
409
|
+
data_hash_2 = {"city" => "Montreal",
|
410
|
+
"province" => "Quebec",
|
411
|
+
"notes" => "Another notes",
|
412
|
+
"register_id" => "45"}
|
413
|
+
id_2 = write_table.add(data_hash_2)
|
414
|
+
data_hash_3 = {"street" => "1111 Main",
|
415
|
+
"city" => "Montreal",
|
416
|
+
"country" => "Canada",
|
417
|
+
"notes" => "Different notes",
|
418
|
+
"register_id" => "45"}
|
419
|
+
id_3 = write_table.add(data_hash_3)
|
420
|
+
|
421
|
+
ensure
|
422
|
+
write_table.close unless write_table.nil?
|
423
|
+
end
|
424
|
+
|
425
|
+
begin
|
426
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
427
|
+
|
428
|
+
read_table.find(id_1).should == {"street" => "1111 Main",
|
429
|
+
"city" => "Montreal",
|
430
|
+
"notes" => "Some notes",
|
431
|
+
"register_id" => "45"}
|
432
|
+
read_table.find(id_2).should == {"city" => "Montreal",
|
433
|
+
"province" => "Quebec",
|
434
|
+
"notes" => "Another notes",
|
435
|
+
"register_id" => "45"}
|
436
|
+
read_table.find(id_3).should == {"street" => "1111 Main",
|
437
|
+
"city" => "Montreal",
|
438
|
+
"country" => "Canada",
|
439
|
+
"notes" => "Different notes",
|
440
|
+
"register_id" => "45"}
|
441
|
+
|
442
|
+
read_table.keys_for_belongs_to_association_id("register_id", "45").should == ["city", "country", "notes", "province", "street"]
|
443
|
+
ensure
|
444
|
+
read_table.close unless read_table.nil?
|
445
|
+
end
|
446
|
+
|
447
|
+
end
|
448
|
+
|
449
|
+
end
|
450
|
+
|
451
|
+
end
|
@@ -16,27 +16,31 @@ describe TokyoWrapper::Table do
|
|
16
16
|
|
17
17
|
it "should add data" do
|
18
18
|
|
19
|
-
|
19
|
+
begin
|
20
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
20
21
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
22
|
+
data_hash = {"street" => "1111 Main",
|
23
|
+
"city" => "Montreal",
|
24
|
+
"notes" => "Some notes"}
|
25
|
+
id = write_table.add(data_hash)
|
26
|
+
ensure
|
27
|
+
write_table.close unless write_table.nil?
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
read_table.all.should == [{:pk => id.to_s,
|
34
|
+
"street" => "1111 Main",
|
35
|
+
"city" => "Montreal",
|
36
|
+
"notes" => "Some notes"}]
|
34
37
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
38
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
39
|
+
"city" => "Montreal",
|
40
|
+
"notes" => "Some notes"}
|
41
|
+
ensure
|
42
|
+
read_table.close unless read_table.nil?
|
43
|
+
end
|
40
44
|
|
41
45
|
end
|
42
46
|
|
@@ -46,219 +50,90 @@ describe TokyoWrapper::Table do
|
|
46
50
|
|
47
51
|
it "should update data" do
|
48
52
|
|
49
|
-
|
53
|
+
begin
|
54
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
50
55
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
56
|
+
data_hash = {"street" => "1111 Main",
|
57
|
+
"city" => "Montreal",
|
58
|
+
"notes" => "Some notes"}
|
59
|
+
id = write_table.add(data_hash)
|
60
|
+
ensure
|
61
|
+
write_table.close unless write_table.nil?
|
62
|
+
end
|
55
63
|
|
56
|
-
|
57
|
-
|
58
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
64
|
+
begin
|
65
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
59
66
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
68
|
+
"city" => "Montreal",
|
69
|
+
"notes" => "Some notes"}
|
70
|
+
ensure
|
71
|
+
read_table.close unless read_table.nil?
|
72
|
+
end
|
73
|
+
|
74
|
+
begin
|
75
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
67
76
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
77
|
+
result = write_table.update(id, "notes" => "Recently situation has been changed.")
|
78
|
+
result.should be_true
|
79
|
+
ensure
|
80
|
+
write_table.close unless write_table.nil?
|
81
|
+
end
|
72
82
|
|
73
|
-
|
83
|
+
begin
|
84
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
74
85
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
86
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
87
|
+
"city" => "Montreal",
|
88
|
+
"notes" => "Recently situation has been changed."}
|
89
|
+
ensure
|
90
|
+
read_table.close unless read_table.nil?
|
91
|
+
end
|
80
92
|
|
81
93
|
end
|
82
94
|
|
83
95
|
end
|
84
96
|
|
85
|
-
context "
|
86
|
-
|
87
|
-
it "should convert array parameter value to joined comma-separated string" do
|
88
|
-
|
89
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
90
|
-
|
91
|
-
params_1 = { "name" => "Temp name", "sector_ids" => [2,5,34,8] }
|
92
|
-
|
93
|
-
write_table.send(:convert_params, params_1).should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
|
94
|
-
|
95
|
-
params_2 = { "name" => "Temp name", "sector_ids" => ["2","5","34","8"] }
|
96
|
-
|
97
|
-
write_table.send(:convert_params, params_2).should == { "name" => "Temp name", "sector_ids" => "2,5,34,8" }
|
98
|
-
|
99
|
-
write_table.close
|
100
|
-
|
101
|
-
end
|
97
|
+
context "Delete" do
|
102
98
|
|
103
|
-
it "should
|
104
|
-
|
105
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
99
|
+
it "should delete a row" do
|
106
100
|
|
107
|
-
|
108
|
-
|
109
|
-
"notes" => "Some notes",
|
110
|
-
"sector_ids" => ["2","5","34","8"]}
|
111
|
-
id = write_table.add(data_hash)
|
101
|
+
begin
|
102
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
112
103
|
|
113
|
-
|
104
|
+
data_hash = {"street" => "1111 Main",
|
105
|
+
"city" => "Montreal",
|
106
|
+
"notes" => "Some notes"}
|
107
|
+
id = write_table.add(data_hash)
|
108
|
+
ensure
|
109
|
+
write_table.close unless write_table.nil?
|
110
|
+
end
|
114
111
|
|
115
|
-
|
116
|
-
|
117
|
-
read_table.all.should == [{:pk => id.to_s,
|
118
|
-
"street" => "1111 Main",
|
119
|
-
"city" => "Montreal",
|
120
|
-
"notes" => "Some notes",
|
121
|
-
"sector_ids" => "2,5,34,8"}]
|
112
|
+
begin
|
113
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
122
114
|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
115
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
116
|
+
"city" => "Montreal",
|
117
|
+
"notes" => "Some notes"}
|
118
|
+
ensure
|
119
|
+
read_table.close unless read_table.nil?
|
120
|
+
end
|
121
|
+
|
122
|
+
begin
|
123
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
124
|
+
|
125
|
+
write_table.delete(id)
|
126
|
+
ensure
|
127
|
+
write_table.close unless write_table.nil?
|
128
|
+
end
|
129
129
|
|
130
|
-
|
131
|
-
|
132
|
-
it "should update data with association" do
|
133
|
-
|
134
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
135
|
-
|
136
|
-
data_hash = {"street" => "1111 Main",
|
137
|
-
"city" => "Montreal",
|
138
|
-
"notes" => "Some notes",
|
139
|
-
"sector_ids" => ["2","5","34","8"]}
|
140
|
-
id = write_table.add(data_hash)
|
141
|
-
|
142
|
-
write_table.close
|
143
|
-
|
144
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
145
|
-
|
146
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
147
|
-
"city" => "Montreal",
|
148
|
-
"notes" => "Some notes",
|
149
|
-
"sector_ids" => "2,5,34,8"}
|
150
|
-
|
151
|
-
read_table.close
|
152
|
-
|
153
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
154
|
-
|
155
|
-
result = write_table.update(id, "notes" => "Recently situation has been changed.", "sector_ids" => ["2","5","40","8","12"])
|
156
|
-
result.should be_true
|
157
|
-
|
158
|
-
write_table.close
|
159
|
-
|
160
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
161
|
-
|
162
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
163
|
-
"city" => "Montreal",
|
164
|
-
"notes" => "Recently situation has been changed.",
|
165
|
-
"sector_ids" => "2,5,40,8,12"}
|
166
|
-
|
167
|
-
read_table.close
|
168
|
-
|
169
|
-
end
|
170
|
-
|
171
|
-
it "should add an association id" do
|
172
|
-
|
173
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
174
|
-
|
175
|
-
data_hash = {"street" => "1111 Main",
|
176
|
-
"city" => "Montreal",
|
177
|
-
"notes" => "Some notes",
|
178
|
-
"sector_ids" => ["2","5","34","8"]}
|
179
|
-
id = write_table.add(data_hash)
|
180
|
-
|
181
|
-
write_table.close
|
182
|
-
|
183
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
184
|
-
|
185
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
186
|
-
"city" => "Montreal",
|
187
|
-
"notes" => "Some notes",
|
188
|
-
"sector_ids" => "2,5,34,8"}
|
189
|
-
|
190
|
-
read_table.close
|
191
|
-
|
192
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
193
|
-
|
194
|
-
result_1 = write_table.add_association_id(id, "sector_id", 78)
|
195
|
-
result_1.should be_true
|
196
|
-
|
197
|
-
write_table.close
|
198
|
-
|
199
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
200
|
-
|
201
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
202
|
-
"city" => "Montreal",
|
203
|
-
"notes" => "Some notes",
|
204
|
-
"sector_ids" => "2,5,34,8,78"}
|
205
|
-
|
206
|
-
read_table.close
|
207
|
-
|
208
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
209
|
-
|
210
|
-
result_2 = write_table.add_association_id(id, "sector_id", "3")
|
211
|
-
result_2.should be_true
|
212
|
-
|
213
|
-
write_table.close
|
214
|
-
|
215
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
216
|
-
|
217
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
218
|
-
"city" => "Montreal",
|
219
|
-
"notes" => "Some notes",
|
220
|
-
"sector_ids" => "2,5,34,8,78,3"}
|
221
|
-
|
222
|
-
read_table.close
|
223
|
-
|
224
|
-
end
|
225
|
-
|
226
|
-
it "should not add an association id if it's already set" do
|
227
|
-
|
228
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
229
|
-
|
230
|
-
data_hash = {"street" => "1111 Main",
|
231
|
-
"city" => "Montreal",
|
232
|
-
"notes" => "Some notes",
|
233
|
-
"sector_ids" => ["2","5","34","8"]}
|
234
|
-
id = write_table.add(data_hash)
|
235
|
-
|
236
|
-
write_table.close
|
237
|
-
|
238
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
239
|
-
|
240
|
-
read_table.find(id).should == {"street" => "1111 Main",
|
241
|
-
"city" => "Montreal",
|
242
|
-
"notes" => "Some notes",
|
243
|
-
"sector_ids" => "2,5,34,8"}
|
244
|
-
|
245
|
-
read_table.close
|
246
|
-
|
247
|
-
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
|
248
|
-
|
249
|
-
result_1 = write_table.add_association_id(id, "sector_id", "8")
|
250
|
-
result_1.should be_true
|
251
|
-
|
252
|
-
write_table.close
|
253
|
-
|
254
|
-
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
130
|
+
begin
|
131
|
+
read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
|
255
132
|
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
read_table.close
|
133
|
+
read_table.find(id).should be_nil
|
134
|
+
ensure
|
135
|
+
read_table.close unless read_table.nil?
|
136
|
+
end
|
262
137
|
|
263
138
|
end
|
264
139
|
|
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.
|
4
|
+
version: 0.1.2
|
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-
|
12
|
+
date: 2010-01-23 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -25,12 +25,14 @@ extra_rdoc_files:
|
|
25
25
|
files:
|
26
26
|
- init.rb
|
27
27
|
- lib/tokyo_wrapper/table.rb
|
28
|
+
- lib/tokyo_wrapper/table_methods/associations.rb
|
28
29
|
- lib/tokyo_wrapper.rb
|
29
30
|
- Rakefile
|
30
31
|
- README.rdoc
|
31
32
|
- spec/macros/rufus_tokyo_macros.rb
|
32
33
|
- spec/spec_helper.rb
|
33
34
|
- spec/tokyo_cabinet_files/table.tct
|
35
|
+
- spec/tokyo_wrapper/table_methods/associations_spec.rb
|
34
36
|
- spec/tokyo_wrapper/table_spec.rb
|
35
37
|
has_rdoc: true
|
36
38
|
homepage: http://github.com/tadatoshi/tokyo_wrapper
|