Class: Csv2Psql::Analyzer

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

Overview

Analyzer file analyzer class

Constant Summary collapse

DEFAULT_OPTIONS =

rubocop:disable Metrics/ClassLength

{}
ANALYZERS_DIR =
File.join(File.dirname(__FILE__), 'types')
EXCLUDED_ANALYZERS =
['base_analyzer']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cache = nil) ⇒ Analyzer

Returns a new instance of Analyzer.



18
19
20
21
22
# File 'lib/csv2psql/analyzer/analyzer.rb', line 18

def initialize(cache = nil)
  @files = {}
  @cache = cache
  @analyzers = load_analyzers
end

Instance Attribute Details

#analyzersObject (readonly)

Returns the value of attribute analyzers.



16
17
18
# File 'lib/csv2psql/analyzer/analyzer.rb', line 16

def analyzers
  @analyzers
end

#filesObject (readonly)

Returns the value of attribute files.



16
17
18
# File 'lib/csv2psql/analyzer/analyzer.rb', line 16

def files
  @files
end

Instance Method Details

#analyze(path, row, opts = {}) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/csv2psql/analyzer/analyzer.rb', line 24

def analyze(path, row, opts = {})
  data = get_data(path)

  header = CsvHelper.get_header(row, opts)
  analyze_row(header, row, data)

  data[:lines] = data[:lines] + 1
end

#analyze_column(analyzer, val, opts = { use_cache: false }) ⇒ Object



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

def analyze_column(analyzer, val, opts = { use_cache: false })
  if opts[:use_cache]
    res = cached_result(val) do
      analyzer[:class].analyze(val)
    end
  else
    res = analyzer[:class].analyze(val)
  end

  update_results(analyzer, res, val) if res
end

#analyze_row(header, row, data) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/csv2psql/analyzer/analyzer.rb', line 45

def analyze_row(header, row, data)
  header.each do |h|
    col = get_column(data, h)
    col.each do |_name, analyzer|
      analyze_column(analyzer, row[h])
    end
  end
end

#cached_result(val, &_block) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/csv2psql/analyzer/analyzer.rb', line 54

def cached_result(val, &_block)
  res = @cache.get(val)
  if res.nil?
    res = Proc.new.call(val)
    @cache.put(val, res)
  end
  res
end

#create_column(data, column) ⇒ Object

Create column analyzers



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/csv2psql/analyzer/analyzer.rb', line 64

def create_column(data, column)
  data[:columns][column] = {}
  res = data[:columns][column]

  analyzers.each do |analyzer|
    analyzer_class = analyzer[:class]
    res[analyzer[:name]] = {
      class: analyzer_class.new,
      results: create_results(analyzer_class)
    }
  end

  res
end

#create_data(path) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/csv2psql/analyzer/analyzer.rb', line 79

def create_data(path)
  files[path] = {
    columns: {
    },
    lines: 0
  }
  files[path]
end

#create_results(analyzer_class) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/csv2psql/analyzer/analyzer.rb', line 88

def create_results(analyzer_class)
  res = {
    count: 0
  }

  res[:min] = nil if analyzer_class.numeric?
  res[:max] = nil if analyzer_class.numeric?

  res
end

#get_column(data, column) ⇒ Object



105
106
107
108
109
110
# File 'lib/csv2psql/analyzer/analyzer.rb', line 105

def get_column(data, column)
  res = data[:columns][column]
  return res if res

  create_column(data, column)
end

#get_data(path) ⇒ Object



99
100
101
102
103
# File 'lib/csv2psql/analyzer/analyzer.rb', line 99

def get_data(path)
  return files[path] if files.key?(path)

  create_data(path)
end

#load_analyzer(path) ⇒ Object



118
119
120
121
122
123
124
125
126
127
# File 'lib/csv2psql/analyzer/analyzer.rb', line 118

def load_analyzer(path)
  fname = File.basename(path, '.rb')
  analyzer_class = fname.camel_case
  require(path)

  {
    name: analyzer_class,
    class: load_analyzer_class(analyzer_class)
  }
end

#load_analyzer_class(analyzer_class) ⇒ Object



112
113
114
115
116
# File 'lib/csv2psql/analyzer/analyzer.rb', line 112

def load_analyzer_class(analyzer_class)
  Object.const_get('Csv2Psql')
  .const_get('Analyzers')
  .const_get(analyzer_class)
end

#load_analyzersObject



129
130
131
132
133
134
135
136
137
# File 'lib/csv2psql/analyzer/analyzer.rb', line 129

def load_analyzers
  res = Dir[ANALYZERS_DIR + '**/*.rb'].map do |path|
    name = File.basename(path, '.rb')
    next if EXCLUDED_ANALYZERS.include?(name)
    load_analyzer(path)
  end

  res.compact
end

#update_numeric_results(ac, ar, val) ⇒ Object

Update numeric results

Parameters:

  • ac

    analyzer class

  • ar

    analyzer results

  • val

    value to be analyzed



143
144
145
146
147
# File 'lib/csv2psql/analyzer/analyzer.rb', line 143

def update_numeric_results(ac, ar, val)
  cval = ac.convert(val)
  ar[:min] = cval if ar[:min].nil? || cval < ar[:min]
  ar[:max] = cval if ar[:max].nil? || cval > ar[:max]
end

#update_results(analyzer, res, val) ⇒ Object



149
150
151
152
153
154
155
# File 'lib/csv2psql/analyzer/analyzer.rb', line 149

def update_results(analyzer, res, val)
  ac = analyzer[:class]
  ar = analyzer[:results]
  ar[:count] += 1

  update_numeric_results(ac, ar, val) if res && ac.numeric?
end