table_inspector 0.3.1 → 0.4.0

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: 42d6919ba568fa3f00f338fc84c7d526de66932b17693f72fea8fd0ab8497961
4
- data.tar.gz: 82978c52fac51ee0943ed41a2d4105079fb536fd69c66fc0bf52a1d2c95fb226
3
+ metadata.gz: 64cb2752d497fae638297315267d3b4f5621b7a197fc321c440b60c73a353a14
4
+ data.tar.gz: 92ca3311efa5e77768aad96784d6d11c14dd7196ee0877529b4aaa2213d90a0f
5
5
  SHA512:
6
- metadata.gz: 2bd23700289b479051fd88f701721e0cffab48b33ebca2a80e043007e8020e78f0db3a05c19405a1b7c2c7656ab526f618b31522b94231670fa87a16700c404d
7
- data.tar.gz: 4a81daad914ffff7eac4853761044c408d49cbd5f4e2ea9f38a2c3ec2ef355b8024b1fc770bdff3124465ec7359a6a2e9db9ebd410fa594cba2d520839eebcdf
6
+ metadata.gz: 2cbf4d4ea540bb0883528f4ee3ab95ba428fc5e649e387461b7c0037165a64a461d9f9db3c1d9c75ee6fa66ae1d64bc3bd77d964e03f75e431d4ad14f5d117cc
7
+ data.tar.gz: 5b4b05a46dea671f457740f72f6e64f08123e006cdbb7bb8f0952990ca9369472aa0454c549a02807b61d54cd54e88b826301235efad9fd43bfb7ed04732381a
data/README.md CHANGED
@@ -12,22 +12,31 @@ 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_1.png)
16
16
 
17
17
  It will print the all table definition and all indexes.
18
18
 
19
+ Or you can use `TableInspector.ascan` to print more colorful table(`ascan` means `awesome scan`) :
20
+ ```ruby
21
+ TableInspector.ascan User
22
+ ```
23
+ ![TableInspect ascan table](/img/table_inspector_ascan_table_1.png)
24
+
19
25
  And to print specific column by:
20
26
 
21
27
  ```ruby
22
28
  TableInspector.scan User, :name
23
29
  ```
24
- ![Table Inspector scan column](img/table_inspector_scan_column.png)
30
+ ![Table Inspector scan column](/img/table_inspector_scan_column_1.png)
25
31
 
26
32
  It will print the column definition and which indexes that contains this column.
27
33
 
28
34
  Also, you can print `sql_type` which type of column in database by provide `sql_type: true` option:
29
35
 
30
- ![Table Inspector scan table column with sql type](img/table_inspector_scan_table_with_sql_type.png)
36
+ ```ruby
37
+ TableInspector.scan User, sql_type: true
38
+ ```
39
+ ![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type_1.png)
31
40
 
32
41
  ## Installation
33
42
  Add this line to your application's Gemfile:
@@ -3,12 +3,11 @@ module TableInspector
3
3
  class Column
4
4
  attr_reader :column, :klass, :sql_type, :presenter
5
5
 
6
- def initialize(klass, column_name, sql_type: false)
6
+ def initialize(klass, column_name, sql_type: false, colorize: 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
- @presenter = Presenter.new(klass, sql_type: sql_type)
10
+ @presenter = Presenter.new(klass, sql_type: sql_type, colorize: false)
12
11
  end
13
12
 
14
13
  def render
@@ -27,19 +26,15 @@ module TableInspector
27
26
  private
28
27
 
29
28
  def render_title
30
- Grid.new.render padding: [0, 2] do |grid|
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
@@ -17,10 +17,16 @@ 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
23
29
  }
24
30
  end
25
31
  end
26
- end
32
+ end
@@ -21,18 +21,17 @@ module TableInspector
21
21
  private
22
22
 
23
23
  def render_title
24
- Grid.new.render padding: [0, 2] do |grid|
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
- puts "Empty."
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
- puts "Empty."
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
@@ -3,15 +3,39 @@ module TableInspector
3
3
  class Presenter
4
4
  attr_reader :klass, :sql_type
5
5
 
6
- def initialize(klass, sql_type:)
6
+ def initialize(klass, sql_type:, colorize: false)
7
7
  @klass = klass
8
8
  @sql_type = sql_type
9
+ @colorize = colorize
9
10
  end
10
11
 
11
- def extract_meta(column)
12
+ def extract_meta_with_highlight(column)
13
+ column_data = column.as_json.merge(column.sql_type_metadata.as_json)
14
+
15
+ # Colorize text but except "comment" field
16
+ column_data.each do |k, v|
17
+ if k != "comment"
18
+ column_data[k] = colorize(v)
19
+ else
20
+ column_data[k]
21
+ end
22
+ end
23
+
24
+ column_data.slice(*ordered_keys)
25
+ end
26
+
27
+ def extract_meta_without_highlight(column)
12
28
  column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
13
29
  end
14
30
 
31
+ def extract_meta(column)
32
+ if @colorize
33
+ extract_meta_with_highlight(column)
34
+ else
35
+ extract_meta_without_highlight(column)
36
+ end
37
+ end
38
+
15
39
  def header
16
40
  first_column = klass.columns.first
17
41
  extract_meta(first_column).keys.map(&:humanize)
@@ -24,5 +48,22 @@ module TableInspector
24
48
  keys << "sql_type" if sql_type
25
49
  end
26
50
  end
