Class: IOStreams::Tabular::Parser::Csv
- Defined in:
- lib/io_streams/tabular/parser/csv.rb
Instance Attribute Summary collapse
-
#csv_parser ⇒ Object
readonly
Returns the value of attribute csv_parser.
Instance Method Summary collapse
-
#initialize ⇒ Csv
constructor
A new instance of Csv.
-
#parse(row) ⇒ Object
Returns [Array] the parsed CSV line.
-
#parse_header(row) ⇒ Object
Returns [Array<String>] the header row.
-
#render(row, header) ⇒ Object
Return the supplied array as a single line CSV string.
Methods inherited from Base
Constructor Details
Instance Attribute Details
#csv_parser ⇒ Object (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 |