table_inspector 0.4.1 → 0.4.3

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
  SHA256:
3
- metadata.gz: dafb1f2e93a8ac7beb1729635132812c7460b4a4bac712c08f011783357bd6ca
4
- data.tar.gz: 4656a91cbb769c9465480bdc1798f3b38bec3b074098eb15cd6549f6ac6e9ea8
3
+ metadata.gz: 7b794452c662ccbfe7471e24a57a86d31ff1919911bd83f2d786e85aca437ef6
4
+ data.tar.gz: 50c4cbcc08b1b93514377474be25e30c0af3c2823bbbf1e36956ccdf48ec78be
5
5
  SHA512:
6
- metadata.gz: 853d58ade3b5b95304c8633cd8e926cc9b1d7d060a0c0d0d6828561d668fae23baf9bca2583619ad07045cf83ad29de3bf98788eb9af2442c8235f4368d1c133
7
- data.tar.gz: 2db47bebcf0715737ebf1e87d3578294be46332ec5023f94bbe3f879bd5fa705506eff47b8c6f30fa5d5254b06f16e30877215cd77c5db7d4e550f01dc1159d0
6
+ metadata.gz: 877e2772ea00688c4e1d08d9cf315d8086768ec8e0670abec682fb4aeb8bc51c78faf4351b5bc265d90e44cffa2c8697fc628e3173aab5bf182ee7068859b2c3
7
+ data.tar.gz: 42a0273f52692a64c1a82af5942038802c377e91b0bcd9f694f4ffb8372746f394943624dd42abd2f780fe007aaf9c769ab05e44b2afbe420bbd23636a1317f8
@@ -0,0 +1,28 @@
1
+
2
+ module TableInspector
3
+ class ColumnValidator
4
+ attr_accessor :klass, :column_name
5
+
6
+ def initialize(klass, column_name)
7
+ @klass = klass
8
+ @column_name = column_name
9
+ end
10
+
11
+ def validate!
12
+ return true if column_exists?
13
+
14
+ puts column_is_not_exists_hint
15
+ false
16
+ end
17
+
18
+ private
19
+
20
+ def column_exists?
21
+ klass.columns.find{|column| column.name == column_name.to_s }
22
+ end
23
+
24
+ def column_is_not_exists_hint
25
+ puts "Column '#{column_name}' doesn't exists in table '#{klass.table_name}'!"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+
2
+ module TableInspector
3
+ class ModelValidator
4
+ attr_accessor :klass
5
+
6
+ def initialize(klass)
7
+ @klass = klass
8
+ end
9
+
10
+ def validate!
11
+ return true if is_model_class?
12
+
13
+ puts not_a_model_class_hint
14
+ false
15
+ end
16
+
17
+ private
18
+
19
+ def is_model_class?
20
+ klass < ActiveRecord::Base
21
+ end
22
+
23
+ def not_a_model_class_hint
24
+ "#{klass} is not a model class!"
25
+ end
26
+ end
27
+ end
@@ -9,6 +9,21 @@ module TableInspector
9
9
  @colorize = colorize
10
10
  end
11
11
 
12
+ def extract_meta(column)
13
+ if @colorize
14
+ extract_meta_with_highlight(column)
15
+ else
16
+ extract_meta_without_highlight(column)
17
+ end
18
+ end
19
+
20
+ def header
21
+ first_column = klass.columns.first
22
+ extract_meta(first_column).keys.map(&:humanize)
23
+ end
24
+
25
+ private
26
+
12
27
  def extract_meta_with_highlight(column)
13
28
  column_data = column.as_json.merge(column.sql_type_metadata.as_json)
14
29
 
@@ -28,21 +43,6 @@ module TableInspector
28
43
  column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
29
44
  end
30
45
 
31
- def extract_meta(column)
32
- if @colorize
33
- extract_meta_with_highlight(column)
34
- else
35
- extract_meta_without_highlight(column)
36
- end
37
- end
38
-
39
- def header
40
- first_column = klass.columns.first
41
- extract_meta(first_column).keys.map(&:humanize)
42
- end
43
-
44
- private
45
-
46
46
  def ordered_keys
47
47
  %w[name type limit null default precision scale comment].tap do |keys|
