Sha256: 5845bfce82f89b22fcbaad0eb1cb2666f786c69d6c7bf4fd741e29825a017452

Contents?: true

Size: 1.01 KB

Versions: 2

Compression:

Stored size: 1.01 KB

Contents

module IOStreams
  class Tabular
    module Parser
      class Csv < Base
        attr_reader :csv_parser

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

        # Returns [Array<String>] the header row.
        # Returns nil if the row is blank.
        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

        # Returns [Array] the parsed CSV line
        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

        # Return the supplied array as a single line CSV string.
        def render(row, header)
          array = header.to_array(row)
          csv_parser.to_csv(array)
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
iostreams-0.16.1 lib/io_streams/tabular/parser/csv.rb
iostreams-0.16.0 lib/io_streams/tabular/parser/csv.rb