Class: DataKit::CSV::Converter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(csv, analysis, output_path) ⇒ Converter

Returns a new instance of Converter.



11
12
13
14
15
# File 'lib/data_kit/csv/converter.rb', line 11

def initialize(csv, analysis, output_path)
  @csv = csv
  @analysis = analysis
  @output_path = File.expand_path(output_path)
end

Instance Attribute Details

#analysisObject

Returns the value of attribute analysis.



8
9
10
# File 'lib/data_kit/csv/converter.rb', line 8

def analysis
  @analysis
end

#csvObject

Returns the value of attribute csv.



7
8
9
# File 'lib/data_kit/csv/converter.rb', line 7

def csv
  @csv
end

#output_pathObject

Returns the value of attribute output_path.



9
10
11
# File 'lib/data_kit/csv/converter.rb', line 9

def output_path
  @output_path
end

Class Method Details

.convert(csv, analysis, output_path) ⇒ Object



37
38
39
40
41
# File 'lib/data_kit/csv/converter.rb', line 37

def convert(csv, analysis, output_path)
  converter = new(csv, analysis, output_path)
  converter.execute
  converter
end

Instance Method Details

#executeObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/data_kit/csv/converter.rb', line 17

def execute
  ::CSV.open(output_path, 'wb') do |writer|
    first = true
    converted = []
    csv.each_row do |row|
      if first
        first = false
        writer << csv.headers
      end
      
      writer << convert_row(csv.headers, row)
    end
  end
end

#field_typesObject



32
33
34
# File 'lib/data_kit/csv/converter.rb', line 32

def field_types
  @field_types ||= analysis.field_types
end