48
48
  keys << "sql_type" if sql_type
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.4.1"
2
+ VERSION = "0.4.3"
3
3
  end
@@ -7,25 +7,26 @@ require "table_inspector/indexes"
7
7
  require "table_inspector/text"
8
8
  require "table_inspector/column"
9
9
  require "table_inspector/presenter"
10
- require "table_inspector/validator"
10
+ require "table_inspector/model_validator"
11
+ require "table_inspector/column_validator"
11
12
 
12
13
  module TableInspector
13
14
  extend self
14
15
 
15
16
  def ascan(klass, column_name = nil, sql_type: false)
16
17
  klass = init_klass!(klass)
17
- return unless klass
18
18
 
19
- return unless Validator.new(klass, column_name)
19
+ return unless klass
20
+ return unless validate!(klass, column_name)
20
21
 
21
22
  render(klass, column_name, sql_type, colorize: true)
22
23
  end
23
24
 
24
25
  def scan(klass, column_name = nil, sql_type: false)
25
26
  klass = init_klass!(klass)
26
- return unless klass
27
27
 
28
- return unless Validator.new(klass, column_name)
28
+ return unless klass
29
+ return unless validate!(klass, column_name)
29
30
 
30
31
  render(klass, column_name, sql_type)
31
32
  end
@@ -45,6 +46,16 @@ module TableInspector
45
46
  klass
46
47
  end
47
48
 
49
+ def validate!(klass, column_name)
50
+ model_validator = ModelValidator.new(klass)
51
+ unless column_name
52
+ return model_validator.validate!
53
+ end
54
+
55
+ column_validator = ColumnValidator.new(klass, column_name)
56
+ model_validator.validate! && column_validator.validate!
57
+ end
58
+
48
59
  def render(klass, column_name, sql_type, colorize: false)
49
60
  if column_name
50
61
  Column.new(klass, column_name, sql_type: sql_type, colorize: colorize).render
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-02 00:00:00.000000000 Z
11
+ date: 2023-03-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -50,20 +50,21 @@ files:
50
50
  - Rakefile
51
51
  - lib/table_inspector.rb
52
52
  - lib/table_inspector/column.rb
53
+ - lib/table_inspector/column_validator.rb
53
54
  - lib/table_inspector/grid.rb
54
55
  - lib/table_inspector/indexes.rb
56
+ - lib/table_inspector/model_validator.rb
55
57
  - lib/table_inspector/presenter.rb
56
58
  - lib/table_inspector/railtie.rb
57
59
  - lib/table_inspector/table.rb
58
60
  - lib/table_inspector/text.rb
59
- - lib/table_inspector/validator.rb
60
61
  - lib/table_inspector/version.rb
61
62
  - lib/tasks/table_inspector_tasks.rake
62
- homepage: https://github.com/table_inspector
63
+ homepage: https://github.com/otorain/table_inspector
63
64
  licenses:
64
65
  - MIT
65
66
  metadata:
66
- homepage_uri: https://github.com/table_inspector
67
+ homepage_uri: https://github.com/otorain/table_inspector
67
68
  source_code_uri: https://github.com/otorain/table_inspector
68
69
  changelog_uri: https://github.com/otorain/table_inspector/CHANGELOG
69
70
  post_install_message:
@@ -1,37 +0,0 @@
1
-
2
- module TableInspector
3
- class Validator
4
- def initialize(klass, column_name)
5
- @klass = klass
6
- @column = column_name
7
- end
8
-
9
- def validate!
10
-
11
- if column_name && !validate_column(klass, column_name)
12
- puts column_is_not_exists_hint(klass, column_name)
13
- return
14
- end
15
-
16
- true
17
- end
18
-
19
- private
20
-
21
- def is_active_record_class?(klass)
22
- klass < ActiveRecord::Base
23
- end
24
-
25
- def validate_column(klass, column_name)
26
- klass.columns.find{|column| column.name == column_name.to_s }
27
- end
28
-
29
- def not_a_model_class_hint(klass)
30
- "#{klass} is not a model klass"
31
- end
32
-
33
- def column_is_not_exists_hint(klass, column_name)
34
- puts "Column '#{column_name}' doesn't exists in table '#{klass.table_name}'"
35
- end
36
- end
37
- end