csv-utils 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/csv-utils.gemspec +1 -1
- data/lib/csv-utils.rb +1 -0
- data/lib/csv_utils/csv_extender.rb +76 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '083044bd714b955ff9d5f6a44cf6d4cb3344cf7f649b105d81a94b3ddb1c9425'
|
4
|
+
data.tar.gz: 5fa1cd9acacf10c275a23176e36722e189f8e6d8a85e1bfc7faf18eb45a1ca31
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de36fb6c80a68b33c92f3c943a665c419ac1287f5b4b2901c1a60f45964d4b13c2199f53e8dd779b2947d6c86439fa1b1b781cc5ba2225656b2d8b50f690e4cc
|
7
|
+
data.tar.gz: 0a6b5a9301f2386c2ad5bf3009ca77f949a92510ebb606844a1e6bf9fcc573f524966e68d1d8cec7db9778be3c93fb58a006fee9008e531c53a2c5391af6c0c3
|
data/csv-utils.gemspec
CHANGED
data/lib/csv-utils.rb
CHANGED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Utility class for appending data to a csv file.
|
2
|
+
class CSVUtils::CSVExtender
|
3
|
+
attr_reader :csv_file,
|
4
|
+
:new_csv_file,
|
5
|
+
:csv_options
|
6
|
+
|
7
|
+
def initialize(csv_file, new_csv_file, csv_options = {})
|
8
|
+
@csv_file = csv_file
|
9
|
+
@new_csv_file = new_csv_file
|
10
|
+
@csv_options = csv_options
|
11
|
+
end
|
12
|
+
|
13
|
+
def append(additional_headers)
|
14
|
+
process(additional_headers) do |current_headers|
|
15
|
+
while (row = src.shift)
|
16
|
+
additional_columns = yield row, current_headers
|
17
|
+
dest << (row + additional_columns)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def append_in_batches(additional_headers, batch_size = 1_000)
|
23
|
+
process(additional_headers) do |current_headers|
|
24
|
+
batch = []
|
25
|
+
|
26
|
+
process_batch_proc = Proc.new do
|
27
|
+
additional_rows = yield batch, current_headers
|
28
|
+
|
29
|
+
batch.each_with_index do |row, idx|
|
30
|
+
dest << (row + additional_rows[idx])
|
31
|
+
end
|
32
|
+
|
33
|
+
batch = []
|
34
|
+
end
|
35
|
+
|
36
|
+
while (row = src.shift)
|
37
|
+
batch << row
|
38
|
+
|
39
|
+
process_batch_proc.call if batch.size >= batch_size
|
40
|
+
end
|
41
|
+
|
42
|
+
process_batch_proc.call if batch.size > 0
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def process(additional_headers)
|
47
|
+
current_headers = append_headers(additional_headers)
|
48
|
+
|
49
|
+
yield current_headers
|
50
|
+
|
51
|
+
close
|
52
|
+
end
|
53
|
+
|
54
|
+
def src
|
55
|
+
@src ||= CSV.open(csv_file, 'rb', csv_options)
|
56
|
+
end
|
57
|
+
|
58
|
+
def dest
|
59
|
+
@dest ||= CSV.open(new_csv_file, 'wb', csv_options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def close
|
63
|
+
src.close
|
64
|
+
dest.close
|
65
|
+
end
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def append_headers(additional_headers)
|
70
|
+
return nil unless additional_headers
|
71
|
+
|
72
|
+
current_headers = src.shift
|
73
|
+
dest << (current_headers + additional_headers)
|
74
|
+
current_headers
|
75
|
+
end
|
76
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: csv-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Doug Youch
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: inheritance-helper
|
@@ -45,6 +45,7 @@ files:
|
|
45
45
|
- bin/csv-readline
|
46
46
|
- csv-utils.gemspec
|
47
47
|
- lib/csv-utils.rb
|
48
|
+
- lib/csv_utils/csv_extender.rb
|
48
49
|
- lib/csv_utils/csv_options.rb
|
49
50
|
- lib/csv_utils/csv_report.rb
|
50
51
|
- lib/csv_utils/csv_row.rb
|