Class: CsvImportAnalyzer::CsvDatatypeAnalysis

Inherits:
Object
  • Object
show all
Includes:
DatatypeValidator, Helper
Defined in:
lib/csv-import-analyzer/csv_datatype_analysis.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DatatypeValidator

#validate_field

Methods included from Helper

#null_like?

Constructor Details

#initialize(options) ⇒ CsvDatatypeAnalysis

Returns a new instance of CsvDatatypeAnalysis.



15
16
17
18
19
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 15

def initialize(options)
  @options = options
  @csv_column_datatypes = {}
  @nullable = []
end

Instance Attribute Details

#csv_column_datatypesObject

Returns the value of attribute csv_column_datatypes.



13
14
15
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 13

def csv_column_datatypes
  @csv_column_datatypes
end

#nullableObject

Returns the value of attribute nullable.



13
14
15
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 13

def nullable
  @nullable
end

Instance Method Details

#datatype_analysisObject

Process a chunk of csv file for all possible datatypes towards each column in the row This datatype analysis is used for analyzing,

Min - Max values of each column
Distinct values of each column
Enumeration eligibility


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 35

def datatype_analysis
  SmarterCSV.process(filename, {:col_sep => delimiter, :chunk_size => chunk_size, 
    :remove_empty_values => false, :remove_zero_values => false}) do |chunk|
    chunk.each do |row|
      row.each do |key, value|
        unless null_like?(value)
          datatype = determine_dataype(value)
          add_to_datatype(key, datatype.to_sym)
        else             
          nullable.push(key) unless nullable.include?(key)
        end
      end
    end
    break
  end
  options[:csv_datatype_analysis] = csv_column_datatypes.clone # To retain the current state of csv_column_datatypes since it's altered further
  finalize_datatypes_for_csv
  options[:csv_column_datatypes] = csv_column_datatypes
  options[:nullable] = nullable
  take_further_actions
end

#filenameObject



25
26
27
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 25

def filename
  @options[:filename]
end

#optionsObject



21
22
23
# File 'lib/csv-import-analyzer/csv_datatype_analysis.rb', line 21

def options
  @options
end