table_inspector 0.5.5 → 1.0.0
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 +4 -4
- data/README.md +75 -18
- data/lib/table_inspector/column.rb +19 -14
- data/lib/table_inspector/column_validator.rb +1 -1
- data/lib/table_inspector/indexes.rb +24 -19
- data/lib/table_inspector/inspectable.rb +16 -0
- data/lib/table_inspector/presenter/mysql2.rb +14 -0
- data/lib/table_inspector/presenter/postgresql.rb +15 -0
- data/lib/table_inspector/presenter/sqlite3.rb +15 -0
- data/lib/table_inspector/presenter.rb +64 -25
- data/lib/table_inspector/presenter_option.rb +12 -0
- data/lib/table_inspector/table.rb +14 -13
- data/lib/table_inspector/{grid.rb → terminal_table.rb} +4 -2
- data/lib/table_inspector/version.rb +1 -1
- data/lib/table_inspector.rb +29 -28
- metadata +11 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ce96fede5dc61b809184ab371402e7972da49f1fc1708529bf7c38751fc1b595
|
4
|
+
data.tar.gz: 9318bbdcfc07d9e5af604bdcf98a1078cada3731136cd259b16ca78e5fbd22fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a127d351bc28568ceb50173324092b43d04289bfe0e3af625b304a2208697ea734437db2fc9be9bc6f296eb3db2e3f227e7eccadaef1a7850d3ed5f6c0d84a2a
|
7
|
+
data.tar.gz: cc0119437824fbd9be0f0ec80756ffd59339db0143167295943f109b91d0585306f7bcd192fe844176e657db5927d9bf07819f5359004d3f60dbf1238e9f65d8
|
data/README.md
CHANGED
@@ -1,20 +1,44 @@
|
|
1
1
|
# TableInspector
|
2
|
-
|
2
|
+

|
3
|
+

|
4
|
+

