Skip to content

Commit 4ec1c2c

Browse files
committed
read columns based on the order of dcolumn definitions and not order from csv file
1 parent d9ade95 commit 4ec1c2c

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

lib/csv_importer/column.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ class Column
66

77
attribute :name, String
88
attribute :definition, ColumnDefinition
9+
attribute :rank, Integer, default: 0
910
end
1011
end

lib/csv_importer/header.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ class Header
88
attribute :column_names, Array[String]
99

1010
def columns
11+
max_column = column_names.size
12+
1113
column_names.map do |column_name|
1214
# ensure column name escapes invisible characters
1315
column_name = column_name.gsub(/\P{Print}|\p{Cf}/, '')
16+
rank = column_definitions.index { |definition| definition.match?(column_name) }
1417

1518
Column.new(
1619
name: column_name,
17-
definition: find_column_definition(column_name)
20+
definition: find_column_definition(column_name),
21+
rank: rank || max_column
1822
)
1923
end
2024
end

lib/csv_importer/row.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def csv_attributes
3333

3434
# Set attributes
3535
def set_attributes(model)
36-
header.columns.each do |column|
36+
header.columns.sort_by(&:rank).each do |column|
3737
value = csv_attributes[column.name]
3838
begin
3939
value = value.dup if value

0 commit comments

Comments
 (0)