table_inspector 0.3.1 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -3
- data/lib/table_inspector/column.rb +2 -7
- data/lib/table_inspector/grid.rb +6 -0
- data/lib/table_inspector/indexes.rb +5 -6
- data/lib/table_inspector/table.rb +2 -2
- data/lib/table_inspector/version.rb +1 -1
- data/lib/table_inspector.rb +26 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5a2079df9ac551a32268d56a87c54ee17c33a4a2c349b19b91d2a38d0d46d192
|
4
|
+
data.tar.gz: 9e6ce315d803a4b69363938e632110078502e9ac64f0b79a836fc1e58c9702c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54961c16f48ef46fe7a0b162341b1844a6dd9245ec5ac3913dd13979404e4bc7b527df33c01c74d0c2338783fb5b6d35f3c823657e7ede877e94de8275362d8b
|
7
|
+
data.tar.gz: d26e796110ca40a61657ffe72b7272d817d61b12612f720e0a03383bf14191edffdd00e0970e598cbc77136239170dbe84ff9b7891f84127931aaaaec8dc1ac9
|
data/README.md
CHANGED
@@ -12,7 +12,7 @@ require "table_inspector"
|
|
12
12
|
TableInspector.scan User
|
13
13
|
```
|
14
14
|
|
15
|
-
![TableInspect scan table](img/table_inspector_scan_table.png)
|
15
|
+
![TableInspect scan table](/img/table_inspector_scan_table.png)
|
16
16
|
|
17
17
|
It will print the all table definition and all indexes.
|
18
18
|
|
@@ -21,13 +21,13 @@ And to print specific column by:
|
|
21
21
|
```ruby
|
22
22
|
TableInspector.scan User, :name
|
23
23
|
```
|
24
|
-
![Table Inspector scan column](img/table_inspector_scan_column.png)
|
24
|
+
![Table Inspector scan column](/img/table_inspector_scan_column.png)
|
25
25
|
|
26
26
|
It will print the column definition and which indexes that contains this column.
|
27
27
|
|
28
28
|
Also, you can print `sql_type` which type of column in database by provide `sql_type: true` option:
|
29
29
|
|
30
|
-
![Table Inspector scan table column with sql type](img/table_inspector_scan_table_with_sql_type.png)
|
30
|
+
![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type.png)
|
31
31
|
|
32
32
|
## Installation
|
33
33
|
Add this line to your application's Gemfile:
|
@@ -5,7 +5,6 @@ module TableInspector
|
|
5
5
|
|
6
6
|
def initialize(klass, column_name, sql_type: false)
|
7
7
|
@column = klass.columns.find {|column| column.name == column_name.to_s}
|
8
|
-
raise_column_not_found_error! unless @column
|
9
8
|
@klass = klass
|
10
9
|
@sql_type = sql_type
|
11
10
|
@presenter = Presenter.new(klass, sql_type: sql_type)
|
@@ -27,19 +26,15 @@ module TableInspector
|
|
27
26
|
private
|
28
27
|
|
29
28
|
def render_title
|
30
|
-
Grid.new.render
|
29
|
+
Grid.new.render(padding: [0, 4, 0, 0]) do |grid|
|
31
30
|
grid << ["#{Text.bold('Table')}: #{klass.table_name}", "#{Text.bold('Column')}: #{column.name}"]
|
32
31
|
end
|
33
32
|
end
|
34
33
|
|
35
34
|
def render_body
|
36
|
-
Grid.new(header: presenter.header).render_ascii do |grid|
|
35
|
+
Grid.new(header: presenter.header).render_ascii(indent: 2) do |grid|
|
37
36
|
grid << @presenter.extract_meta(column).values
|
38
37
|
end
|
39
38
|
end
|
40
|
-
|
41
|
-
def raise_column_not_found_error!
|
42
|
-
raise TableInspector::Error, "Column not found!"
|
43
|
-
end
|
44
39
|
end
|
45
40
|
end
|
data/lib/table_inspector/grid.rb
CHANGED
@@ -17,6 +17,12 @@ module TableInspector
|
|
17
17
|
puts grid.render(:ascii, **common_render_options.merge(with))
|
18
18
|
end
|
19
19
|
|
20
|
+
def self.render_empty
|
21
|
+
new.render(padding: [0, 2]) do |grid|
|
22
|
+
grid << ["Empty."]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
20
26
|
def common_render_options
|
21
27
|
{
|
22
28
|
multiline: true
|
@@ -21,18 +21,17 @@ module TableInspector
|
|
21
21
|
private
|
22
22
|
|
23
23
|
def render_title
|
24
|
-
Grid.new.render
|
24
|
+
Grid.new.render do |grid|
|
25
25
|
grid << [Text.bold("Indexes")]
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
def render_indexes_with_specific_column
|
30
30
|
if indexes_with_specific_column.blank?
|
31
|
-
|
32
|
-
return
|
31
|
+
Grid.render_empty
|
33
32
|
end
|
34
33
|
|
35
|
-
Grid.new.render do |grid|
|
34
|
+
Grid.new.render(padding: [0, 2]) do |grid|
|
36
35
|
indexes_with_specific_column.each do |index|
|
37
36
|
grid << compose_index_data(index)
|
38
37
|
end
|
@@ -41,11 +40,11 @@ module TableInspector
|
|
41
40
|
|
42
41
|
def render_indexes
|
43
42
|
if indexes.blank?
|
44
|
-
|
43
|
+
Grid.render_empty
|
45
44
|
return
|
46
45
|
end
|
47
46
|
|
48
|
-
Grid.new.render do |grid|
|
47
|
+
Grid.new.render(padding: [0, 2]) do |grid|
|
49
48
|
indexes.each do |index|
|
50
49
|
grid << compose_index_data(index)
|
51
50
|
end
|
@@ -25,13 +25,13 @@ module TableInspector
|
|
25
25
|
private
|
26
26
|
|
27
27
|
def render_title
|
28
|
-
Grid.new.render
|
28
|
+
Grid.new.render do |grid|
|
29
29
|
grid << ["#{Text.bold('Table')}: #{klass.table_name}"]
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
def render_body
|
34
|
-
Grid.new(header: presenter.header).render_ascii do |grid|
|
34
|
+
Grid.new(header: presenter.header).render_ascii(indent: 2) do |grid|
|
35
35
|
klass.columns.each do |column|
|
36
36
|
grid << presenter.extract_meta(column).values
|
37
37
|
end
|
data/lib/table_inspector.rb
CHANGED
@@ -11,18 +11,25 @@ require "table_inspector/presenter"
|
|
11
11
|
module TableInspector
|
12
12
|
extend self
|
13
13
|
|
14
|
-
Error = Class.new(StandardError)
|
15
|
-
|
16
14
|
def scan(klass, column_name = nil, sql_type: false)
|
17
15
|
begin
|
18
16
|
unless klass.is_a?(Class)
|
19
|
-
klass = klass.constantize
|
17
|
+
klass = klass.to_s.classify.constantize
|
20
18
|
end
|
21
19
|
rescue NameError
|
22
|
-
|
20
|
+
puts invalid_model_name_hint(klass.inspect)
|
21
|
+
return
|
23
22
|
end
|
24
23
|
|
25
|
-
|
24
|
+
unless klass < ActiveRecord::Base
|
25
|
+
puts not_a_model_class_hint(klass)
|
26
|
+
return
|
27
|
+
end
|
28
|
+
|
29
|
+
if column_name && !validate_column(klass, column_name)
|
30
|
+
puts column_is_not_exists_hint(klass, column_name)
|
31
|
+
return
|
32
|
+
end
|
26
33
|
|
27
34
|
if column_name
|
28
35
|
Column.new(klass, column_name, sql_type: sql_type).render
|
@@ -33,7 +40,19 @@ module TableInspector
|
|
33
40
|
|
34
41
|
private
|
35
42
|
|
36
|
-
def
|
37
|
-
|
43
|
+
def validate_column(klass, column_name)
|
44
|
+
klass.columns.find{|column| column.name == column_name.to_s }
|
45
|
+
end
|
46
|
+
|
47
|
+
def invalid_model_name_hint(klass)
|
48
|
+
"'#{klass}' can be transform to a model class."
|
49
|
+
end
|
50
|
+
|
51
|
+
def not_a_model_class_hint(klass)
|
52
|
+
"#{klass} is not a model klass"
|
53
|
+
end
|
54
|
+
|
55
|
+
def column_is_not_exists_hint(klass, column_name)
|
56
|
+
puts "Column '#{column_name}' doesn't exists in table '#{klass.table_name}'"
|
38
57
|
end
|
39
58
|
end
|
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.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-02-
|
11
|
+
date: 2023-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|