Class: MapFields::MappedFields

Inherits:
Object
  • Object
show all
Defined in:
lib/map_fields.rb

Instance Method Summary collapse

Constructor Details

#initialize(file, mapping, ignore_first_row, parser_options) ⇒ MappedFields

Returns a new instance of MappedFields.



113
114
115
116
117
118
119
120
121
# File 'lib/map_fields.rb', line 113

def initialize(file, mapping, ignore_first_row, parser_options)
  @file = file
  @parser_options = parser_options
  @mapping = {}
  @ignore_first_row = ignore_first_row
  mapping.each do |k,v|
    @mapping[v.to_i - 1] = k.to_i - 1 unless v.to_i == 0
  end
end

Instance Method Details

#eachObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/map_fields.rb', line 123

def each
  row_number = 1
  CsvParser.foreach(@file,@parser_options) do |csv_row|
    unless row_number == 1 && @ignore_first_row
      row = []
      @mapping.each do |k,v|
        row[k] = csv_row[v]
      end
      row.class.send(:define_method, :number) { row_number }
      yield(row)
    end
    row_number += 1
  end
end

#sizeObject



138
139
140
141
142
# File 'lib/map_fields.rb', line 138

def size
  @size = CsvParser.read(@file,@parser_options).size
  @size -= 1 if @ignore_first_row
  return @size
end