Class: Itiel::Load::CSVFile
- Inherits:
-
Object
- Object
- Itiel::Load::CSVFile
- Includes:
- ChainedStep, Nameable
- Defined in:
- lib/itiel/load/csv_file.rb
Overview
Instance Attribute Summary
Attributes included from Nameable
Instance Method Summary collapse
-
#initialize(file_name, append = true) ⇒ CSVFile
constructor
A new instance of CSVFile.
- #persist(input_stream) ⇒ Object
Methods included from ChainedStep
Constructor Details
#initialize(file_name, append = true) ⇒ CSVFile
Returns a new instance of CSVFile.
17 18 19 20 |
# File 'lib/itiel/load/csv_file.rb', line 17 def initialize(file_name, append=true) @append = append @file_name = file_name end |
Instance Method Details
#persist(input_stream) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/itiel/load/csv_file.rb', line 22 def persist(input_stream) headers = input_stream.collect(&:keys).flatten.uniq mode = @append ? "ab" : "w" skip_headers = skip_headers? CSV.open(@file_name, mode) do |csv| csv << headers unless skip_headers input_stream.each do |row| csv_row = [] headers.each do |h| csv_row << row[h] end csv << csv_row end end end |