table_inspector 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 501b198b975a552496604e4342afac6e837423ca04487327a9a7354c72f9ba36
4
- data.tar.gz: 04d8de8b58039d17d8e5229cfab6b766776c099ce5ef0d39f22cfbba8ffab09a
3
+ metadata.gz: 83143bbf7fb6d4ce752da10da85b265037f75fad9417f104ab761660d638db1b
4
+ data.tar.gz: 6268b3194f9eef7ec25c830cae63cad79d0fa29ed8b98f27c77049b450d31def
5
5
  SHA512:
6
- metadata.gz: baaf7974ceeb46d135c2d611b7397669b13081e3d29e15d5a5703b080e97f9380f29b599ab6119f6e5f7e4da3eead47201e5cd7a5532993358c8565e9afa8ec4
7
- data.tar.gz: 30175fa53056aaa4716aabd45085b0f84d05cc5f68f746828dd8b06c082565799bfc1bdeec962ad77e3756de883a2d4fc9c6190b104cd2f4508d0ef43d7d60ab
6
+ metadata.gz: 5e5df809d52919113556d0048e91e633663bcba444b4d2cff13919bb0670f01a51a9460de7789ef556f661c5fc25aa213eff3fb38547a0566331300cd17dd5f4
7
+ data.tar.gz: a137cf1372c765da06bcfc459f4ab0d5b7f9e09f947bbaa0908c560f8cbea24e3e6ab45a5dad043527e7b6f57328d4541876f1acae10394faf2d5435888bf344
data/README.md CHANGED
@@ -1,7 +1,5 @@
1
1
  # TableInspector
