Class: CSV::AutoParser

Inherits:
Object
  • Object
show all
Defined in:
lib/csv/autoparser.rb,
lib/csv/autoparser/version.rb

Defined Under Namespace

Classes: HeaderRowNotFound, Row

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, &is_header) ⇒ AutoParser

Returns a new instance of AutoParser.

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/csv/autoparser.rb', line 27

def initialize file, &is_header
  map = {}
  csv_line_number = 0
  @rows = []
  @pre_header_rows = []
  CSV.foreach(file) do |row| 
    csv_line_number += 1
    if map.empty?
      if is_header.call(csv_line_number, row)
        row.each_index {|index| map[row[index]] = index } 
        Row.define_methods(map)
      else
        @pre_header_rows << row
      end
    else
      @rows << Row.new(row)
    end
  end
  raise HeaderRowNotFound, "Could not find header row in #{file}." if map.empty?
end

Instance Attribute Details

#pre_header_rowsObject (readonly)

Returns the value of attribute pre_header_rows.



25
26
27
# File 'lib/csv/autoparser.rb', line 25

def pre_header_rows
  @pre_header_rows
end

#rowsObject (readonly)

Returns the value of attribute rows.



25
26
27
# File 'lib/csv/autoparser.rb', line 25

def rows
  @rows
end