Class: AttributeStats::TabularFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/formatters/tabular_formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(options: {}, table_info: {}, migration: nil) ⇒ TabularFormatter

Returns a new instance of TabularFormatter.



6
7
8
9
# 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

Instance Method Details

#output_all_attributesObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 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_dormant_tablesObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/formatters/tabular_formatter.rb', line 31

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_attributesObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/formatters/tabular_formatter.rb', line 48

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