Class: CSVConv::Converter
- Inherits:
-
Object
- Object
- CSVConv::Converter
- Defined in:
- lib/csvconv/converter.rb
Overview
Converter from CSV
Instance Method Summary collapse
- #convert(input) ⇒ Object
- #convert_stream(input, output) ⇒ Object
-
#initialize(format, options) ⇒ Converter
constructor
A new instance of Converter.
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, ) @format = format @sep = [:sep] || ',' @header = [: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 |