csv2psql 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6f5f61f71e9f7603d48dc14ef87523cc694aa2d1
4
- data.tar.gz: b94b1da7149fc9440b9a32fd814759d65fe1d6cc
3
+ metadata.gz: c574fd6b1d061c440819ab604708ccba37066764
4
+ data.tar.gz: 303669c9e29f0ac4d6acb3486c571b1292a48019
5
5
  SHA512:
6
- metadata.gz: 3aaadd21b47420361bd97b1b43f331527f5302a89670d58160c5bdadfc67772aa978659651b780da81ed53308f3686890b903558a9c993e58134de14d2105620
7
- data.tar.gz: a95cbe9e026613a399e62081666ecc3505a26377534b915d1748730018fe92e78a3e5efcb6a28c354788d003a40b1064ac1cc9d569652e35509806700d84e690
6
+ metadata.gz: 885032b508c06b0a2cbe0596aeda0e6a07df4a1d0f5f6ae8ba566d8fdb5e37724120a937eb9722bcad25b0cee25290502c90aff5bf725700d31d8a40300494cb
7
+ data.tar.gz: 37e881ed5a62e9eaf6f82c3496964f7c05db63693bad55348f2f2e48f5f838787be4fc5e0c003396fbc50e8c56751d87890823347b758d5f3105088290b3406f
@@ -69,19 +69,21 @@ module Csv2Psql
69
69
  create_column(data, column)
70
70
  end
71
71
 
72
+ def load_analyze_class(analyzer_class)
73
+ Object.const_get('Csv2Psql')
74
+ .const_get('Analyzers')
75
+ .const_get(analyzer_class)
76
+ end
77
+
72
78
  def load_analyzers
73
79
  Dir[ANALYZERS_DIR + '**/*.rb'].map do |path|
74
80
  fname = File.basename(path, '.rb')
75
81
  analyzer_class = fname.camel_case
76
82
  require(path)
77
83
 
78
- klass = Object.const_get('Csv2Psql')
79
- .const_get('Analyzers')
80
- .const_get(analyzer_class)
81
-
82
84
  {
83
- :name => analyzer_class,
84
- :class => klass
85
+ name: analyzer_class,
86
+ class: load_analyze_class(analyzer_class)
85
87
  }
86
88
  end
87
89
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Csv2Psql
4
4
  module Analyzers
5
+ # Bigint value matcher
5
6
  class Bigint
6
7
  TYPE = :bigint
7
8
 
@@ -14,11 +15,17 @@ module Csv2Psql
14
15
  end
15
16
 
16
17
  def analyze(val)
17
- match = val.is_a?(Integer) || (val && val.match(/^\d+$/))
18
- return if match.nil?
18
+ return unless val.is_a?(Integer) || (val && val.match(/^\d+$/))
19
19
 
20
- val = val.to_i
21
- @count = @count + 1
20
+ update(convert(val))
21
+ end
22
+
23
+ def convert(val)
24
+ val.to_i
25
+ end
26
+
27
+ def update(val)
28
+ @count += 1
22
29
  @min = val if @min.nil? || val < @min
23
30
  @max = val if @max.nil? || val > @max
24
31
  end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ module Csv2Psql
4
+ module Analyzers
5
+ # Character value matcher
6
+ class Character
7
+ TYPE = :bigint
8
+
9
+ attr_reader :count
10
+
11
+ def initialize
12
+ @count = 0
13
+ end
14
+
15
+ def analyze(val)
16
+ match = val && val.to_s.length == 1
17
+ return unless match
18
+ @count += 1
19
+ end
20
+ end
21
+ end
22
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Csv2Psql
4
4
  module Analyzers
5
+ # Decimal value matcher
5
6
  class Decimal
6
7
  TYPE = :decimal
7
8
 
@@ -14,11 +15,17 @@ module Csv2Psql
14
15
  end
15
16
 
16
17
  def analyze(val)
17
- match = val.is_a?(Float) || (val && val.match(/(\d+[,.]\d+)/))
18
- return if match.nil?
18
+ return unless val.is_a?(Float) || (val && val.match(/(\d+[,.]\d+)/))
19
19
 
20
- val = val.to_f
21
- @count = @count + 1
20
+ update(convert(val))
21
+ end
22
+
23
+ def convert(val)
24
+ val.to_f
25
+ end
26
+
27
+ def update(val)
28
+ @count += 1
22
29
  @min = val if @min.nil? || val < @min
