table_inspector 0.2.0 → 0.3.1

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: 9658ef637dc9ee281cfc80f5480949dc522aa16c1c0b9c1ea61310058f9fb62d
4
- data.tar.gz: 2460f6b96e97c20bc81bcd61dfe3ed021e5c0e1a4efec47ef1a0ebd17176ebb5
3
+ metadata.gz: 42d6919ba568fa3f00f338fc84c7d526de66932b17693f72fea8fd0ab8497961
4
+ data.tar.gz: 82978c52fac51ee0943ed41a2d4105079fb536fd69c66fc0bf52a1d2c95fb226
5
5
  SHA512:
6
- metadata.gz: c26c6a0458ed9fdda4430d645af95891acb2604e731fcd846c17024377bba51fc2fa7044210867596811a8062fe11ddfa7f467f59f1777a366dbe72e538ded7a
7
- data.tar.gz: 83ef3ead19f01b223a6f52dd9bf06c2d2e961f28ca0c8aa794f5374e4a82ed84f2e8f58962cdcd99687f37705bf2edd0dad927312bb3bc5c756bb3645aa820bb
6
+ metadata.gz: 2bd23700289b479051fd88f701721e0cffab48b33ebca2a80e043007e8020e78f0db3a05c19405a1b7c2c7656ab526f618b31522b94231670fa87a16700c404d
7
+ data.tar.gz: 4a81daad914ffff7eac4853761044c408d49cbd5f4e2ea9f38a2c3ec2ef355b8024b1fc770bdff3124465ec7359a6a2e9db9ebd410fa594cba2d520839eebcdf
data/README.md CHANGED
@@ -4,7 +4,8 @@ but sometimes it is hard to maintain, and it has a little noise. I want to find
4
4
  using `annotate`, So I wrote this gem to do this.
5
5
 
6
6
  ## Usage
7
- Assuming there is a model is called User which has `id` and `name` column. For print the definition of User, we can use:
7
+ Assuming there is a model call `User` which has `id` and `name` column, and has a unique index for `name`.
8
+ For print the definition of User, we can use:
8
9
  ```ruby
9
10
  require "table_inspector"
10
11
 
@@ -13,12 +14,21 @@ TableInspector.scan User
13
14
 
14
15
  ![TableInspect scan table](img/table_inspector_scan_table.png)
15
16
 
17
+ It will print the all table definition and all indexes.
18
+
16
19
  And to print specific column by:
17
20
 
18
21
  ```ruby
19
22
  TableInspector.scan User, :name
