tokyo_wrapper 0.1.0 → 0.1.1

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.
@@ -27,18 +27,30 @@ module TokyoWrapper
27
27
 
28
28
  def add(params = {})
29
29
  id = @table.generate_unique_id
30
- @table[id] = params
30
+ @table[id] = convert_params(params)
31
31
  id
32
32
  end
33
33
 
34
34
  def update(id, params)
35
35
  if !@table[id.to_s].nil? && !@table[id.to_s].empty?
36
- @table[id.to_s] = @table[id.to_s].merge(params)
36
+ @table[id.to_s] = @table[id.to_s].merge(convert_params(params))
37
37
  true
38
38
  else
39
39
  false
40
40
  end
41
41
  end
42
+
43
+ def add_association_id(id, association_id_name, association_id)
44
+ if !@table[id.to_s].nil? && !@table[id.to_s].empty?
45
+ association_ids_string = @table[id.to_s]["#{association_id_name}s"]
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
42
54
 
43
55
  def all
44
56
  @table.query
@@ -48,6 +60,19 @@ module TokyoWrapper
48
60
  @table[id.to_s]
49
61
  end
50
62
 
63
+ private
64
+ #
65
+ # rufus-tokyo converts array to string without a delimiter e.g. It converts [1,2,3,4] to "1234".
66
+ # So the array needs to be converted to the comma separated string before being added. e.g. "1,2,3,4"
67
+ #
68
+ def convert_params(params)
69
+ params.each do |key, value|
70
+ if value.instance_of?(Array)
71
+ params[key] = value.join(",")
72
+ end
73
+ end
74
+ end
75
+
51
76
  end
52
77
 
53
78
  end
@@ -14,29 +14,29 @@ describe TokyoWrapper::Table do
14
14
 
15
15
  context "Add" do
16
16
 
17
- before(:each) do
18
- @table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
19
- end
20
-
21
- after(:each) do
22
- @table.close
23
- end
24
-
25
17
  it "should add data" do
18
+
19
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
26
20
 
27
21
  data_hash = {"street" => "1111 Main",
28
22
  "city" => "Montreal",
29
23
  "notes" => "Some notes"}
30
- id = @table.add(data_hash)
24
+ id = write_table.add(data_hash)
25
+
26
+ write_table.close
27
+
28
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
31
29
 
32
- @table.all.should == [{:pk => id.to_s,
33
- "street" => "1111 Main",
34
- "city" => "Montreal",
35
- "notes" => "Some notes"}]
36
-
37
- @table.find(id).should == {"street" => "1111 Main",
30
+ read_table.all.should == [{:pk => id.to_s,
31
+ "street" => "1111 Main",
38
32
  "city" => "Montreal",
39
- "notes" => "Some notes"}
33
+ "notes" => "Some notes"}]
34
+
35
+ read_table.find(id).should == {"street" => "1111 Main",
36
+ "city" => "Montreal",
37
+ "notes" => "Some notes"}
38
+
39
+ read_table.close
40
40
 
41
41
  end
42
42
 
@@ -44,31 +44,221 @@ describe TokyoWrapper::Table do
44
44
 
45
45
  context "Update" do
46
46
 
47
- before(:each) do
48
- @table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
47
+ it "should update data" do
48
+
49
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
50
+
51
+ data_hash = {"street" => "1111 Main",
52
+ "city" => "Montreal",
53
+ "notes" => "Some notes"}
54
+ id = write_table.add(data_hash)
55
+
56
+ write_table.close
57
+
58
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
59
+
60
+ read_table.find(id).should == {"street" => "1111 Main",
61
+ "city" => "Montreal",
62
+ "notes" => "Some notes"}
63
+
64
+ read_table.close
65
+
66
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
67
+
68
+ result = write_table.update(id, "notes" => "Recently situation has been changed.")
69
+ result.should be_true
70
+
71
+ write_table.close
72
+
73
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
74
+
75
+ read_table.find(id).should == {"street" => "1111 Main",
76
+ "city" => "Montreal",
77
+ "notes" => "Recently situation has been changed."}
78
+
79
+ read_table.close
80
+
49
81
  end
50
82
 
51
- after(:each) do
52
- @table.close
53
- end
83
+ end
84
+
85
+ context "One-to-many and many-to-many associations" do
54
86
 
55
- it "should update data" do
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
102
+
103
+ it "should add data with association" do
104
+
105
+ write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file)
56
106
 
57
107
  data_hash = {"street" => "1111 Main",
58
108
  "city" => "Montreal",
59
- "notes" => "Some notes"}
60
- id = @table.add(data_hash)
61
-
62
- @table.find(id).should == {"street" => "1111 Main",
109
+ "notes" => "Some notes",
110
+ "sector_ids" => ["2","5","34","8"]}
111
+ id = write_table.add(data_hash)
112
+
113
+ write_table.close
114
+
115
+ read_table = TokyoWrapper::Table.create_with_read_non_locking(@table_file)
116
+
117
+ read_table.all.should == [{:pk => id.to_s,
118
+ "street" => "1111 Main",
63
119
  "city" => "Montreal",
64
- "notes" => "Some notes"}
65
-
66
- result = @table.update(id, "notes" => "Recently situation has been changed.")
120
+ "notes" => "Some notes",
121
+ "sector_ids" => "2,5,34,8"}]
122
+
123
+ read_table.find(id).should == {"street" => "1111 Main",
124
+ "city" => "Montreal",
125
+ "notes" => "Some notes",
126
+ "sector_ids" => "2,5,34,8"}
127
+
128
+ read_table.close
129
+
130
+ end
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)
67
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"])
68
156
  result.should be_true
69
- @table.find(id).should == {"street" => "1111 Main",
70
- "city" => "Montreal",
71
- "notes" => "Recently situation has been changed."}
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)
255
+
256
+ read_table.find(id).should == {"street" => "1111 Main",
257
+ "city" => "Montreal",
258
+ "notes" => "Some notes",
259
+ "sector_ids" => "2,5,34,8"}
260
+
261
+ read_table.close
72
262
 
73
263
  end
74
264
 
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.0
4
+ version: 0.1.1
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-17 00:00:00 -05:00
12
+ date: 2010-01-20 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15