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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ecb75f60c8e9b9db4cc3eb0e4ca3a0ac53aad67726ed995b7e8c341cd0dc76a3
4
- data.tar.gz: 5138b5cc82eec0b7667c9e3435c2662bbfa1de51e469582372a158b943e57d7f
3
+ metadata.gz: '083044bd714b955ff9d5f6a44cf6d4cb3344cf7f649b105d81a94b3ddb1c9425'
4
+ data.tar.gz: 5fa1cd9acacf10c275a23176e36722e189f8e6d8a85e1bfc7faf18eb45a1ca31
5
5
  SHA512:
6
- metadata.gz: 1a7d685b0db28805833596b32793fca968c6a5a1f57346223b487579622423d0151a122253c69806e377015fe0cf9cf02381bafbee37cb0ba54fa290a857c1cc
7
- data.tar.gz: 7c572f9e7c74d626084612afa188bb15b036377bbac16ed5374d7fcff300e58fab8100a77729c2fa428e4294ac6b3b643feb00b2e87e7de78883c8548193de54
6
+ metadata.gz: de36fb6c80a68b33c92f3c943a665c419ac1287f5b4b2901c1a60f45964d4b13c2199f53e8dd779b2947d6c86439fa1b1b781cc5ba2225656b2d8b50f690e4cc
7
+ data.tar.gz: 0a6b5a9301f2386c2ad5bf3009ca77f949a92510ebb606844a1e6bf9fcc573f524966e68d1d8cec7db9778be3c93fb58a006fee9008e531c53a2c5391af6c0c3
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'csv-utils'
5
- s.version = '0.2.0'
5
+ s.version = '0.2.1'
6
6
  s.licenses = ['MIT']
7
7
  s.summary = 'CSV Utils'
8
8
  s.description = 'Tools for debugging malformed CSV files'
@@ -2,6 +2,7 @@ require 'csv'
2
2
 
3
3
  # Collection of tools for working with CSV files.
4
4
  module CSVUtils
5
+ autoload :CSVExtender, 'csv_utils/csv_extender'
5
6
  autoload :CSVOptions, 'csv_utils/csv_options'
6
7
  autoload :CSVReport, 'csv_utils/csv_report'
7
8
  autoload :CSVRow, 'csv_utils/csv_row'
@@ -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.0
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-06-23 00:00:00.000000000 Z
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