pcsv 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/pcsv.rb +16 -4
- data/lib/pcsv/version.rb +1 -1
- metadata +1 -1
data/lib/pcsv.rb
CHANGED
@@ -22,19 +22,26 @@ class PCSV
|
|
22
22
|
|
23
23
|
# Performs a given action on each cell of a CSV file.
|
24
24
|
def self.process(action, path, options={})
|
25
|
+
options[:headers] = true
|
25
26
|
thread_count = options.delete(:thread_count) || 10
|
27
|
+
if_proc = options.delete(:if)
|
26
28
|
|
27
29
|
# Open CSV & build a worker queue.
|
28
30
|
csv = CSV.read(path, options)
|
29
31
|
queue = []
|
32
|
+
headers = nil
|
30
33
|
csv.each_with_index do |row, row_index|
|
34
|
+
headers ||= csv.headers
|
35
|
+
|
31
36
|
row.fields.each_with_index do |field, col_index|
|
32
|
-
|
37
|
+
item = {
|
38
|
+
row:row,
|
33
39
|
row_index:row_index,
|
34
40
|
col_index:col_index,
|
35
|
-
|
36
|
-
|
41
|
+
value:field.to_s,
|
42
|
+
header:headers[col_index]
|
37
43
|
}
|
44
|
+
queue << item
|
38
45
|
end
|
39
46
|
end
|
40
47
|
|
@@ -53,6 +60,7 @@ class PCSV
|
|
53
60
|
|
54
61
|
# Invoke the block with the row info.
|
55
62
|
begin
|
63
|
+
next if if_proc.nil? || !if_proc.call(item)
|
56
64
|
result = yield item, mutex
|
57
65
|
|
58
66
|
if action == :map
|
@@ -68,7 +76,11 @@ class PCSV
|
|
68
76
|
end
|
69
77
|
end
|
70
78
|
|
71
|
-
|
79
|
+
begin
|
80
|
+
threads.each { |t| t.join }
|
81
|
+
rescue SystemExit, Interrupt
|
82
|
+
threads.each { |thread| thread.kill }
|
83
|
+
end
|
72
84
|
|
73
85
|
return csv
|
74
86
|
end
|
data/lib/pcsv/version.rb
CHANGED