Module: CsvRowModel::Import::FileModel
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/csv_row_model/import/file_model.rb
Class Method Summary collapse
-
.header_matchers ⇒ Array
Header_matchs matchers for the row model.
-
.index_header_match(cell) ⇒ Integer
Safe to override.
- .next(csv, source_header, context = {}, previous = nil) ⇒ Object
Class Method Details
.header_matchers ⇒ Array
Returns header_matchs matchers for the row model.
22 23 24 25 26 27 28 29 |
# File 'lib/csv_row_model/import/file_model.rb', line 22 def header_matchers @header_matchers ||= begin columns.map do |name, | matchers = [:header_matchs] || [name.to_s] Regexp.new(matchers.join('|'),Regexp::IGNORECASE) end end end |
.index_header_match(cell) ⇒ Integer
Safe to override
14 15 16 17 18 19 |
# File 'lib/csv_row_model/import/file_model.rb', line 14 def index_header_match(cell) match = header_matchers.each_with_index.select do |matcher, index| cell.match(matcher) end.first match ? match[1] : nil end |
.next(csv, source_header, context = {}, previous = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/csv_row_model/import/file_model.rb', line 31 def next(csv, source_header, context={}, previous=nil) return csv.read_row unless csv.next_row source_row = Array.new(header_matchers.size) while csv.next_row current_row = csv.read_row current_row.each_with_index do |cell, position| next if cell.blank? index = index_header_match(cell) next unless index source_row[index] = current_row[position + 1] break end end new(source_row, source_header: source_header, context: context, previous: previous) end |