Class: CsvReader::Parser

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

Instance Method Summary collapse

Constructor Details

#initialize(csv_data) ⇒ Parser

Returns a new instance of Parser.



3
4
5
6
# File 'lib/csv_reader/parser.rb', line 3

def initialize(csv_data)
  @csv_data = csv_data
  parse
end

Instance Method Details

#data_rowsObject



20
21
22
23
24
25
26
# File 'lib/csv_reader/parser.rb', line 20

def data_rows
  rows = []
  1.upto(@rows.length-1).each do |i|
    rows << @rows[i].split(",").map{|x| x.split.first}
  end
  rows
end

#headersObject



16
17
18
# File 'lib/csv_reader/parser.rb', line 16

def headers
  @rows[0].split(",").map{|x| x.split.first}
end

#parseObject



8
9
10
# File 'lib/csv_reader/parser.rb', line 8

def parse
  @rows = @csv_data.split("\n").reject{|x| x.split == []}
end

#rowsObject



12
13
14
# File 'lib/csv_reader/parser.rb', line 12

def rows
  @rows
end