-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from otorain/feat/0.6.0
Add :comment_only option; Add the Inspectable module for more concise usage; Code refactor
- Loading branch information
Showing
26 changed files
with
315 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ | |
*.gem | ||
*.lock | ||
.ruby-version | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,68 @@ | ||
require "table_inspector/version" | ||
require "table_inspector/railtie" | ||
require "terminal-table" | ||
require "table_inspector/text" | ||
require "table_inspector/table" | ||
require "table_inspector/grid" | ||
require "table_inspector/terminal_table" | ||
require "table_inspector/indexes" | ||
require "table_inspector/text" | ||
require "table_inspector/column" | ||
require "table_inspector/presenter" | ||
require "table_inspector/model_validator" | ||
require "table_inspector/column_validator" | ||
require "table_inspector/presenter_option" | ||
require "table_inspector/inspectable" | ||
|
||
module TableInspector | ||
extend self | ||
mattr_accessor :style | ||
|
||
def ascan(klass, column_name = nil, sql_type: false) | ||
klass = classify!(klass) | ||
|
||
return unless klass | ||
return unless validate!(klass, column_name) | ||
|
||
render(klass, column_name, sql_type, colorize: true) | ||
# options: | ||
# - sql_type: pass "true" print sql type, pass "false" not print sql type, default is false | ||
# - comment_only: pass "true" print comment only, pass "false" will print all column info, default is false | ||
def ascan(klass, column_name = nil, **options) | ||
scan(klass, column_name, colorize: true, **options) | ||
end | ||
|
||
def scan(klass, column_name = nil, sql_type: false) | ||
# options: | ||
# - sql_type: pass "true" print sql type, pass "false" not print sql type, default is false | ||
# - comment_only: pass "true" print comment only, pass "false" will print all column info, default is false | ||
# - colorize: pass "true" print colorful scheme, pass "false" will print scheme without color, default is false | ||
def scan(klass, column_name = nil, colorize: false, **options) | ||
klass = classify!(klass) | ||
|
||
return unless klass | ||
return unless validate!(klass, column_name) | ||
|
||
render(klass, column_name, sql_type) | ||
presenter_options = PresenterOption.new({ **options, colorize: colorize }) | ||
render(klass, column_name, presenter_options) | ||
end | ||
|
||
private | ||
|
||
def classify!(klass) | ||
begin | ||
unless klass.is_a?(Class) | ||
klass = klass.to_s.classify.constantize | ||
end | ||
rescue NameError | ||
puts invalid_model_name_hint(klass.inspect) | ||
return | ||
end | ||
|
||
klass | ||
return klass if klass.is_a?(Class) | ||
klass.to_s.classify.constantize | ||
rescue NameError | ||
puts invalid_model_name_hint(klass.inspect) | ||
end | ||
|
||
def validate!(klass, column_name) | ||
model_validator = ModelValidator.new(klass) | ||
unless column_name | ||
return model_validator.validate! | ||
end | ||
return model_validator.validate! unless column_name | ||
|
||
column_validator = ColumnValidator.new(klass, column_name) | ||
model_validator.validate! && column_validator.validate! | ||
end | ||
|
||
def render(klass, column_name, sql_type, colorize: false) | ||
def render(klass, column_name, presenter_option) | ||
if column_name | ||
Column.new(klass, column_name, sql_type: sql_type, colorize: colorize).render | ||
Column.new(klass, column_name, presenter_option).render | ||
else | ||
Table.new(klass, sql_type: sql_type, colorize: colorize).render | ||
Table.new(klass, presenter_option).render | ||
end | ||
end | ||
|
||
def invalid_model_name_hint(klass) | ||
"'#{klass}' can be transform to a model class." | ||
"'#{klass}' cannot be transformed to a valid model class." | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,42 @@ | ||
|
||
module TableInspector | ||
class Column | ||
attr_reader :column, :klass, :sql_type, :presenter | ||
attr_reader :column, :klass, :presenter | ||
delegate :break_line, :bold, to: TableInspector::Text | ||
|
||
def initialize(klass, column_name, sql_type: false, colorize: false ) | ||
@column = klass.columns.find {|column| column.name == column_name.to_s} | ||
def initialize(klass, column_name, presenter_option) | ||
@column = klass.columns.find { |column| column.name == column_name.to_s } | ||
@klass = klass | ||
@sql_type = sql_type | ||
@colorize = colorize | ||
@presenter = Presenter.new(klass, sql_type: sql_type, colorize: colorize) | ||
@presenter = Presenter.new(klass, presenter_option) | ||
end | ||
|
||
def render | ||
Text.break_line | ||
break_line # empty line | ||
render_title | ||
render_body | ||
Text.break_line | ||
Indexes.new(klass, column.name, colorize: @colorize).render | ||
Text.break_line | ||
break_line # empty line | ||
render_indexes | ||
break_line # empty line | ||
end | ||
|
||
private | ||
|
||
def render_title | ||
Grid.new do |grid| | ||
grid.add_row(["#{Text.bold('Table')}: #{klass.table_name}", "#{Text.bold('Column')}: #{column.name}"]) | ||
TerminalTable.new do |terminal_table| | ||
table_name = bold('Table') + ": " + klass.table_name | ||
column_name = bold('Column') + ": " + column.name | ||
terminal_table.add_row([table_name, column_name]) | ||
end.render | ||
end | ||
|
||
def render_body | ||
Grid.new(headings: presenter.headings) do |grid| | ||
grid.add_row(@presenter.extract_meta(column).values) | ||
TerminalTable.new(headings: presenter.headings) do |terminal_table| | ||
terminal_table.add_row(@presenter.extract_meta(column).values) | ||
end.render | ||
end | ||
|
||
def render_indexes | ||
Indexes.new(klass, column.name, colorize: presenter.option.colorize).render | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.