Module: Jcsv::ListReader::Header

Defined in:
lib/list_reader.rb

Overview

This module should be included if the ListReader has headers; otherwise, the HeaderLess module should be included.

Instance Method Summary collapse

Instance Method Details

#mapping=(column_mapping) ⇒ Object


When file has headers, mapping should be done through the use of a hash like data structure that responds to [] with a key. A mapping allows reordering of columns and also columns removal. A column will be removed if this columns mapping is false. When reading with dimensions, a mapping is automatically created and dimensions are mapped to true.




75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/list_reader.rb', line 75

def mapping=(column_mapping)
  
  # should allow mapping even with dimensions, but we need to be careful since
  # dimensions set a mapping and this needs to be preserved. 
  @column_mapping.mapping ||= Array.new        
  
  j = 0
  @headers.each_with_index do |h, i|
    if column_mapping[h].nil?
      @column_mapping.mapping[i] ||= j
      j += 1
    else
      raise "'true' is not allowed as a mapping: #{column_mapping}" if
        column_mapping[h] == true
      @column_mapping.mapping[i] ||= column_mapping[h]
    end
  end
  
end