Class: CSVUtils::CSVExtender

Inherits:
Object
  • Object
show all
Defined in:
lib/csv_utils/csv_extender.rb

Overview

Utility class for appending data to a csv file.

Instance Method Summary collapse

Constructor Details

#initialize(src_csv, dest_csv, csv_options = {}) ⇒ CSVExtender

Returns a new instance of CSVExtender.



3
4
5
6
# File 'lib/csv_utils/csv_extender.rb', line 3

def initialize(src_csv, dest_csv, csv_options = {})
  @src_csv = CSVUtils::CSVWrapper.new(src_csv, 'rb', csv_options)
  @dest_csv = CSVUtils::CSVWrapper.new(dest_csv, 'wb', csv_options)
end

Instance Method Details

#append(additional_headers) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/csv_utils/csv_extender.rb', line 8

def append(additional_headers)
  process(additional_headers) do |current_headers|
    while (row = @src_csv.shift)
      additional_columns = yield row, current_headers
      @dest_csv << (row + additional_columns)
    end
  end
end

#append_in_batches(additional_headers, batch_size = 1_000) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/csv_utils/csv_extender.rb', line 17

def append_in_batches(additional_headers, batch_size = 1_000)
  process(additional_headers) do |current_headers|
    batch = []

    process_batch_proc = Proc.new do
      additional_rows = yield batch, current_headers

      batch.each_with_index do |row, idx|
        @dest_csv << (row + additional_rows[idx])
      end

      batch = []
    end

    while (row = @src_csv.shift)
      batch << row

      process_batch_proc.call if batch.size >= batch_size
    end

    process_batch_proc.call if batch.size > 0
  end
end