Class: Itiel::Load::CSVFile

Inherits:
Object
  • Object
show all
Includes:
ChainedStep, Nameable
Defined in:
lib/itiel/load/csv_file.rb

Overview

Loads the data stream into a CSV file

Usage:

@csv_file = Itiel::Load::CSVFile.new('filename.csv')
@csv_file.input = []

Instance Attribute Summary

Attributes included from Nameable

#debug, #step_name

Instance Method Summary collapse

Methods included from ChainedStep

included

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