table_inspector 0.6.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7b361cd6519e4f22acd13f7414daa56d361f83b2bc742679fd7413bc3a3e9d3
4
- data.tar.gz: efebb7e0def5dc75c974a06d7504efaa462068d009438885c16d9a0e6ed73ba1
3
+ metadata.gz: ce96fede5dc61b809184ab371402e7972da49f1fc1708529bf7c38751fc1b595
4
+ data.tar.gz: 9318bbdcfc07d9e5af604bdcf98a1078cada3731136cd259b16ca78e5fbd22fd
5
5
  SHA512:
6
- metadata.gz: cfb17c67a0091dbaccbad92a638bd22de4f7361458baf0bcc25902ce97709d6d15b69d7e01ec51112d5b649f2d89e1c24e762ce3dd2d536055ad9d1b8ab15fe8
7
- data.tar.gz: dd4b194ebf0c45c0b76c18b447ca77460fc84cc74343673cfe250a561e381efc8bccabd4628ec46c2a42262745e7485d8861c498be8f81f2b2cff82d332bc6f4
6
+ metadata.gz: a127d351bc28568ceb50173324092b43d04289bfe0e3af625b304a2208697ea734437db2fc9be9bc6f296eb3db2e3f227e7eccadaef1a7850d3ed5f6c0d84a2a
7
+ data.tar.gz: cc0119437824fbd9be0f0ec80756ffd59339db0143167295943f109b91d0585306f7bcd192fe844176e657db5927d9bf07819f5359004d3f60dbf1238e9f65d8
data/README.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # TableInspector
2
- ![Test coverage](https://img.shields.io/badge/Test_coverage-99.65-green)
2
+ ![Test coverage](https://img.shields.io/badge/Test_coverage-96.71%25-green)
3
3
  ![Release version](https://img.shields.io/badge/Release-v0.6.0-green)
4
4
  ![Workflow badge](https://github.com/otorain/table_inspector/actions/workflows/run_test.yml/badge.svg)
5
5
 
@@ -30,18 +30,15 @@ $ gem install table_inspector
30
30
  Assuming there is a model call `User` which has `id` and `name` column, and has a unique index for `name`.
31
31
  For print the table structure of User, we can use:
32
32
  ```ruby
33
- require "table_inspector"
34
-
35
33
  TableInspector.scan User
36
34
  ```
37
35
 
38
36
  ![TableInspect scan table](/img/table_inspector_scan_table_3.png)
39
37
 
40
- It will print the scheme of table and indexes.
41
- (If the table content is too long, It may be printed messy. You can adjust the scaling of the terminal window to fix it.
42
- Alternatively, you can use `TableInspector.scan(User, :name)` to print a specific column)
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.
43
40
 
44
- Or you can use `TableInspector.ascan` to print more colorful table(`ascan` means `awesome scan`) :
41
+ Alternatively, you can use `TableInspector.ascan` to print a more colorful table(`ascan` means `awesome scan`) :
45
42
  ```ruby
46
43
  TableInspector.ascan User
47
44
  ```
@@ -56,19 +53,25 @@ TableInspector.scan User, :name
56
53
 
57
54
  It will print the column definition and which indexes that contains this column.
58
55
 
59
- **It is recommended to include `TableInspector::Inspectable` in `app/models/application_record.rb` and use `ti` or `ati` method to print the table definition: **
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:
60
57
  ```ruby
61
58
  # app/models/application_record.rb
62
59
  class ApplicationRecord < ActiveRecord::Base
63
- self.abstract_class = true
64
-
65
- # Add this line
66
- include TableInspector::Inspectable
60
+ # ...
67
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
+
68
70
  # ...
69
71
  end
70
72
  ```
71
- and call:
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:
74
+
72
75
  ```ruby
73
76
  # Same as TableInspector.scan User
74
77
  User.ti
@@ -76,7 +79,6 @@ User.ti
76
79
  # Same as TableInspector.ascan User
77
80
  User.ati
78
81
  ```
79
- The module `TableInspector::Inspectable` only defines two class methods: `ti` and `ati`, which delegate to `TableInspector.scan` and `TableInspector.ascan` respectively.
80
82
 
81
83
  You can print the database column type by providing the `sql_type: true` option:
82
84
  ```ruby
@@ -86,7 +88,7 @@ TableInspector.scan User, sql_type: true
86
88
  ```
87
89
  ![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type_3.png)
88
90
 
89
- To print comment column only for the table, use `comment_only: true` option:
91
+ Additionally, if you want to print the comments associated with the columns of the table, you can use the `comment_only` option:
90
92
  ```ruby
91
93
  User.ti comment_only: true
92
94
  # or
@@ -96,7 +98,7 @@ TableInspector.scan User, comment_only: true
96
98
  If the `sql_type: true` option is also provided, the sql_type option will be omitted.
97
99
 
98
100
  ## Style
99
- You can change the style of the table by setting the `TableInspector.style` in `config/initalizers/table_inspector.rb`(create it if not exists):
101
+ To change the style of the table by setting the `TableInspector.style` in `config/initializers/table_inspector.rb`(create it if not exists):
100
102
  ```ruby
101
103
  # config/initializers/table_inspector.rb
102
104
 
@@ -7,7 +7,7 @@ module TableInspector
7
7
  def initialize(klass, column_name, presenter_option)
8
8
  @column = klass.columns.find { |column| column.name == column_name.to_s }
9
9
  @klass = klass
10
- @presenter = Presenter.new(klass, presenter_option)
10
+ @presenter = Presenter.current.new(klass, presenter_option)
11
11
  end
12
12
 
13
13
  def render
@@ -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
@@ -21,31 +21,44 @@ module TableInspector
21
21
  ordered_keys.map(&:humanize)
22
22
  end
23
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
33
+ end
34
+
24
35
  private
25
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
+
26
47
  def extract_meta_with_highlight(column)
27
48
  column_data = extract_meta_without_highlight(column)
28
49
 
29
- # Colorize text but except "comment" field
30
- column_data.each do |k, v|
31
- if k != "comment"
32
- 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)
33
54
  else
34
- column_data[k]
55
+ column_data[k] = colorize(v)
35
56
  end
36
57
  end
37
58
 
38
59
  column_data.slice(*ordered_keys)
39
60
  end
40
61
 
41
- def extract_meta_without_highlight(column)
42
- column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
43
- if column_data["default"] == ""
44
- column_data["default"] = column_data["default"].inspect
45
- end
46
- end
47
- end
48
-
49
62
  def ordered_keys
50
63
  if option.comment_only
51
64
  %w[name comment]
@@ -56,7 +69,30 @@ module TableInspector
56
69
  end
57
70
  end
58
71
 
59
- def colorize(value)
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
92
+ end
93
+ end
94
+
95
+ def colorize_by_value(value)
60
96
  case value
61
97
  when TrueClass, DateTime, 'datetime'
62
98
  green(value)
@@ -6,7 +6,7 @@ module TableInspector
6
6
 
7
7
  def initialize(klass, presenter_option)
8
8
  @klass = klass
9
- @presenter = Presenter.new(klass, presenter_option)
9
+ @presenter = Presenter.current.new(klass, presenter_option)
10
10
  @presenter_option = presenter_option
11
11
  end
12
12
 
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.6.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -7,6 +7,9 @@ require "table_inspector/terminal_table"
7
7
  require "table_inspector/indexes"
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"
12
15
  require "table_inspector/presenter_option"
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.6.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: 2023-08-27 00:00:00.000000000 Z
11
+ date: 2024-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -55,6 +55,9 @@ files:
55
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
58
61
  - lib/table_inspector/presenter_option.rb
59
62
  - lib/table_inspector/railtie.rb
60
63
  - lib/table_inspector/table.rb
@@ -84,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
87
  - !ruby/object:Gem::Version
85
88
  version: '0'
86
89
  requirements: []
87
- rubygems_version: 3.4.10
90
+ rubygems_version: 3.5.17
88
91
  signing_key:
89
92
  specification_version: 4
90
93
  summary: Print table structure of ActiveRecord class