for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
require "csv"
module Koine
module Csv
class MappedColumnsParser < CsvParser
attr_reader :column_names
def initialize(options = {})
@column_names = options.fetch(:column_names)
options.delete(:column_names)
super(options)
end
def parse(contents, &block)
mapped = []
CSV.parse(contents, parser_options) do |row|
new_row = {}
row.each_with_index do |value, index|
new_row[column_name(index)] = value
mapped << new_row
block_given? ? mapped.each(&block) : mapped
private
def column_name(index)
column_names.fetch(index)