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



33
34
35
36
37
# File 'lib/data_kit/csv/converter.rb', line 33

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
# File 'lib/data_kit/csv/converter.rb', line 17

def execute
  ::CSV.open(output_path, 'wb') do |writer|
    writer << csv.headers
    csv.each_row do |row|
      writer << csv.headers.collect do |field_name|
        convert(row[field_name], field_types[field_name])
      end
    end
  end
end

#field_typesObject



28
29
30
# File 'lib/data_kit/csv/converter.rb', line 28

def field_types
  @field_types ||= analysis.field_types
end