Class: CSVConv::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/csvconv/converter.rb

Overview

Converter from CSV

Instance Method Summary collapse

Constructor Details

#initialize(format, options) ⇒ Converter

Returns a new instance of Converter.



4
5
6
7
8
# File 'lib/csvconv/converter.rb', line 4

def initialize(format, options)
  @format = format
  @sep = options[:sep] || ','
  @header = options[:header]
end

Instance Method Details

#convert(input) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/csvconv/converter.rb', line 10

def convert(input)
  @header ||= Parser.read_header(input, @sep)
  hash_array = []
  while (line = input.gets)
    hash_array << Parser.parse_line(line, @header, @sep)
  end
  Formatter.send(@format, hash_array)
end

#convert_stream(input, output) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/csvconv/converter.rb', line 19

def convert_stream(input, output)
  @header ||= Parser.read_header(input, @sep)
  while (line = input.gets)
    hash = Parser.parse_line(line, @header, @sep)
    output.puts Formatter.send(@format, [hash])
  end
end