tokyo_wrapper 0.1.8 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +31 -7
- data/lib/tokyo_wrapper/table.rb +22 -9
- data/spec/tokyo_wrapper/table_spec.rb +29 -0
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -76,16 +76,29 @@
|
|
76
76
|
Use when table file already exists and you want to read data from it.
|
77
77
|
It doesn't lock the table.
|
78
78
|
|
79
|
-
2-2.
|
79
|
+
2-2. Closing table.
|
80
80
|
|
81
|
-
|
81
|
+
2-2-1. Table object must be closed when all the operations are done by calling close method.
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
e.g.
|
84
|
+
|
85
|
+
begin
|
86
|
+
write_table = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt")
|
85
87
|
|
86
|
-
|
87
|
-
|
88
|
-
|
88
|
+
ensure
|
89
|
+
write_table.close unless write_table.nil?
|
90
|
+
end
|
91
|
+
|
92
|
+
2-2-2. Alternatively, you can pass a block with the operations to a create method.
|
93
|
+
It is ensured that the table is closed.
|
94
|
+
The return value is the return value from the block.
|
95
|
+
|
96
|
+
e.g. This acts same as the example for 2-2-1. above
|
97
|
+
except that the return value is the last value in the block.
|
98
|
+
|
99
|
+
TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt") do |write_table|
|
100
|
+
|
101
|
+
end
|
89
102
|
|
90
103
|
2-3. Basic operations. (Defined in TokyoWrapper::Table class itself.)
|
91
104
|
|
@@ -109,6 +122,17 @@
|
|
109
122
|
ensure
|
110
123
|
write_table.close unless write_table.nil?
|
111
124
|
end
|
125
|
+
|
126
|
+
e.g. This acts exactly same as the example above.
|
127
|
+
|
128
|
+
id = TokyoWrapper::Table.create_with_create_write_non_blocking_lock("table.txt") do |write_table|
|
129
|
+
|
130
|
+
data_hash = {"street" => "1111 Main",
|
131
|
+
"city" => "Montreal",
|
132
|
+
"notes" => "Some notes"}
|
133
|
+
write_table.add(data_hash)
|
134
|
+
|
135
|
+
end
|
112
136
|
|
113
137
|
2-3-2. Updating data.
|
114
138
|
|
data/lib/tokyo_wrapper/table.rb
CHANGED
@@ -13,21 +13,34 @@ module TokyoWrapper
|
|
13
13
|
@table = table
|
14
14
|
end
|
15
15
|
|
16
|
-
def self.create_with_create_write_non_blocking_lock(file)
|
17
|
-
|
18
|
-
self.new(table)
|
16
|
+
def self.create_with_create_write_non_blocking_lock(file, &block)
|
17
|
+
create(file, "cwf", &block)
|
19
18
|
end
|
20
19
|
|
21
|
-
def self.create_with_write_non_blocking_lock(file)
|
22
|
-
|
23
|
-
self.new(table)
|
20
|
+
def self.create_with_write_non_blocking_lock(file, &block)
|
21
|
+
create(file, "wf", &block)
|
24
22
|
end
|
25
23
|
|
26
|
-
def self.create_with_read_non_locking(file)
|
27
|
-
|
28
|
-
self.new(table)
|
24
|
+
def self.create_with_read_non_locking(file, &block)
|
25
|
+
create(file, "re", &block)
|
29
26
|
end
|
30
27
|
|
28
|
+
def self.create(file, mode, &block)
|
29
|
+
if block_given?
|
30
|
+
begin
|
31
|
+
table = Rufus::Tokyo::Table.new(File.expand_path(file), :mode => mode)
|
32
|
+
return_value_from_block = block.call(new(table))
|
33
|
+
ensure
|
34
|
+
table.close unless table.nil?
|
35
|
+
end
|
36
|
+
return_value_from_block
|
37
|
+
else
|
38
|
+
new(Rufus::Tokyo::Table.new(File.expand_path(file), :mode => mode))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private_class_method :new, :create
|
43
|
+
|
31
44
|
def close
|
32
45
|
@table.close
|
33
46
|
end
|
@@ -338,4 +338,33 @@ describe TokyoWrapper::Table do
|
|
338
338
|
|
339
339
|
end
|
340
340
|
|
341
|
+
context "Taking care of closing table after the given block is executed" do
|
342
|
+
|
343
|
+
it "should add data by providing the code to do so in the block" do
|
344
|
+
|
345
|
+
id = TokyoWrapper::Table.create_with_create_write_non_blocking_lock(@table_file) do |write_table|
|
346
|
+
|
347
|
+
data_hash = {"street" => "1111 Main",
|
348
|
+
"city" => "Montreal",
|
349
|
+
"notes" => "Some notes"}
|
350
|
+
write_table.add(data_hash)
|
351
|
+
|
352
|
+
end
|
353
|
+
|
354
|
+
TokyoWrapper::Table.create_with_read_non_locking(@table_file) do |read_table|
|
355
|
+
|
356
|
+
read_table.all.should == [{:pk => id.to_s,
|
357
|
+
"street" => "1111 Main",
|
358
|
+
"city" => "Montreal",
|
359
|
+
"notes" => "Some notes"}]
|
360
|
+
|
361
|
+
read_table.find(id).should == {"street" => "1111 Main",
|
362
|
+
"city" => "Montreal",
|
363
|
+
"notes" => "Some notes"}
|
364
|
+
|
365
|
+
end
|
366
|
+
|
367
|
+
end
|
368
|
+
end
|
369
|
+
|
341
370
|
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.
|
4
|
+
version: 0.1.9
|
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-02-
|
12
|
+
date: 2010-02-20 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|