2
- This is a rails gem for print the definition of table. Sometimes we use some gems to embed the table schema to model file(like `annotate`).
3
- but sometimes it is hard to maintain, and it has a little noise. I want to find another way to check the schema of table instead of
4
- using `annotate`, So I wrote this gem to do this.
2
+ This is a Rails gem that allows you to print the definition of a table. While some gems (like [annotate_models](https://github.com/ctran/annotate_models)) are used to embed the table schema in the model file, they can be difficult to maintain and add unnecessary noise. This gem provides an alternative way to check the schema of a table without using `annotate_models`.
5
3
 
6
4
  ## Usage
7
5
  Assuming there is a model call `User` which has `id` and `name` column, and has a unique index for `name`.
@@ -12,7 +10,7 @@ require "table_inspector"
12
10
  TableInspector.scan User
13
11
  ```
14
12
 
15
- ![TableInspect scan table](/img/table_inspector_scan_table_2.png)
13
+ ![TableInspect scan table](/img/table_inspector_scan_table_3.png)
16
14
 
17
15
  It will print the all table definition and all indexes.
18
16
 
@@ -20,14 +18,14 @@ Or you can use `TableInspector.ascan` to print more colorful table(`ascan` means
20
18
  ```ruby
21
19
  TableInspector.ascan User
22
20
  ```
23
- ![TableInspect ascan table](/img/table_inspector_ascan_table_2.png)
21
+ ![TableInspect ascan table](/img/table_inspector_ascan_table_3.png)
24
22
 
25
23
  And to print a specific column by:
26
24
 
27
25
  ```ruby
28
26
  TableInspector.scan User, :name
29
27
  ```
30
- ![Table Inspector scan column](/img/table_inspector_scan_column_2.png)
28
+ ![Table Inspector scan column](/img/table_inspector_scan_column_3.png)
31
29
 
32
30
  It will print the column definition and which indexes that contains this column.
33
31
 
@@ -36,7 +34,7 @@ Also, you can print `sql_type` which type of column in database by provide `sql_
36
34
  ```ruby
37
35
  TableInspector.scan User, sql_type: true
38
36
  ```
39
- ![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type_2.png)
37
+ ![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type_3.png)
40
38
 
41
39
  ## Installation
42
40
  Add this line to your application's Gemfile:
@@ -7,6 +7,7 @@ module TableInspector
7
7
  @column = klass.columns.find {|column| column.name == column_name.to_s}
8
8
  @klass = klass
9
9
  @sql_type = sql_type
10
+ @colorize = colorize
10
11
  @presenter = Presenter.new(klass, sql_type: sql_type, colorize: colorize)
11
12
  end
12
13
 
@@ -15,7 +16,7 @@ module TableInspector
15
16
  render_title
16
17
  render_body
17
18
  Text.break_line
18
- Indexes.new(klass, column.name).render
19
+ Indexes.new(klass, column.name, colorize: @colorize).render
19
20
  Text.break_line
20
21
  end
21
22
 
@@ -3,9 +3,10 @@ module TableInspector
3
3
  class Indexes
4
4
  attr_reader :klass, :column_name
5
5
 
6
- def initialize(klass, column_name = nil)
6
+ def initialize(klass, column_name = nil, colorize: false)
7
7
  @klass = klass
8
8
  @column_name = column_name
9
+ @colorize = colorize
9
10
  end
10
11
 
11
12
  def render
@@ -43,6 +44,22 @@ module TableInspector
43
44
  end
44
45
 
45
46
  def compose_index_data(index)
47
+ if @colorize
48
+ compose_index_data_with_highlight(index)
49
+ else
50
+ compose_index_data_without_highlight(index)
51
+ end
52
+ end
53
+
54
+ def compose_index_data_with_highlight(index)
55
+ [
56
+ index.name,
57
+ "[#{Text.yellow(index.columns.join(', '))}]",
58
+ index.unique ? "UNIQUE" : ""
59
+ ]
60
+ end
61
+
62
+ def compose_index_data_without_highlight(index)
46
63
  [
47
64
  index.name,
48
65
  "[#{index.columns.join(', ')}]",
@@ -59,7 +76,7 @@ module TableInspector
59
76
  end
60
77
 
61
78
  def indexes_with_specific_column
62
- indexes.select{|index| index.columns.include?(column_name) }
79
+ indexes.select{|index| index.columns.include?(column_name.to_s) }
63
80
  end
64
81
 
65
82
  def connection
@@ -25,7 +25,7 @@ module TableInspector
25
25
  private
26
26
 
27
27
  def extract_meta_with_highlight(column)
28
- column_data = column.as_json.merge(column.sql_type_metadata.as_json)
28
+ column_data = extract_meta_without_highlight(column)
29
29
 
30
30
  # Colorize text but except "comment" field
31
31
  column_data.each do |k, v|
@@ -40,7 +40,11 @@ module TableInspector
40
40
  end
41
41
 
42
42
  def extract_meta_without_highlight(column)
43
- column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
43
+ column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
44
+ if column_data["default"] == ""
45
+ column_data["default"] = column_data["default"].inspect
46
+ end
47
+ end
44
48
  end
45
49
 
46
50
  def ordered_keys
@@ -7,6 +7,7 @@ module TableInspector
7
7
  @klass = klass
8
8
  @sql_type = sql_type
9
9
  @presenter = Presenter.new(klass, sql_type: sql_type, colorize: colorize)
10
+ @colorize = colorize
10
11
  end
11
12
 
12
13
  def render
@@ -35,7 +36,7 @@ module TableInspector
35
36
  end
36
37
 
37
38
  def render_indexes
38
- Indexes.new(klass).render
39
+ Indexes.new(klass, colorize: @colorize).render
39
40
  end
40
41
  end
41
42
  end
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  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.5.4
4
+ version: 0.5.5
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-31 00:00:00.000000000 Z
11
+ date: 2023-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -82,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
84
  requirements: []
85
- rubygems_version: 3.4.7
85
+ rubygems_version: 3.4.10
86
86
  signing_key:
87
87
  specification_version: 4
88
88
  summary: Inspect table structure of ActiveRecord class