table_inspector 0.3.0 → 0.3.2

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: 13d2e909962ad986dd0d91e00067ccb1329951a2235fa3cee6e24b702043d8de
4
- data.tar.gz: bfd53531536b24cd6b701106a505731d03212984d332627a34085e277bdef76f
3
+ metadata.gz: 5a2079df9ac551a32268d56a87c54ee17c33a4a2c349b19b91d2a38d0d46d192
4
+ data.tar.gz: 9e6ce315d803a4b69363938e632110078502e9ac64f0b79a836fc1e58c9702c6
5
5
  SHA512:
6
- metadata.gz: aafe5d2d7e3bafb8b8138572668af4ea163ae367ac2ccd92cf35e6e09512315ef99c23f607ac40f5a00507b37111da2862eccb762d7f872764b39e95e7621f10
7
- data.tar.gz: ff22018ac69756f6cf3f795331e1697f9116e5329d27dea11195f6200d5b5cad32808d6e213bb06b637af02ffaa3249cfcaf42032cb96f677000a68bffb81524
6
+ metadata.gz: 54961c16f48ef46fe7a0b162341b1844a6dd9245ec5ac3913dd13979404e4bc7b527df33c01c74d0c2338783fb5b6d35f3c823657e7ede877e94de8275362d8b
7
+ data.tar.gz: d26e796110ca40a61657ffe72b7272d817d61b12612f720e0a03383bf14191edffdd00e0970e598cbc77136239170dbe84ff9b7891f84127931aaaaec8dc1ac9
data/README.md CHANGED
@@ -12,7 +12,7 @@ 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.png)
16
16
 
17
17
  It will print the all table definition and all indexes.
18
18
 
@@ -21,13 +21,13 @@ And to print specific column by:
21
21
  ```ruby
22
22
  TableInspector.scan User, :name
23
23
  ```
24
- ![Table Inspector scan column](img/table_inspector_scan_column.png)
24
+ ![Table Inspector scan column](/img/table_inspector_scan_column.png)
25
25
 
26
26
  It will print the column definition and which indexes that contains this column.
27
27
 
28
28
  Also, you can print `sql_type` which type of column in database by provide `sql_type: true` option:
29
29
 
30
- ![Table Inspector scan table column with sql type](img/table_inspector_scan_table_with_sql_type.png)
30
+ ![Table Inspector scan table column with sql type](/img/table_inspector_scan_table_with_sql_type.png)
31
31
 
32
32
  ## Installation
33
33
  Add this line to your application's Gemfile:
@@ -5,7 +5,6 @@ module TableInspector
5
5
 
6
6
  def initialize(klass, column_name, sql_type: 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
10
  @presenter = Presenter.new(klass, sql_type: sql_type)
@@ -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,6 +17,12 @@ 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
@@ -12,7 +12,7 @@ module TableInspector
12
12
  render_title
13
13
 
14
14
  if column
15
- render_indexes_with_specify_column
15
+ render_indexes_with_specific_column
16
16
  else
17
17
  render_indexes
18
18
  end
@@ -21,45 +21,44 @@ 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
- def render_indexes_with_specify_column
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
- grid << [
38
- index.name,
39
- "[#{index.columns.join(', ')}]",
40
- index.unique ? "UNIQUE" : ""
41
- ]
36
+ grid << compose_index_data(index)
42
37
  end
43
38
  end
44
39
  end
45
40
 
46
41
  def render_indexes
47
42
  if indexes.blank?
48
- puts "Empty."
43
+ Grid.render_empty
49
44
  return
50
45
  end
51
46
 
52
- Grid.new.render do |grid|
47
+ Grid.new.render(padding: [0, 2]) do |grid|
53
48
  indexes.each do |index|
54
- grid << [
55
- index.name,
56
- "[#{index.columns.join(', ')}]",
57
- index.unique ? "UNIQUE" : ""
58
- ]
49
+ grid << compose_index_data(index)
59
50
  end
60
51
  end
61
52
  end
62
53
 
54
+ def compose_index_data(index)
55
+ [
56
+ index.name,
57
+ "[#{index.columns.join(', ')}]",
58
+ index.unique ? "UNIQUE" : ""
59
+ ]
60
+ end
61
+
63
62
  def indexes
64
63
  @indexes ||= connection.indexes(klass.table_name)
65
64
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -11,18 +11,25 @@ require "table_inspector/presenter"
11
11
  module TableInspector
12
12
  extend self
13
13
 
14
- Error = Class.new(StandardError)
15
-
16
14
  def scan(klass, column_name = nil, sql_type: false)
17
15
  begin
18
16
  unless klass.is_a?(Class)
19
- klass = klass.constantize
17
+ klass = klass.to_s.classify.constantize
20
18
  end
21
19
  rescue NameError
22
- raise_invalid_model_error!
20
+ puts invalid_model_name_hint(klass.inspect)
21
+ return
23
22
  end
24
23
 
25
- raise_invalid_model_error! unless klass < ActiveRecord::Base
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
26
33
 
27
34
  if column_name
28
35
  Column.new(klass, column_name, sql_type: sql_type).render
@@ -33,7 +40,19 @@ module TableInspector
33
40
 
34
41
  private
35
42
 
36
- def raise_invalid_model_error!
37
- raise Error, "Passed class is not a Model class!"
43
+ def validate_column(klass, column_name)
44
+ klass.columns.find{|column| column.name == column_name.to_s }
45
+ end
46
+
47
+ def invalid_model_name_hint(klass)
48
+ "'#{klass}' can be transform to a model class."
49
+ 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}'"
38
57
  end
39
58
  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.0
4
+ version: 0.3.2
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-02-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails