lib/koine/csv/named_columns_parser.rb in koine-csv-0.2.0 vs lib/koine/csv/named_columns_parser.rb in koine-csv-0.2.1
- old
+ new
@@ -1,21 +1,24 @@
-require "csv"
+require 'csv'
module Koine
module Csv
class NamedColumnsParser < CsvParser
def parse(contents, &block)
- contents = contents.split("\n")
- titles = contents.shift
- column_names = CSV.parse(titles, col_sep: column_separator).first
+ csv = CSV.parse(contents, headers: true, col_sep: column_separator)
+ column_names = csv.headers
+ collection = []
- options = {
- column_names: column_names,
- column_separator: column_separator
- }
+ csv.each do |line|
+ element = {}
+ column_names.each do |name|
+ element[name] = line[name]
+ end
+ collection << element
+ end
- contents = contents.join("\n")
- MappedColumnsParser.new(options).parse(contents, &block)
+ return collection.each(&block) if block
+ collection
end
end
end
end