table_inspector 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 38e7941896785704a1baec51bb5f4a76b9ed699ad2957b796797739b08d5169a
4
+ data.tar.gz: 32ab18e8189f664e8928f42050a14daa19f467b6a2de15db95d1de7a3b04771b
5
+ SHA512:
6
+ metadata.gz: e5cd066d6abc3300397f72caeb5239313db2a0981c5b0b4b72ab8abd2f030b151208a96100cb36266b3e36a0a49c8ba3470047c8820e0b2c324405d6ebbbe175
7
+ data.tar.gz: 7f9abe964be15dccec104bc094a8c6e8b7a296d3a61cfe3831f8382d49b986c67c1a10cde3f971cec276805e784c419f19c95316c705eec639e46baffd8ddc11
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2023 ian
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
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. Cause we may just want to see the definition once, and it always in the model file.
4
+
5
+ ## Usage
6
+ Assuming there is a model is called User which has `id` and `name` column. For print the definition of User, we can use:
7
+
8
+ ```ruby
9
+ require "table_inspector"
10
+
11
+ TableInspector.scan User
12
+ ```
13
+
14
+ And to print specific column by:
15
+
16
+ ```ruby
17
+ TableInspector.scan User, :name
18
+ ```
19
+
20
+ ## Installation
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem "table_inspector"
25
+ ```
26
+
27
+ And then execute:
28
+ ```bash
29
+ $ bundle
30
+ ```
31
+
32
+ Or install it yourself as:
33
+ ```bash
34
+ $ gem install table_inspector
35
+ ```
36
+
37
+ ## Contributing
38
+ Contribution directions go here.
39
+
40
+ ## License
41
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/setup"
2
+
3
+ require "bundler/gem_tasks"
@@ -0,0 +1,4 @@
1
+ module TableInspector
2
+ class Railtie < ::Rails::Railtie
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module TableInspector
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,73 @@
1
+ require "table_inspector/version"
2
+ require "table_inspector/railtie"
3
+ require "tty-table"
4
+
5
+ module TableInspector
6
+ Error = Class.new(StandardError)
7
+
8
+ extend self
9
+
10
+ def scan(klass, column_name = nil)
11
+ begin
12
+ unless klass.is_a?(Class)
13
+ klass = klass.constantize
14
+ end
15
+ rescue NameError => e
16
+ raise_invalid_model_error!
17
+ end
18
+
19
+ raise_invalid_model_error! unless klass < ActiveRecord::Base
20
+
21
+ if column_name
22
+ scan_column(klass, column_name)
23
+ else
24
+ scan_table(klass)
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def scan_column(klass, col_name)
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)
37
+ header = meta.keys.map(&:upcase_first)
38
+
39
+ table = TTY::Table.new(header: header)
40
+ table << extract_meta(column).values
41
+
42
+ puts table.render(:ascii)
43
+ end
44
+
45
+ def scan_table(klass)
46
+ columns = klass.columns
47
+ first_column_meta = extract_meta(columns.first)
48
+ header = first_column_meta.keys.map(&:upcase_first)
49
+ table = TTY::Table.new(header: header)
50
+
51
+ columns.each do |column|
52
+ table << extract_meta(column).values
53
+ end
54
+
55
+ puts table.render(:ascii)
56
+ end
57
+
58
+ def extract_meta(column)
59
+ column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
60
+ end
61
+
62
+ def ordered_keys
63
+ %w[ name type limit null default precision scale comment]
64
+ end
65
+
66
+ def raise_invalid_model_error!
67
+ raise Error, "Passed class is not a Model class!"
68
+ end
69
+
70
+ def raise_column_not_found_error!
71
+ raise Error, "Column not found!"
72
+ end
73
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :table_inspector do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: table_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - ian
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.12.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.12.0
41
+ description: Inspect table structure of ActiveRecord class
42
+ email:
43
+ - ianlynxk@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - lib/table_inspector.rb
52
+ - lib/table_inspector/railtie.rb
53
+ - lib/table_inspector/version.rb
54
+ - lib/tasks/table_inspector_tasks.rake
55
+ homepage: https://github.com/table_inspector
56
+ licenses:
57
+ - MIT
58
+ metadata:
59
+ homepage_uri: https://github.com/table_inspector
60
+ source_code_uri: https://github.com/otorain/table_inspector
61
+ changelog_uri: https://github.com/otorain/table_inspector/CHANGELOG
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubygems_version: 3.3.13
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: Inspect table structure of ActiveRecord class
81
+ test_files: []