51
+
52
+ def colorize(value)
53
+ case value
54
+ when TrueClass, DateTime, 'datetime'
55
+ Text.green(value)
56
+ when FalseClass
57
+ Text.red(value)
58
+ when Numeric, 'integer', 'decimal'
59
+ Text.blue(value)
60
+ when 'boolean'
61
+ Text.cyan(value)
62
+ when String
63
+ Text.yellow(value)
64
+ else
65
+ value
66
+ end
67
+ end
27
68
  end
28
- end
69
+ end
@@ -3,10 +3,10 @@ module TableInspector
3
3
  class Table
4
4
  attr_reader :klass, :sql_type, :presenter
5
5
 
6
- def initialize(klass, sql_type: false)
6
+ def initialize(klass, sql_type: false, colorize: false)
7
7
  @klass = klass
8
8
  @sql_type = sql_type
9
- @presenter = Presenter.new(klass, sql_type: sql_type)
9
+ @presenter = Presenter.new(klass, sql_type: sql_type, colorize: colorize)
10
10
  end
11
11
 
12
12
  def render
@@ -25,13 +25,13 @@ module TableInspector
25
25
  private
26
26
 
27
27
  def render_title
28
- Grid.new.render with: {padding: [0, 2]} do |grid|
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
@@ -10,5 +10,25 @@ module TableInspector
10
10
  def break_line
11
11
  puts "\n"
12
12
  end
13
+
14
+ def red(text)
15
+ "\e[31m#{text}\e[0m"
16
+ end
17
+
18
+ def green(text)
19
+ "\e[32m#{text}\e[0m"
20
+ end
21
+
22
+ def yellow(text)
23
+ "\e[33m#{text}\e[0m"
24
+ end
25
+
26
+ def blue(text)
27
+ "\e[34m#{text}\e[0m"
28
+ end
29
+
30
+ def cyan(text)
31
+ "\e[36m#{text}\e[0m"
32
+ end
13
33
  end
14
- end
34
+ end
@@ -0,0 +1,37 @@
1
+
2
+ module TableInspector
3
+ class Validator
4
+ def initialize(klass, column_name)
5
+ @klass = klass
6
+ @column = column_name
7
+ end
8
+
9
+ def validate!
10
+
11
+ if column_name && !validate_column(klass, column_name)
12
+ puts column_is_not_exists_hint(klass, column_name)
13
+ return
14
+ end
15
+
16
+ true
17
+ end
18
+
19
+ private
20
+
21
+ def is_active_record_class?(klass)
22
+ klass < ActiveRecord::Base
23
+ end
24
+
25
+ def validate_column(klass, column_name)
26
+ klass.columns.find{|column| column.name == column_name.to_s }
27
+ end
28
+
29
+ def not_a_model_class_hint(klass)
30
+ "#{klass} is not a model klass"
31
+ end
32
+
33
+ def column_is_not_exists_hint(klass, column_name)
34
+ puts "Column '#{column_name}' doesn't exists in table '#{klass.table_name}'"
35
+ end
36
+ end
37
+ end
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -7,33 +7,53 @@ require "table_inspector/indexes"
7
7
  require "table_inspector/text"
8
8
  require "table_inspector/column"
9
9
  require "table_inspector/presenter"
10
+ require "table_inspector/validator"
10
11
 
11
12
  module TableInspector
12
13
  extend self
13
14
 
14
- Error = Class.new(StandardError)
15
+ def ascan(klass, column_name = nil, sql_type: false)
16
+ klass = init_klass!(klass)
17
+ return unless klass
18
+
19
+ return unless Validator.new(klass, column_name)
20
+
21
+ render(klass, column_name, sql_type, colorize: true)
22
+ end
15
23
 
16
24
  def scan(klass, column_name = nil, sql_type: false)
25
+ klass = init_klass!(klass)
26
+ return unless klass
27
+
28
+ return unless Validator.new(klass, column_name)
29
+
30
+ render(klass, column_name, sql_type)
31
+ end
32
+
33
+ private
34
+
35
+ def init_klass!(klass)
17
36
  begin
18
37
  unless klass.is_a?(Class)
19
- klass = klass.constantize
38
+ klass = klass.to_s.classify.constantize
20
39
  end
21
40
  rescue NameError
22
- raise_invalid_model_error!
41
+ puts invalid_model_name_hint(klass.inspect)
42
+ return
23
43
  end
24
44
 
25
- raise_invalid_model_error! unless klass < ActiveRecord::Base
45
+ klass
46
+ end
26
47
 
48
+ def render(klass, column_name, sql_type, colorize: false)
27
49
  if column_name
28
- Column.new(klass, column_name, sql_type: sql_type).render
50
+ Column.new(klass, column_name, sql_type: sql_type, colorize: colorize).render
29
51
  else
30
- Table.new(klass, sql_type: sql_type).render
52
+ Table.new(klass, sql_type: sql_type, colorize: colorize).render
31
53
  end
32
54
  end
33
-
34
- private
35
55
 
36
- def raise_invalid_model_error!
37
- raise Error, "Passed class is not a Model class!"
56
+ def invalid_model_name_hint(klass)
57
+ "'#{klass}' can be transform to a model class."
38
58
  end
39
59
  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.1
4
+ version: 0.4.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-02-19 00:00:00.000000000 Z
11
+ date: 2023-03-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -56,6 +56,7 @@ files:
56
56
  - lib/table_inspector/railtie.rb
57
57
  - lib/table_inspector/table.rb
58
58
  - lib/table_inspector/text.rb
59
+ - lib/table_inspector/validator.rb
59
60
  - lib/table_inspector/version.rb
60
61
  - lib/tasks/table_inspector_tasks.rake
61
62
  homepage: https://github.com/table_inspector