Class: MapFields::MappedFields
- Inherits:
-
Object
- Object
- MapFields::MappedFields
- Defined in:
- lib/map_fields.rb
Instance Method Summary collapse
- #each ⇒ Object
-
#initialize(file, mapping, ignore_first_row, parser_options) ⇒ MappedFields
constructor
A new instance of MappedFields.
- #size ⇒ Object
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, ) @file = file @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
#each ⇒ Object
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 |
#size ⇒ Object
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 |