Class: Csv2Psql::SchemaGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/csv2psql/schema/schema_generator.rb

Overview

Csv2Psql schema generator class

Class Method Summary collapse

Class Method Details

.format_result(analysis, lines) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/csv2psql/schema/schema_generator.rb', line 33

def format_result(analysis, lines)
  res = { columns: {} }
  analysis.each do |k, v|
    res[:columns][k] = {
      type: v[:class].sql_type,
      null: v[:results][:count] != lines
    }
  end
  res
end

.generate(analysis, _opts = {}) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/csv2psql/schema/schema_generator.rb', line 44

def generate(analysis, _opts = {})
  res = {}
  analysis[:columns].each do |name, analyzers|
    analyzer = select_best(analyzers, analysis[:lines])
    res[name] = analyzer
  end
  format_result(res, analysis[:lines])
end

.select_analyzers_by_match(analyzers, match) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/csv2psql/schema/schema_generator.rb', line 7

def select_analyzers_by_match(analyzers, match)
  null_count = analyzers['Null'][:results][:count]
  analyzers.select do |_k, v|
    next if _k != 'String' && v[:results][:count] == 0
    v[:results][:count] + null_count == match
  end
end

.select_analyzers_class(analyzers, class_name) ⇒ Object



15
16
17
# File 'lib/csv2psql/schema/schema_generator.rb', line 15

def select_analyzers_class(analyzers, class_name)
  analyzers.select { |_k, v| v[:class].sql_class?(class_name) }
end

.select_best(in_analyzers, lines) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/csv2psql/schema/schema_generator.rb', line 19

def select_best(in_analyzers, lines)
  analyzers = select_analyzers_by_match(in_analyzers, lines)

  # matched_analyzers = analyzers.select do |name, analyzer|
  #   analyzer[:results][:count] === lines
  # end

  sorted = analyzers.sort do |a, b|
    a[1][:class].weight <=> b[1][:class].weight
  end

  analyzers[sorted.last[0]]
end