20
23
  ```
21
24
  ![Table Inspector scan column](img/table_inspector_scan_column.png)
25
+
26
+ It will print the column definition and which indexes that contains this column.
27
+
28
+ Also, you can print `sql_type` which type of column in database by provide `sql_type: true` option:
29
+
30
+ ![Table Inspector scan table column with sql type](img/table_inspector_scan_table_with_sql_type.png)
31
+
22
32
  ## Installation
23
33
  Add this line to your application's Gemfile:
24
34
 
@@ -0,0 +1,45 @@
1
+
2
+ module TableInspector
3
+ class Column
4
+ attr_reader :column, :klass, :sql_type, :presenter
5
+
6
+ def initialize(klass, column_name, sql_type: false)
7
+ @column = klass.columns.find {|column| column.name == column_name.to_s}
8
+ raise_column_not_found_error! unless @column
9
+ @klass = klass
10
+ @sql_type = sql_type
11
+ @presenter = Presenter.new(klass, sql_type: sql_type)
12
+ end
13
+
14
+ def render
15
+ Text.break_line
16
+
17
+ render_title
18
+ render_body
19
+
20
+ Text.break_line
21
+
22
+ Indexes.new(klass, column.name).render
23
+
24
+ Text.break_line
25
+ end
26
+
27
+ private
28
+
29
+ def render_title
30
+ Grid.new.render padding: [0, 2] do |grid|
31
+ grid << ["#{Text.bold('Table')}: #{klass.table_name}", "#{Text.bold('Column')}: #{column.name}"]
32
+ end
33
+ end
34
+
35
+ def render_body
36
+ Grid.new(header: presenter.header).render_ascii do |grid|
37
+ grid << @presenter.extract_meta(column).values
38
+ end
39
+ end
40
+
41
+ def raise_column_not_found_error!
42
+ raise TableInspector::Error, "Column not found!"
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+
2
+ module TableInspector
3
+ class Grid
4
+ attr_reader :grid
5
+
6
+ def initialize(**options)
7
+ @grid = TTY::Table.new(**options)
8
+ end
9
+
10
+ def render(**with)
11
+ yield grid
12
+ puts grid.render(**common_render_options.merge(with))
13
+ end
14
+
15
+ def render_ascii(**with)
16
+ yield grid
17
+ puts grid.render(:ascii, **common_render_options.merge(with))
18
+ end
19
+
20
+ def common_render_options
21
+ {
22
+ multiline: true
23
+ }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,75 @@
1
+
2
+ module TableInspector
3
+ class Indexes
4
+ attr_reader :klass, :column
5
+
6
+ def initialize(klass, column = nil)
7
+ @klass = klass
8
+ @column = column
9
+ end
10
+
11
+ def render
12
+ render_title
13
+
14
+ if column
15
+ render_indexes_with_specific_column
16
+ else
17
+ render_indexes
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def render_title
24
+ Grid.new.render padding: [0, 2] do |grid|
25
+ grid << [Text.bold("Indexes")]
26
+ end
27
+ end
28
+
29
+ def render_indexes_with_specific_column
30
+ if indexes_with_specific_column.blank?
31
+ puts "Empty."
32
+ return
33
+ end
34
+
35
+ Grid.new.render do |grid|
36
+ indexes_with_specific_column.each do |index|
37
+ grid << compose_index_data(index)
38
+ end
39
+ end
40
+ end
41
+
42
+ def render_indexes
43
+ if indexes.blank?
44
+ puts "Empty."
45
+ return
46
+ end
47
+
48
+ Grid.new.render do |grid|
49
+ indexes.each do |index|
50
+ grid << compose_index_data(index)
51
+ end
52
+ end
53
+ end
54
+
55
+ def compose_index_data(index)
56
+ [
57
+ index.name,
58
+ "[#{index.columns.join(', ')}]",
59
+ index.unique ? "UNIQUE" : ""
60
+ ]
61
+ end
62
+
63
+ def indexes
64
+ @indexes ||= connection.indexes(klass.table_name)
65
+ end
66
+
67
+ def indexes_with_specific_column
68
+ indexes.select{|index| index.columns.include?(column) }
69
+ end
70
+
71
+ def connection
72
+ @connection ||= ActiveRecord::Base.connection
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,28 @@
1
+
2
+ module TableInspector
3
+ class Presenter
4
+ attr_reader :klass, :sql_type
5
+
6
+ def initialize(klass, sql_type:)
7
+ @klass = klass
8
+ @sql_type = sql_type
9
+ end
10
+
11
+ def extract_meta(column)
12
+ column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
13
+ end
14
+
15
+ def header
16
+ first_column = klass.columns.first
17
+ extract_meta(first_column).keys.map(&:humanize)
18
+ end
19
+
20
+ private
21
+
22
+ def ordered_keys
23
+ %w[name type limit null default precision scale comment].tap do |keys|
24
+ keys << "sql_type" if sql_type
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,41 @@
1
+
2
+ module TableInspector
3
+ class Table
4
+ attr_reader :klass, :sql_type, :presenter
5
+
6
+ def initialize(klass, sql_type: false)
7
+ @klass = klass
8
+ @sql_type = sql_type
9
+ @presenter = Presenter.new(klass, sql_type: sql_type)
10
+ end
11
+
12
+ def render
13
+ Text.break_line
14
+
15
+ render_title
16
+ render_body
17
+
18
+ Text.break_line
19
+
20
+ Indexes.new(klass).render
21
+
22
+ Text.break_line
23
+ end
24
+
25
+ private
26
+
27
+ def render_title
28
+ Grid.new.render with: {padding: [0, 2]} do |grid|
29
+ grid << ["#{Text.bold('Table')}: #{klass.table_name}"]
30
+ end
31
+ end
32
+
33
+ def render_body
34
+ Grid.new(header: presenter.header).render_ascii do |grid|
35
+ klass.columns.each do |column|
36
+ grid << presenter.extract_meta(column).values
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module TableInspector
3
+ module Text
4
+ extend self
5
+
6
+ def bold(text)
7
+ "\033[1m#{text}\033[0m"
8
+ end
9
+
10
+ def break_line
11
+ puts "\n"
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -1,12 +1,18 @@
1
1
  require "table_inspector/version"
2
2
  require "table_inspector/railtie"
3
3
  require "tty-table"
4
+ require "table_inspector/table"
5
+ require "table_inspector/grid"
6
+ require "table_inspector/indexes"
7
+ require "table_inspector/text"
8
+ require "table_inspector/column"
9
+ require "table_inspector/presenter"
4
10
 
5
11
  module TableInspector
12
+ extend self
13
+
6
14
  Error = Class.new(StandardError)
7
15
 
8
- extend self
9
-
10
16
  def scan(klass, column_name = nil, sql_type: false)
11
17
  begin
12
18
  unless klass.is_a?(Class)
@@ -19,91 +25,15 @@ module TableInspector
19
25
  raise_invalid_model_error! unless klass < ActiveRecord::Base
20
26
 
21
27
  if column_name
22
- scan_column(klass, column_name, sql_type: sql_type)
28
+ Column.new(klass, column_name, sql_type: sql_type).render
23
29
  else
24
- scan_table(klass, sql_type: sql_type)
30
+ Table.new(klass, sql_type: sql_type).render
25
31
  end
26
32
  end
27
33
 
28
34
  private
29
35
 
30
- def scan_column(klass, col_name, sql_type: false)
31
- columns = klass.columns
32
- column = columns.find{|col| col.name == col_name.to_s}
33
-
34
- raise_column_not_found_error! unless column
35
-
36
- meta = extract_meta(column, sql_type: sql_type)
37
- header = meta.keys.map(&:upcase_first)
38
-
39
- table = TTY::Table.new(header: header)
40
- table << extract_meta(column, sql_type: sql_type).values
41
-
42
- puts table.render(:ascii)
43
- end
44
-
45
- def scan_table(klass, sql_type: false )
46
- columns = klass.columns
47
- first_column_meta = extract_meta(columns.first, sql_type: sql_type)
48
- header = first_column_meta.keys.map(&:humanize)
49
- table = TTY::Table.new(header: header)
50
-
51
- columns.each do |column|
52
- table << extract_meta(column, sql_type: sql_type).values
53
- end
54
-
55
- puts "\n"
56
- puts "#{bold("Table")}: #{klass.table_name}"
57
- puts "\n"
58
- puts table.render(:ascii)
59
- puts "\n"
60
- print_indexes(klass)
61
- end
62
-
63
- def extract_meta(column, sql_type: false)
64
- column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys(sql_type: sql_type))
65
- end
66
-
67
- def ordered_keys(sql_type: false)
68
- %w[ name type limit null default precision scale comment].tap do |keys|
69
- keys << "sql_type" if sql_type
70
- end
71
- end
72
-
73
36
  def raise_invalid_model_error!
74
37
  raise Error, "Passed class is not a Model class!"
75
38
  end
76
-
77
- def raise_column_not_found_error!
78
- raise Error, "Column not found!"
79
- end
80
-
81
- def list_indexes(klass)
82
- table_name = klass.table_name
83
- connection.indexes(table_name)
84
- end
85
-
86
- def print_indexes(klass)
87
- table = TTY::Table.new
88
- indexes = list_indexes(klass)
89
- indexes.each do |index|
90
- table << [
91
- index.name,
92
- "[#{index.columns.join(', ')}]",
93
- index.unique ? "UNIQUE" : ""
94
- ]
95
- end
96
-
97
- puts bold("Indexes")
98
- puts "\n"
99
- puts table.render
100
- end
101
-
102
- def connection
103
- @_connection ||= ActiveRecord::Base.connection
104
- end
105
-
106
- def bold(str)
107
- "\033[1m#{str}\033[0m"
108
- end
109
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
@@ -49,7 +49,13 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/table_inspector.rb
52
+ - lib/table_inspector/column.rb
53
+ - lib/table_inspector/grid.rb
54
+ - lib/table_inspector/indexes.rb
55
+ - lib/table_inspector/presenter.rb
52
56
  - lib/table_inspector/railtie.rb
57
+ - lib/table_inspector/table.rb
58
+ - lib/table_inspector/text.rb
53
59
  - lib/table_inspector/version.rb
54
60
  - lib/tasks/table_inspector_tasks.rake
55
61
  homepage: https://github.com/table_inspector