class CSVUtils::CSVTransformer

Transforms a CSV given a series of steps

Attributes

headers[R]

Public Class Methods

new(src_csv, dest_csv, csv_options = {}) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 5
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

Public Instance Methods

additional_data(&block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 15
def additional_data(&block)
  steps << [:additional_data, @headers, block]
  self
end
append(additional_headers, &block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 36
def append(additional_headers, &block)
  steps << [:append, @headers, block]

  if additional_headers
    @headers += additional_headers
  else
    @headers = nil
  end

  self
end
each(&block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 48
def each(&block)
  steps << [:each, @headers, block]
  self
end
map(new_headers, &block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 30
def map(new_headers, &block)
  steps << [:map, @headers, block]
  @headers = new_headers
  self
end
process(batch_size = 10_000, &block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 58
def process(batch_size = 10_000, &block)
  batch = []

  @dest_csv << @headers if @headers

  steps_proc = Proc.new do
    steps.each do |step_type, current_headers, proc|
      batch = process_step(step_type, current_headers, batch, &proc)
    end

    batch.each { |row| @dest_csv << row }

    batch = []
  end
                 
  while (row = @src_csv.shift)
    batch << row
    steps_proc.call if batch.size >= batch_size
  end

  steps_proc.call if batch.size > 0

  @src_csv.close
  @dest_csv.close
end
read_headers() click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 10
def read_headers
  @headers = @src_csv.shift
  self
end
reject(&block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 25
def reject(&block)
  steps << [:reject, @headers, block]
  self
end
select(&block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 20
def select(&block)
  steps << [:select, @headers, block]
  self
end
set_headers(headers) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 53
def set_headers(headers)
  @headers = headers
  self
end

Private Instance Methods

process_step(step_type, current_headers, batch, &block) click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 91
def process_step(step_type, current_headers, batch, &block)
  case step_type
  when :select
    batch.select! do |row|
      block.call row, current_headers, @additional_data
    end
  when :reject
    batch.reject! do |row|
      block.call row, current_headers, @additional_data
    end
  when :map
    batch.map! do |row|
      block.call row, current_headers, @additional_data
    end
  when :append
    batch.map! do |row|
      row + block.call(row, current_headers, @additional_data)
    end
  when :additional_data
    @additional_data = block.call(batch, current_headers)
  when :each
    batch.each do |row|
      block.call(row, current_headers, @additional_data)
    end
  end

  batch
end
steps() click to toggle source
# File lib/csv_utils/csv_transformer.rb, line 86
def steps
  @steps ||= []
end