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.



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

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

Instance Attribute Details

#csv_parserObject (readonly)

Returns the value of attribute csv_parser.



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

def csv_parser
  @csv_parser
end

Instance Method Details

#parse(row) ⇒ Object

Returns [Array] the parsed CSV line



22
23
24
25
26
27
28
# File 'lib/io_streams/tabular/parser/csv.rb', line 22

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)

  csv_parser.parse(row)
end

#parse_header(row) ⇒ Object

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



13
14
15
16
17
18
19
# File 'lib/io_streams/tabular/parser/csv.rb', line 13

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

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

  csv_parser.parse(row)
end

#render(row, header) ⇒ Object

Return the supplied array as a single line CSV string.



31
32
33
34
# File 'lib/io_streams/tabular/parser/csv.rb', line 31

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