23
30
  @max = val if @max.nil? || val > @max
24
31
  end
@@ -0,0 +1,22 @@
1
+ # encoding: UTF-8
2
+
3
+ module Csv2Psql
4
+ module Analyzers
5
+ # Null value matcher
6
+ class Null
7
+ TYPE = :null
8
+
9
+ attr_reader :count
10
+
11
+ def initialize
12
+ @count = 0
13
+ end
14
+
15
+ def analyze(val)
16
+ match = val.nil? || val.empty?
17
+ return unless match
18
+ @count += 1
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ module Csv2Psql
4
+ module Analyzers
5
+ # UUID value matcher
6
+ class Uuid
7
+ TYPE = :uuid
8
+ RE = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/ # rubocop:disable Metrics/LineLength
9
+
10
+ attr_reader :count
11
+
12
+ def initialize
13
+ @count = 0
14
+ end
15
+
16
+ def analyze(val)
17
+ match = val && val.match(RE)
18
+ return if match.nil?
19
+ @count += 1
20
+ end
21
+ end
22
+ end
23
+ end
@@ -49,9 +49,9 @@ flag [:s, :separator], cmds[:s]
49
49
 
50
50
  module Csv2Psql
51
51
  # Apollon CLI
52
- module Cli
53
- # CLI Application
54
- class App
52
+ module Cli
53
+ # CLI Application
54
+ class App
55
55
  extend Csv2Psql::Cli::Shared
56
56
 
57
57
  cmds = File.absolute_path(File.join(File.dirname(__FILE__), 'cmd'))
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'gli'
4
4
  require 'pp'
5
+ require 'terminal-table'
5
6
 
6
7
  include GLI::App
7
8
 
@@ -9,9 +10,6 @@ require_relative '../shared'
9
10
  require_relative '../../convert/convert'
10
11
  require_relative '../../processor/processor'
11
12
 
12
- cmds = {
13
- }
14
-
15
13
  desc 'Analyze csv file'
16
14
  command :analyze do |c|
17
15
  c.action do |global_options, options, args|
@@ -19,6 +17,16 @@ command :analyze do |c|
19
17
 
20
18
  opts = {}.merge(global_options).merge(options)
21
19
  res = Csv2Psql::Convert.analyze(args, opts)
22
- pp res.files
20
+
21
+ res.files.each do |_file, details|
22
+ header = ['column'] + res.analyzers.map { |a| a[:name] }
23
+
24
+ rows = details[:columns].map do |k, v|
25
+ [k] + v.keys.map { |name| v[name].count }
26
+ end
27
+
28
+ table = Terminal::Table.new headings: header, rows: rows
29
+ puts table
30
+ end
23
31
  end
24
32
  end
@@ -1,3 +1,6 @@
1
+ # encoding: UTF-8
2
+
3
+ # String class
1
4
  class String
2
5
  def camel_case
3
6
  return self if self !~ /_/ && self =~ /[A-Z]+.*/
@@ -5,6 +8,6 @@ class String
5
8
  end
6
9
 
7
10
  def camel_case_lower
8
- self.split('_').inject([]) { |buffer, e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
11
+ split('_').reduce([]) { |a, e| a.push(a.empty? ? e : e.capitalize) }.join
9
12
  end
10
13
  end
@@ -40,7 +40,6 @@ module Csv2Psql
40
40
  analyzer
41
41
  end
42
42
 
43
-
44
43
  def convert(paths, opts = {})
45
44
  file_headers = {}
46
45
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  # Csv2Psql module
4
4
  module Csv2Psql
5
- CODENAME = 'Blurry bat'
6
- VERSION = '0.0.8'
5
+ CODENAME = 'Flying fish'
6
+ VERSION = '0.0.9'
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv2psql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomas Korcak
@@ -199,7 +199,10 @@ files:
199
199
  - lib/csv2psql.rb
200
200
  - lib/csv2psql/analyzer/analyzer.rb
201
201
  - lib/csv2psql/analyzer/types/bigint.rb
202
+ - lib/csv2psql/analyzer/types/character.rb
202
203
  - lib/csv2psql/analyzer/types/decimal.rb
204
+ - lib/csv2psql/analyzer/types/null.rb
205
+ - lib/csv2psql/analyzer/types/uuid.rb
203
206
  - lib/csv2psql/cli/app.rb
204
207
  - lib/csv2psql/cli/cli.rb
205
208
  - lib/csv2psql/cli/cmd/analyze_cmd.rb