Skip to content

Commit

Permalink
The default value will now output with the correct color
Browse files Browse the repository at this point in the history
  • Loading branch information
otorain committed Nov 25, 2024
1 parent 047b9ac commit 564de76
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 100 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# TableInspector
![Test coverage](https://img.shields.io/badge/Test_coverage-99.65%25-green)
![Test coverage](https://img.shields.io/badge/Test_coverage-96.71%25-green)
![Release version](https://img.shields.io/badge/Release-v0.6.0-green)
![Workflow badge](https://github.com/otorain/table_inspector/actions/workflows/run_test.yml/badge.svg)

Expand Down
3 changes: 3 additions & 0 deletions lib/table_inspector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
require "table_inspector/indexes"
require "table_inspector/column"
require "table_inspector/presenter"
require "table_inspector/presenter/sqlite3"
require "table_inspector/presenter/postgresql"
require "table_inspector/presenter/mysql2"
require "table_inspector/model_validator"
require "table_inspector/column_validator"
require "table_inspector/presenter_option"
Expand Down
2 changes: 1 addition & 1 deletion lib/table_inspector/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Column
def initialize(klass, column_name, presenter_option)
@column = klass.columns.find { |column| column.name == column_name.to_s }
@klass = klass
@presenter = Presenter.new(klass, presenter_option)
@presenter = Presenter.current.new(klass, presenter_option)
end

def render
Expand Down
64 changes: 50 additions & 14 deletions lib/table_inspector/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,44 @@ def headings
ordered_keys.map(&:humanize)
end

def self.current
adapter = ApplicationRecord.connection_db_config.adapter.camelize
available_presenter = %w[Mysql2 Postgresql Sqlite3]

if available_presenter.include?(adapter)
"TableInspector::Presenter::#{adapter}".constantize
else
TableInspector::Presenter
end
end

private

def extract_meta_without_highlight(column)
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
column_data["default"] = if column_data["type"] == "string"
column_data["default"]&.inspect
else
column_data["default"]
end
end
end

def extract_meta_with_highlight(column)
column_data = extract_meta_without_highlight(column)

# Colorize text but except "comment" field
column_data.each do |k, v|
if k != "comment"
column_data[k] = colorize(v)
# Colorize text except comment
column_data.except("comment").each do |k, v|
if k == "default"
column_data[k] = colorize(v, column.type)
else
column_data[k]
column_data[k] = colorize(v)
end
end

column_data.slice(*ordered_keys)
end

def extract_meta_without_highlight(column)
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
if column_data["default"] == ""
column_data["default"] = column_data["default"].inspect
end
end
end

def ordered_keys
if option.comment_only
%w[name comment]
Expand All @@ -56,7 +69,30 @@ def ordered_keys
end
end

def colorize(value)
def colorize(value, type = nil)
if type
colorize_by_type(value, type)
else
colorize_by_value(value)
end
end

def colorize_by_type(value, type)
case type
when :datetime
green(value)
when :boolean
value ? green(value) : red(value)
when :integer, :decimal
blue(value)
when :string, :json, :jsonb
yellow(value)
else
value
end
end

def colorize_by_value(value)
case value
when TrueClass, DateTime, 'datetime'
green(value)
Expand Down
14 changes: 14 additions & 0 deletions lib/table_inspector/presenter/mysql2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class TableInspector::Presenter::Mysql2 < TableInspector::Presenter
def extract_meta_without_highlight(column)
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
column_data["default"] = case column_data["type"]
when "string"
column_data["default"]&.inspect
when "boolean"
{ "1" => true, "0" => false }[column_data["default"]]
else
column_data["default"]
end
end
end
end
15 changes: 15 additions & 0 deletions lib/table_inspector/presenter/postgresql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class TableInspector::Presenter::Postgresql < TableInspector::Presenter
def extract_meta_without_highlight(column)
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
column_data["default"] = case column_data["type"]
when "string"
column_data["default"]&.inspect
when "boolean"
{ "true" => true, "false" => false }[column_data["default"]]
else
column_data["default"]
end
end
end
end
15 changes: 15 additions & 0 deletions lib/table_inspector/presenter/sqlite3.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class TableInspector::Presenter::Sqlite3 < TableInspector::Presenter

def extract_meta_without_highlight(column)
column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys).tap do |column_data|
column_data["default"] = case column_data["type"]
when "string"
column_data["default"]&.inspect
when "boolean"
{ "1" => true, "0" => false }[column_data["default"]]
else
column_data["default"]
end
end
end
end
2 changes: 1 addition & 1 deletion lib/table_inspector/table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Table

def initialize(klass, presenter_option)
@klass = klass
@presenter = Presenter.new(klass, presenter_option)
@presenter = Presenter.current.new(klass, presenter_option)
@presenter_option = presenter_option
end

Expand Down
83 changes: 0 additions & 83 deletions spec/lib/presenter_spec.rb

This file was deleted.

0 comments on commit 564de76

Please sign in to comment.