Class: EfoNelfo::Reader::CSV

Inherits:
Object
  • Object
show all
Defined in:
lib/efo_nelfo/reader/csv.rb

Constant Summary collapse

ENCODING =
Encoding::ISO_8859_1
CSV_OPTIONS =
{
  col_sep: ';',
  headers: false,
  row_sep: "\r\n",
  quote_char: "\x00",
  force_quotes: false,
  skip_blanks: true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ CSV

Returns a new instance of CSV.



20
21
22
23
24
25
26
27
28
# File 'lib/efo_nelfo/reader/csv.rb', line 20

def initialize(options)
  if options[:filename]
    @data = File.read(options[:filename], encoding: ENCODING)
  else
    @data = options[:data].force_encoding ENCODING
  end

  @csv = ::CSV.new @data, CSV_OPTIONS
end

Instance Attribute Details

#csvObject (readonly)

Returns the value of attribute csv.



18
19
20
# File 'lib/efo_nelfo/reader/csv.rb', line 18

def csv
  @csv
end

#dataObject (readonly)

Returns the value of attribute data.



18
19
20
# File 'lib/efo_nelfo/reader/csv.rb', line 18

def data
  @data
end

Instance Method Details

#firstObject

Returns the first row of the csv file



44
45
46
47
48
# File 'lib/efo_nelfo/reader/csv.rb', line 44

def first
  return @first if @first
  csv.rewind
  @first = csv.first
end

#headObject



50
51
52
53
54
55
# File 'lib/efo_nelfo/reader/csv.rb', line 50

def head
  return @head if @head
  klass = EfoNelfo::PostType.for first[0], first[2]
  raise EfoNelfo::UnsupportedPostType.new("Don't know how to handle v#{first[2]} of #{first[0]}") if klass.nil?
  @head = klass.new first
end

#parseObject

Parses the data and returns an EfoNelfo object of some kind



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/efo_nelfo/reader/csv.rb', line 31

def parse
  csv.each do |row|
    # Find the correct posttype module for given posttype and version
    klass = EfoNelfo::PostType.for row[0], head.version
    next if klass.nil?

    head.add klass.new(row)
  end

  head
end