class ArcFurnace::CSVSource

Constants

COMMA

Attributes

csv[R]
delimiter[R]
file[R]
group_by[R]
group_by?[R]
key_column[R]
preprocessed_csv[R]
value[R]

Public Class Methods

new( filename: nil, csv: nil, encoding: 'UTF-8', delimiter: COMMA, group_by: false, key_column: nil ) click to toggle source
Calls superclass method
# File lib/arc-furnace/csv_source.rb, line 14
def initialize(
  filename: nil,
  csv: nil,
  encoding: 'UTF-8',
  delimiter: COMMA,
  group_by: false,
  key_column: nil
)
  @file = File.open(filename, encoding: encoding) if filename
  @csv = csv
  @delimiter = delimiter
  @preprocessed_csv = []
  @group_by = group_by
  @key_column = key_column
  super()
end

Public Instance Methods

build_enumerator() click to toggle source
# File lib/arc-furnace/csv_source.rb, line 48
def build_enumerator
  Enumerator.new do |yielder|
    if group_by?
      preprocessed_csv.each { |_, array| yielder.yield(array) }
    else
      parse_file { |row| yielder.yield(csv_to_hash_with_duplicates(row)) }
    end
  end
end
finalize() click to toggle source
# File lib/arc-furnace/csv_source.rb, line 44
def finalize
  file.close if file
end
parse_file() { |row| ... } click to toggle source
# File lib/arc-furnace/csv_source.rb, line 58
def parse_file
  (csv ? csv : CSV.new(file, { headers: true, col_sep: delimiter })).each { |row| yield row }
end
preprocess() click to toggle source

note that group_by requires the entire file to be read into memory

# File lib/arc-furnace/csv_source.rb, line 37
def preprocess
  if group_by?
    parse_file { |row| @preprocessed_csv << csv_to_hash_with_duplicates(row) }
    @preprocessed_csv = @preprocessed_csv.group_by { |row| row[key_column] }
  end
end