class AttributeStats::TabularFormatter
Public Class Methods
new(options: {}, table_info: {}, migration: nil)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 6 def initialize(options: {}, table_info: {}, migration: nil) @options, @table_info, @migration = options, table_info, migration @buffer = '' end
Public Instance Methods
output_all_attributes()
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 11 def output_all_attributes attribute_results = [] @table_info.each do |table_info| table_info.attributes.sort_by(&:usage_percent).each do |attribute| attribute_results << { model: table_info.name, set_count: attribute.count, set_percent: (attribute.usage_percent * 100).round(1).to_s.rjust(5), attribute_name: attribute.name } end end if attribute_results.empty? puts "No attributes found" else print_table attribute_results, title: 'Attributes Utilization' end @buffer end
output_attribute_references()
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 31 def output_attribute_references output = [] @table_info.each do |table_info| table_info.attributes.each do |attribute| next unless attribute.empty? output << { model: table_info.name, attribute_name: attribute.name, all_references: attribute.total_references, code: attribute.references['app'], specs: attribute.references['spec'], views: attribute.references['views'] } end end output.sort!{|a,b| a[:all_references] <=> b[:all_references]} if output.empty? puts "No unused attributes found" else print_table output, title: ['Attribute Code References', 'Mentions of the attribute name in app/ (except assets), lib/, config/, and spec/', '(symbol or dot notation, or wrapped in quotes)'] end @buffer end
output_dormant_tables()
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 55 def output_dormant_tables output = [] @table_info.each do |table_info| date = table_info.last_updated output << { model: table_info.name, last_updated: date.nil? ? 'Never Updated' : table_info.last_updated.to_date.to_s(:long) } if table_info.dormant? end if output.empty? puts "No dormant tables" else print_table output, title: ['Dormant Tables', "No updated_ats after #{@options[:dormant_table_age].to_date.to_s(:long)}"] end @buffer end
output_unused_attributes()
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 72 def output_unused_attributes output = [] @table_info.each do |table_info| table_info.attributes.sort_by(&:name).each do |attribute| output << { model: table_info.name, attribute_name: attribute.name } if attribute.empty? end end unused_values = ['Nil', 'Empty'] unused_values << 'Default Values' if @options[:consider_defaults_unused] if output.empty? puts "No unused attributes (good for you!)" else print_table output, title: ['Unused Attributes', unused_values.join(', ')] end @buffer end
Private Instance Methods
formatted_headers(column_names)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 137 def formatted_headers(column_names) column_names.inject({}) do |hash, name| hash[name] = name.to_s.titleize hash end end
header_order(column_names)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 131 def header_order(column_names) [:model, :table_name, :attribute_name, :last_updated, :set_count, :set_percent, :code, :views, :specs] & column_names end
output_table_title(title_rows, table_output)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 118 def output_table_title(title_rows, table_output) header_line = table_output.split(/\n/).first puts '', header_line Array(title_rows).each do |title| if header_line.length > title.length+4 puts "| #{title.center(header_line.length - 4)} |" else puts "| #{title}" end end end
print_section_header(text)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 104 def print_section_header(text) line = '#'*(text.length+4) puts '', line, " #{text} ", line end
print_table(data, title: nil)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 109 def print_table(data, title: nil) column_names = data.first.keys output = Hirb::Helpers::AutoTable.render(data, fields: header_order(column_names), headers: formatted_headers(column_names)) output_table_title(title, output) unless title.blank? puts output end
print_table_header(table_info)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 99 def print_table_header(table_info) line = '-'*(table_info.name.length+4) puts '', line, " #{table_info.name} ", line end
puts(*values)
click to toggle source
# File lib/formatters/tabular_formatter.rb, line 94 def puts(*values) @buffer << values.join("\n") @buffer << "\n" end