Class: IOStreams::Tabular::Parser::Csv

Inherits:
Base
  • Object
show all
Defined in:
lib/io_streams/tabular/parser/csv.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#requires_header?

Constructor Details

#initializeCsv

Returns a new instance of Csv.



9
10
11
# File 'lib/io_streams/tabular/parser/csv.rb', line 9

def initialize
  @csv_parser = Utility::CSVRow.new
end

Instance Attribute Details

#csv_parserObject (readonly)

Returns the value of attribute csv_parser.



6
7
8
# File 'lib/io_streams/tabular/parser/csv.rb', line 6

def csv_parser
  @csv_parser
end

Instance Method Details

#parse(row) ⇒ Object

Returns [Array] the parsed CSV line



27
28
29
30
31
32
33
# File 'lib/io_streams/tabular/parser/csv.rb', line 27

def parse(row)
  return row if row.is_a?(::Array)

  raise(IOStreams::Errors::TypeMismatch, "Format is :csv. Invalid input: #{row.class.name}") unless row.is_a?(String)

  parse_line(row)
end

#parse_header(row) ⇒ Object

Returns [Array<String>] the header row. Returns nil if the row is blank.



16
17
18
19
20
21
22
23
24
# File 'lib/io_streams/tabular/parser/csv.rb', line 16

def parse_header(row)
  return row if row.is_a?(::Array)

  unless row.is_a?(String)
    raise(IOStreams::Errors::InvalidHeader, "Format is :csv. Invalid input header: #{row.class.name}")
  end

  parse_line(row)
end

#render(row, header) ⇒ Object

Return the supplied array as a single line CSV string.



36
37
38
39
# File 'lib/io_streams/tabular/parser/csv.rb', line 36

def render(row, header)
  array = header.to_array(row)
  render_array(array)
end