Total Complexity | 3 |
Total Lines | 27 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | require "csv" |
||
5 | class MappedColumnsParser < CsvParser |
||
6 | attr_reader :column_names |
||
7 | |||
8 | def initialize(options = {}) |
||
9 | super(options) |
||
10 | |||
11 | @column_names = options.fetch(:column_names) |
||
12 | end |
||
13 | |||
14 | def parse(contents, &block) |
||
15 | mapped = [] |
||
16 | CSV.parse(contents, parser_options) do |row| |
||
17 | new_row = {} |
||
18 | |||
19 | row.each_with_index do |value, index| |
||
20 | new_row[column_name(index)] = value |
||
21 | end |
||
22 | |||
23 | mapped << new_row |
||
24 | end |
||
25 | mapped |
||
26 | end |
||
27 | |||
28 | def column_name(index) |
||
29 | column_names.fetch(index) |
||
30 | end |
||
31 | end |
||
32 | end |
||
34 |