|
5
|
+
|
6
|
+
This is a Rails gem that allows you to print the structure of a database table by providing a model class.
|
7
|
+
It functions similar to [annotate_models](https://github.com/ctran/annotate_models), but offers an alternative way to checking the table schema.
|
8
|
+
The print function is based on [terminal-table](https://github.com/tj/terminal-table)
|
9
|
+
|
10
|
+
This doc is about version 0.6.0. For version 0.5.0, please see [here](https://github.com/otorain/table_inspector/tree/v0.5.5)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem "table_inspector", "~> 0.6.0"
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
```bash
|
21
|
+
$ bundle
|
22
|
+
```
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
```bash
|
26
|
+
$ gem install table_inspector
|
27
|
+
```
|
3
28
|
|
4
29
|
## Usage
|
5
30
|
Assuming there is a model call `User` which has `id` and `name` column, and has a unique index for `name`.
|
6
|
-
For print the
|
31
|
+
For print the table structure of User, we can use:
|
7
32
|
```ruby
|
8
|
-
require "table_inspector"
|
9
|
-
|
10
33
|
TableInspector.scan User
|
11
34
|
```
|
12
35
|
|
13
36
|

|
14
37
|
|
15
|
-
|
38
|
+
This will print the scheme of table and indexes.
|
39
|
+
If the table content is too long and appears messy, you can adjust the scaling of the terminal window or use the `TableInspector.scan(User, :name)` syntax to print a specific column.
|
16
40
|
|
17
|
-
|
41
|
+
Alternatively, you can use `TableInspector.ascan` to print a more colorful table(`ascan` means `awesome scan`) :
|
18
42
|
```ruby
|
19
43
|
TableInspector.ascan User
|
20
44
|
```
|
@@ -29,29 +53,62 @@ TableInspector.scan User, :name
|
|
29
53
|
|
30
54
|
It will print the column definition and which indexes that contains this column.
|
31
55
|
|
32
|
-
|
56
|
+
If you are using Ruby version 2.7.0 or later, you can define a helper method directly in the model itself by editing the `app/models/application_record.rb` file and adding the code provided below:
|
57
|
+
```ruby
|
58
|
+
# app/models/application_record.rb
|
59
|
+
class ApplicationRecord < ActiveRecord::Base
|
60
|
+
# ...
|
61
|
+
|
62
|
+
def self.ti(...)
|
63
|
+
TableInspector.scan(self, ...) if const_defined?("TableInspector")
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.ati(...)
|
67
|
+
TableInspector.ascan(self, ...) if const_defined?("TableInspector")
|
68
|
+
end
|
69
|
+
|
70
|
+
# ...
|
71
|
+
end
|
72
|
+
```
|
73
|
+
Then you will be able to achieve the same effect as `scan` and `ascan` do by invoking `ti` and `ati` on the model class:
|
33
74
|
|
34
75
|
```ruby
|
76
|
+
# Same as TableInspector.scan User
|
77
|
+
User.ti
|
78
|
+
|
79
|
+
# Same as TableInspector.ascan User
|
80
|
+
User.ati
|
81
|
+
```
|
82
|
+
|
83
|
+
You can print the database column type by providing the `sql_type: true` option:
|
84
|
+
```ruby
|
85
|
+
User.ti sql_type: true
|
86
|
+
# or
|
35
87
|
TableInspector.scan User, sql_type: true
|
36
88
|
```
|
37
89
|

|
38
90
|
|
39
|
-
|
40
|
-
Add this line to your application's Gemfile:
|
41
|
-
|
91
|
+
Additionally, if you want to print the comments associated with the columns of the table, you can use the `comment_only` option:
|
42
92
|
```ruby
|
43
|
-
|
93
|
+
User.ti comment_only: true
|
94
|
+
# or
|
95
|
+
TableInspector.scan User, comment_only: true
|
44
96
|
```
|
97
|
+

|
98
|
+
If the `sql_type: true` option is also provided, the sql_type option will be omitted.
|
45
99
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
100
|
+
## Style
|
101
|
+
To change the style of the table by setting the `TableInspector.style` in `config/initializers/table_inspector.rb`(create it if not exists):
|
102
|
+
```ruby
|
103
|
+
# config/initializers/table_inspector.rb
|
50
104
|
|
51
|
-
|
52
|
-
|
53
|
-
|
105
|
+
TableInspector.style = { border: :unicode }
|
106
|
+
# TableInspector.style = { border: :ascii } # default border style
|
107
|
+
# TableInspector.style = { border: :unicode_round }
|
108
|
+
# TableInspector.style = { border: :unicode_thick_edge }
|
109
|
+
# TableInspector.style = { border: :markdown }
|
54
110
|
```
|
111
|
+
You can use other options available in `TerminalTable#style` of the [terminal-table](https://github.com/tj/terminal-table)
|
55
112
|
|
56
113
|
## Contributing
|
57
114
|
Contribution directions go here.
|
@@ -1,37 +1,42 @@
|
|
1
1
|
|
2
2
|
module TableInspector
|
3
3
|
class Column
|
4
|
-
attr_reader :column, :klass, :
|
4
|
+
attr_reader :column, :klass, :presenter
|
5
|
+
delegate :break_line, :bold, to: TableInspector::Text
|
5
6
|
|
6
|
-
def initialize(klass, column_name,
|
7
|
-
@column = klass.columns.find {|column| column.name == column_name.to_s}
|
7
|
+
def initialize(klass, column_name, presenter_option)
|
8
|
+
@column = klass.columns.find { |column| column.name == column_name.to_s }
|
8
9
|
@klass = klass
|
9
|
-
@
|
10
|
-
@colorize = colorize
|
11
|
-
@presenter = Presenter.new(klass, sql_type: sql_type, colorize: colorize)
|
10
|
+
@presenter = Presenter.current.new(klass, presenter_option)
|
12
11
|
end
|
13
12
|
|
14
13
|
def render
|
15
|
-
|
14
|
+
break_line # empty line
|
16
15
|
render_title
|
17
16
|
render_body
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
break_line # empty line
|
18
|
+
render_indexes
|
19
|
+
break_line # empty line
|
21
20
|
end
|
22
21
|
|
23
22
|
private
|
24
23
|
|
25
24
|
def render_title
|
26
|
-
|
27
|
-
|
25
|
+
TerminalTable.new do |terminal_table|
|
26
|
+
table_name = bold('Table') + ": " + klass.table_name
|
27
|
+
column_name = bold('Column') + ": " + column.name
|
28
|
+
terminal_table.add_row([table_name, column_name])
|
28
29
|
end.render
|
29
30
|
end
|
30
31
|
|
31
32
|
def render_body
|
32
|
-
|
33
|
-
|
33
|
+
TerminalTable.new(headings: presenter.headings) do |terminal_table|
|
34
|
+
terminal_table.add_row(@presenter.extract_meta(column).values)
|
34
35
|
end.render
|
35
36
|
end
|
37
|
+
|
38
|
+
def render_indexes
|
39
|
+
Indexes.new(klass, column.name, colorize: presenter.option.colorize).render
|
40
|
+
end
|
36
41
|
end
|
37
42
|
end
|
@@ -13,7 +13,7 @@ module TableInspector
|
|
13
13
|
render_title
|
14
14
|
|
15
15
|
if column_name
|
16
|
-
|
16
|
+
render_indexes_of_column
|
17
17
|
else
|
18
18
|
render_indexes
|
19
19
|
end
|
@@ -22,48 +22,53 @@ module TableInspector
|
|
22
22
|
private
|
23
23
|
|
24
24
|
def render_title
|
25
|
-
|
26
|
-
|
25
|
+
TerminalTable.new do |terminal_table|
|
26
|
+
terminal_table.add_row([Text.bold("Indexes")])
|
27
27
|
end.render
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
def render_indexes_of_column
|
31
|
+
TerminalTable.new(headings: headings) do |terminal_table|
|
32
|
+
indexes_of_column.each do |index|
|
33
|
+
terminal_table.add_row(compose_index_data(index))
|
34
34
|
end
|
35
35
|
end.render
|
36
36
|
end
|
37
37
|
|
38
38
|
def render_indexes
|
39
|
-
|
39
|
+
TerminalTable.new(headings: headings) do |terminal_table|
|
40
40
|
indexes.each do |index|
|
41
|
-
|
41
|
+
terminal_table.add_row(compose_index_data(index))
|
42
42
|
end
|
43
43
|
end.render
|
44
44
|
end
|
45
45
|
|
46
46
|
def compose_index_data(index)
|
47
47
|
if @colorize
|
48
|
-
|
48
|
+
compose_index_data_with_color(index)
|
49
49
|
else
|
50
|
-
|
50
|
+
compose_index_data_without_color(index)
|
51
51
|
end
|
52
52
|
end
|
53
53
|
|
54
|
-
def
|
54
|
+
def compose_index_data_with_color(index)
|
55
|
+
index_columns_text = index.columns.join(', ')
|
56
|
+
unique_text = index.unique ? "UNIQUE" : ""
|
57
|
+
|
55
58
|
[
|
56
59
|
index.name,
|
57
|
-
"[#{Text.yellow(
|
58
|
-
|
60
|
+
"[#{Text.yellow(index_columns_text)}]",
|
61
|
+
unique_text
|
59
62
|
]
|
60
63
|
end
|
61
64
|
|
62
|
-
def
|
65
|
+
def compose_index_data_without_color(index)
|
66
|
+
index_columns_text = index.columns.join(', ')
|
67
|
+
unique_text = index.unique ? "UNIQUE" : ""
|
63
68
|
[
|
64
69
|
index.name,
|
65
|
-
"[#{
|
66
|
-
|
70
|
+
"[#{index_columns_text}]",
|
71
|
+
unique_text
|
67
72
|
]
|
68
73
|
end
|
69
74
|
|
@@ -75,8 +80,8 @@ module TableInspector
|
|
75
80
|
@indexes ||= connection.indexes(klass.table_name)
|
76
81
|
end
|
77
82
|
|
78
|
-
def
|
79
|
-
indexes.select{|index| index.columns.include?(column_name.to_s) }
|
83
|
+
def indexes_of_column
|
84
|
+
indexes.select{ |index| index.columns.include?(column_name.to_s) }
|
80
85
|
end
|
81
86
|
|
82
87
|
def connection
|
@@ -0,0 +1,16 @@
|
|
1
|
+
|
2
|
+
module TableInspector
|
3
|
+
module Inspectable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def ti(*args, **kwargs, &block)
|
8
|
+
TableInspector.scan(self, *args, **kwargs, &block)
|
9
|
+
end
|
10
|
+
|
11
|
+
def ati(*args, **kwargs, &block)
|
12
|
+
TableInspector.ascan(self, *args, **kwargs, &block)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class TableInspector::Presenter::Mysql2 < TableInspector::Presenter
|
2
|
+
def extract_meta_without_highlight(column)
|
3
|
+
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
|
4
|
+
column_data["default"] = case column_data["type"]
|
5
|
+
when "string"
|
6
|
+
column_data["default"]&.inspect
|
7
|
+
when "boolean"
|
8
|
+
{ "1" => true, "0" => false }[column_data["default"]]
|
9
|
+
else
|
10
|
+
column_data["default"]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
class TableInspector::Presenter::Postgresql < TableInspector::Presenter
|
3
|
+
def extract_meta_without_highlight(column)
|
4
|
+
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
|
5
|
+
column_data["default"] = case column_data["type"]
|
6
|
+
when "string"
|
7
|
+
column_data["default"]&.inspect
|
8
|
+
when "boolean"
|
9
|
+
{ "true" => true, "false" => false }[column_data["default"]]
|
10
|
+
else
|
11
|
+
column_data["default"]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
class TableInspector::Presenter::Sqlite3 < TableInspector::Presenter
|
2
|
+
|
3
|
+
def extract_meta_without_highlight(column)
|
4
|
+
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
|
5
|
+
column_data["default"] = case column_data["type"]
|
6
|
+
when "string"
|
7
|
+
column_data["default"]&.inspect
|
8
|
+
when "boolean"
|
9
|
+
{ "1" => true, "0" => false }[column_data["default"]]
|
10
|
+
else
|
11
|
+
column_data["default"]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -1,16 +1,16 @@
|
|
1
1
|
|
2
2
|
module TableInspector
|
3
3
|
class Presenter
|
4
|
-
attr_reader :klass, :
|
4
|
+
attr_reader :klass, :option
|
5
|
+
delegate :green, :yellow, :red, :blue, :cyan, to: Text
|
5
6
|
|
6
|
-
def initialize(klass,
|
7
|
+
def initialize(klass, option)
|
7
8
|
@klass = klass
|
8
|
-
@
|
9
|
-
@colorize = colorize
|
9
|
+
@option = option
|
10
10
|
end
|
11
11
|
|
12
12
|
def extract_meta(column)
|
13
|
-
if
|
13
|
+
if option.colorize
|
14
14
|
extract_meta_with_highlight(column)
|
15
15
|
else
|
16
16
|
extract_meta_without_highlight(column)
|
@@ -18,53 +18,92 @@ module TableInspector
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def headings
|
21
|
-
|
22
|
-
|
21
|
+
ordered_keys.map(&:humanize)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.current
|
25
|
+
adapter = ApplicationRecord.connection_db_config.adapter.camelize
|
26
|
+
available_presenter = %w[Mysql2 Postgresql Sqlite3]
|
27
|
+
|
28
|
+
if available_presenter.include?(adapter)
|
29
|
+
"TableInspector::Presenter::#{adapter}".constantize
|
30
|
+
else
|
31
|
+
TableInspector::Presenter
|
32
|
+
end
|
23
33
|
end
|
24
34
|
|
25
35
|
private
|
26
36
|
|
37
|
+
def extract_meta_without_highlight(column)
|
38
|
+
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
|
39
|
+
column_data["default"] = if column_data["type"] == "string"
|
40
|
+
column_data["default"]&.inspect
|
41
|
+
else
|
42
|
+
column_data["default"]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
27
47
|
def extract_meta_with_highlight(column)
|
28
48
|
column_data = extract_meta_without_highlight(column)
|
29
49
|
|
30
|
-
# Colorize text
|
31
|
-
column_data.each do |k, v|
|
32
|
-
if k
|
33
|
-
column_data[k] = colorize(v)
|
50
|
+
# Colorize text except comment
|
51
|
+
column_data.except("comment").each do |k, v|
|
52
|
+
if k == "default"
|
53
|
+
column_data[k] = colorize(v, column.type)
|
34
54
|
else
|
35
|
-
column_data[k]
|
55
|
+
column_data[k] = colorize(v)
|
36
56
|
end
|
37
57
|
end
|
38
58
|
|
39
59
|
column_data.slice(*ordered_keys)
|
40
60
|
end
|
41
61
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
|
62
|
+
def ordered_keys
|
63
|
+
if option.comment_only
|
64
|
+
%w[name comment]
|
65
|
+
else
|
66
|
+
%w[name type limit null default precision scale comment].tap do |keys|
|
67
|
+
keys << "sql_type" if option.sql_type
|
46
68
|
end
|
47
69
|
end
|
48
70
|
end
|
49
71
|
|
50
|
-
def
|
51
|
-
|
52
|
-
|
72
|
+
def colorize(value, type = nil)
|
73
|
+
if type
|
74
|
+
colorize_by_type(value, type)
|
75
|
+
else
|
76
|
+
colorize_by_value(value)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def colorize_by_type(value, type)
|
81
|
+
case type
|
82
|
+
when :datetime
|
83
|
+
green(value)
|
84
|
+
when :boolean
|
85
|
+
value ? green(value) : red(value)
|
86
|
+
when :integer, :decimal
|
87
|
+
blue(value)
|
88
|
+
when :string, :json, :jsonb
|
89
|
+
yellow(value)
|
90
|
+
else
|
91
|
+
value
|
53
92
|
end
|
54
93
|
end
|
55
94
|
|
56
|
-
def
|
95
|
+
def colorize_by_value(value)
|
57
96
|
case value
|
58
97
|
when TrueClass, DateTime, 'datetime'
|
59
|
-
|
98
|
+
green(value)
|
60
99
|
when FalseClass
|
61
|
-
|
100
|
+
red(value)
|
62
101
|
when Numeric, 'integer', 'decimal'
|
63
|
-
|
102
|
+
blue(value)
|
64
103
|
when 'boolean'
|
65
|
-
|
104
|
+
cyan(value)
|
66
105
|
when String
|
67
|
-
|
106
|
+
yellow(value)
|
68
107
|
else
|
69
108
|
value
|
70
109
|
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
|
2
|
+
module TableInspector
|
3
|
+
class PresenterOption
|
4
|
+
attr_accessor :sql_type, :comment_only, :colorize
|
5
|
+
|
6
|
+
def initialize(options = {})
|
7
|
+
@sql_type = options.fetch(:sql_type, false)
|
8
|
+
@comment_only = options.fetch(:comment_only, false)
|
9
|
+
@colorize = options.fetch(:colorize, false)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,42 +1,43 @@
|
|
1
1
|
|
2
2
|
module TableInspector
|
3
3
|
class Table
|
4
|
-
attr_reader :klass, :
|
4
|
+
attr_reader :klass, :presenter, :presenter_option
|
5
|
+
delegate :break_line, :bold, to: TableInspector::Text
|
5
6
|
|
6
|
-
def initialize(klass,
|
7
|
+
def initialize(klass, presenter_option)
|
7
8
|
@klass = klass
|
8
|
-
@
|
9
|
-
@
|
10
|
-
@colorize = colorize
|
9
|
+
@presenter = Presenter.current.new(klass, presenter_option)
|
10
|
+
@presenter_option = presenter_option
|
11
11
|
end
|
12
12
|
|
13
13
|
def render
|
14
|
-
|
14
|
+
break_line # empty line
|
15
15
|
render_title
|
16
16
|
render_body
|
17
|
-
|
17
|
+
break_line # empty line
|
18
18
|
render_indexes
|
19
|
-
|
19
|
+
break_line # empty line
|
20
20
|
end
|
21
21
|
|
22
22
|
private
|
23
23
|
|
24
24
|
def render_title
|
25
|
-
|
26
|
-
|
25
|
+
TerminalTable.new do |terminal_table|
|
26
|
+
table_name = bold('Table') + ": " + klass.table_name
|
27
|
+
terminal_table.add_row([table_name])
|
27
28
|
end.render
|
28
29
|
end
|
29
30
|
|
30
31
|
def render_body
|
31
|
-
|
32
|
+
TerminalTable.new(headings: presenter.headings) do |terminal_table|
|
32
33
|
klass.columns.each do |column|
|
33
|
-
|
34
|
+
terminal_table.add_row(presenter.extract_meta(column).values)
|
34
35
|
end
|
35
36
|
end.render
|
36
37
|
end
|
37
38
|
|
38
39
|
def render_indexes
|
39
|
-
Indexes.new(klass, colorize:
|
40
|
+
Indexes.new(klass, colorize: presenter.option.colorize).render
|
40
41
|
end
|
41
42
|
end
|
42
43
|
end
|
@@ -1,6 +1,6 @@
|
|
1
1
|
|
2
2
|
module TableInspector
|
3
|
-
class
|
3
|
+
class TerminalTable
|
4
4
|
attr_reader :terminal_table
|
5
5
|
|
6
6
|
delegate :add_row,
|
@@ -9,13 +9,15 @@ module TableInspector
|
|
9
9
|
|
10
10
|
def initialize(**options, &block)
|
11
11
|
@terminal_table = ::Terminal::Table.new(**options)
|
12
|
+
style = TableInspector.style || { border: :ascii }
|
13
|
+
@terminal_table.style = style
|
12
14
|
yield self if block_given?
|
13
15
|
end
|
14
16
|
|
15
17
|
def render
|
16
18
|
if rows.empty?
|
17
19
|
Text.break_line
|
18
|
-
puts "
|
20
|
+
puts " Empty."
|
19
21
|
else
|
20
22
|
puts self
|
21
23
|
end
|
data/lib/table_inspector.rb
CHANGED
@@ -1,70 +1,71 @@
|
|
1
1
|
require "table_inspector/version"
|
2
2
|
require "table_inspector/railtie"
|
3
3
|
require "terminal-table"
|
4
|
+
require "table_inspector/text"
|
4
5
|
require "table_inspector/table"
|
5
|
-
require "table_inspector/
|
6
|
+
require "table_inspector/terminal_table"
|
6
7
|
require "table_inspector/indexes"
|
7
|
-
require "table_inspector/text"
|
8
8
|
require "table_inspector/column"
|
9
9
|
require "table_inspector/presenter"
|
10
|
+
require "table_inspector/presenter/sqlite3"
|
11
|
+
require "table_inspector/presenter/postgresql"
|
12
|
+
require "table_inspector/presenter/mysql2"
|
10
13
|
require "table_inspector/model_validator"
|
11
14
|
require "table_inspector/column_validator"
|
15
|
+
require "table_inspector/presenter_option"
|
16
|
+
require "table_inspector/inspectable"
|
12
17
|
|
13
18
|
module TableInspector
|
14
19
|
extend self
|
20
|
+
mattr_accessor :style
|
15
21
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
render(klass, column_name, sql_type, colorize: true)
|
22
|
+
# options:
|
23
|
+
# - sql_type: pass "true" print sql type, pass "false" not print sql type, default is false
|
24
|
+
# - comment_only: pass "true" print comment only, pass "false" will print all column info, default is false
|
25
|
+
def ascan(klass, column_name = nil, **options)
|
26
|
+
scan(klass, column_name, colorize: true, **options)
|
23
27
|
end
|
24
28
|
|
25
|
-
|
29
|
+
# options:
|
30
|
+
# - sql_type: pass "true" print sql type, pass "false" not print sql type, default is false
|
31
|
+
# - comment_only: pass "true" print comment only, pass "false" will print all column info, default is false
|
32
|
+
# - colorize: pass "true" print colorful scheme, pass "false" will print scheme without color, default is false
|
33
|
+
def scan(klass, column_name = nil, colorize: false, **options)
|
26
34
|
klass = classify!(klass)
|
27
35
|
|
28
36
|
return unless klass
|
29
37
|
return unless validate!(klass, column_name)
|
30
38
|
|
31
|
-
|
39
|
+
presenter_options = PresenterOption.new({ **options, colorize: colorize })
|
40
|
+
render(klass, column_name, presenter_options)
|
32
41
|
end
|
33
42
|
|
34
43
|
private
|
35
44
|
|
36
45
|
def classify!(klass)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
rescue NameError
|
42
|
-
puts invalid_model_name_hint(klass.inspect)
|
43
|
-
return
|
44
|
-
end
|
45
|
-
|
46
|
-
klass
|
46
|
+
return klass if klass.is_a?(Class)
|
47
|
+
klass.to_s.classify.constantize
|
48
|
+
rescue NameError
|
49
|
+
puts invalid_model_name_hint(klass.inspect)
|
47
50
|
end
|
48
51
|
|
49
52
|
def validate!(klass, column_name)
|
50
53
|
model_validator = ModelValidator.new(klass)
|
51
|
-
unless column_name
|
52
|
-
return model_validator.validate!
|
53
|
-
end
|
54
|
+
return model_validator.validate! unless column_name
|
54
55
|
|
55
56
|
column_validator = ColumnValidator.new(klass, column_name)
|
56
57
|
model_validator.validate! && column_validator.validate!
|
57
58
|
end
|
58
59
|
|
59
|
-
def render(klass, column_name,
|
60
|
+
def render(klass, column_name, presenter_option)
|
60
61
|
if column_name
|
61
|
-
Column.new(klass, column_name,
|
62
|
+
Column.new(klass, column_name, presenter_option).render
|
62
63
|
else
|
63
|
-
Table.new(klass,
|
64
|
+
Table.new(klass, presenter_option).render
|
64
65
|
end
|
65
66
|
end
|
66
67
|
|
67
68
|
def invalid_model_name_hint(klass)
|
68
|
-
"'#{klass}'
|
69
|
+
"'#{klass}' cannot be transformed to a valid model class."
|
69
70
|
end
|
70
71
|
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.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ian
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: 3.0.2
|
41
|
-
description:
|
41
|
+
description: Print table structure of ActiveRecord class
|
42
42
|
email:
|
43
43
|
- ianlynxk@gmail.com
|
44
44
|
executables: []
|
@@ -51,12 +51,17 @@ files:
|
|
51
51
|
- lib/table_inspector.rb
|
52
52
|
- lib/table_inspector/column.rb
|
53
53
|
- lib/table_inspector/column_validator.rb
|
54
|
-
- lib/table_inspector/grid.rb
|
55
54
|
- lib/table_inspector/indexes.rb
|
55
|
+
- lib/table_inspector/inspectable.rb
|
56
56
|
- lib/table_inspector/model_validator.rb
|
57
57
|
- lib/table_inspector/presenter.rb
|
58
|
+
- lib/table_inspector/presenter/mysql2.rb
|
59
|
+
- lib/table_inspector/presenter/postgresql.rb
|
60
|
+
- lib/table_inspector/presenter/sqlite3.rb
|
61
|
+
- lib/table_inspector/presenter_option.rb
|
58
62
|
- lib/table_inspector/railtie.rb
|
59
63
|
- lib/table_inspector/table.rb
|
64
|
+
- lib/table_inspector/terminal_table.rb
|
60
65
|
- lib/table_inspector/text.rb
|
61
66
|
- lib/table_inspector/version.rb
|
62
67
|
- lib/tasks/table_inspector_tasks.rake
|
@@ -82,8 +87,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
87
|
- !ruby/object:Gem::Version
|
83
88
|
version: '0'
|
84
89
|
requirements: []
|
85
|
-
rubygems_version: 3.
|
90
|
+
rubygems_version: 3.5.17
|
86
91
|
signing_key:
|
87
92
|
specification_version: 4
|
88
|
-
summary:
|
93
|
+
summary: Print table structure of ActiveRecord class
|
89
94
|
test_files: []
|