table_inspector 0.3.2 → 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: 5a2079df9ac551a32268d56a87c54ee17c33a4a2c349b19b91d2a38d0d46d192
4
- data.tar.gz: 9e6ce315d803a4b69363938e632110078502e9ac64f0b79a836fc1e58c9702c6
3
+ metadata.gz: 64cb2752d497fae638297315267d3b4f5621b7a197fc321c440b60c73a353a14
4
+ data.tar.gz: 92ca3311efa5e77768aad96784d6d11c14dd7196ee0877529b4aaa2213d90a0f
5
5
  SHA512:
6
- metadata.gz: 54961c16f48ef46fe7a0b162341b1844a6dd9245ec5ac3913dd13979404e4bc7b527df33c01c74d0c2338783fb5b6d35f3c823657e7ede877e94de8275362d8b
7
- data.tar.gz: d26e796110ca40a61657ffe72b7272d817d61b12612f720e0a03383bf14191edffdd00e0970e598cbc77136239170dbe84ff9b7891f84127931aaaaec8dc1ac9
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,11 +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
8
  @klass = klass
9
9
  @sql_type = sql_type
10
- @presenter = Presenter.new(klass, sql_type: sql_type)
10
+ @presenter = Presenter.new(klass, sql_type: sql_type, colorize: false)
11
11
  end
12
12
 
13
13
  def render
@@ -29,4 +29,4 @@ module TableInspector
29
29
  }
30
30
  end
31
31
  end
32
- end
32
+ 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
@@ -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.2"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -7,11 +7,32 @@ 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
 
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
23
+
14
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)
15
36
  begin
16
37
  unless klass.is_a?(Class)
17
38
  klass = klass.to_s.classify.constantize
@@ -21,38 +42,18 @@ module TableInspector
21
42
  return
22
43
  end
23
44
 
24
- unless klass < ActiveRecord::Base
25
- puts not_a_model_class_hint(klass)
26
- return
27
- end
28
-
29
- if column_name && !validate_column(klass, column_name)
30
- puts column_is_not_exists_hint(klass, column_name)
31
- return
32
- end
45
+ klass
46
+ end
33
47
 
48
+ def render(klass, column_name, sql_type, colorize: false)
34
49
  if column_name
35
- Column.new(klass, column_name, sql_type: sql_type).render
50
+ Column.new(klass, column_name, sql_type: sql_type, colorize: colorize).render
36
51
  else
37
- Table.new(klass, sql_type: sql_type).render
52
+ Table.new(klass, sql_type: sql_type, colorize: colorize).render
38
53
  end
39
54
  end
40
-
41
- private
42
-
43
- def validate_column(klass, column_name)
44
- klass.columns.find{|column| column.name == column_name.to_s }
45
- end
46
55
 
47
56
  def invalid_model_name_hint(klass)
48
57
  "'#{klass}' can be transform to a model class."
49
58
  end
50
-
51
- def not_a_model_class_hint(klass)
52
- "#{klass} is not a model klass"
53
- end
54
-
55
- def column_is_not_exists_hint(klass, column_name)
56
- puts "Column '#{column_name}' doesn't exists in table '#{klass.table_name}'"
57
- end
58
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.2
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-20 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