class AttributeStats::StatsGenerator

Constants

DEFAULT_OPTIONS

Attributes

migration[R]
options[R]
table_info[R]

Public Class Methods

new(opts={}) click to toggle source
# File lib/stats_generation/stats_generator.rb, line 13
def initialize(opts={})
  @options = DEFAULT_OPTIONS.merge(opts)
end

Public Instance Methods

attribute_usage() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 17
def attribute_usage
  fetch_attribute_usage
  output formatter.output_all_attributes
end
dormant_tables() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 22
def dormant_tables
  fetch_dormant_tables
  output formatter.output_dormant_tables
end
generate_migration() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 32
def generate_migration
  fetch_empty_attributes
  GenerateMigration.new(table_info: table_info, options: options).output_migration
end
inspect() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 57
def inspect
  "StatsGenerator(results: #{table_info.to_s[0..200]}, options: #{options})"
end
set_formatter(formatter_type) click to toggle source
# File lib/stats_generation/stats_generator.rb, line 43
def set_formatter(formatter_type)
  formatter_was = options[:formatter]
  case formatter_type.to_s.downcase
  when 'json'
    options[:formatter] = :json
  when 'tabular'
    options[:formatter] = :tabular
  when 'hash'
    options[:formatter] = :hash
  end
  @formatter = nil unless options[:formatter] == formatter_was
  options[:formatter]
end
unused_attribute_references() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 37
def unused_attribute_references
  fetch_empty_attributes
  SetAttributeReferences.new(table_info: table_info, options: options).execute
  output formatter.output_attribute_references
end
unused_attributes() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 27
def unused_attributes
  fetch_empty_attributes
  output formatter.output_unused_attributes
end

Private Instance Methods

attribute_stats_setter() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 101
def attribute_stats_setter
  @attribute_stats_setter ||= SetAttributeStats.new(table_info: table_info, options: options)
end
fetch_attribute_usage() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 85
def fetch_attribute_usage
  @fetch_attribute_usage ||= begin
    initialize_tables
    attribute_stats_setter.set_counts
    true
  end
end
fetch_dormant_tables() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 105
def fetch_dormant_tables
  @fetched_dormant_tables ||= begin
    initialize_tables
    SetDormantTables.new(table_info: table_info, options: options).call
    true
  end
end
fetch_empty_attributes() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 93
def fetch_empty_attributes
  @fetch_empty_attributes ||= begin
    initialize_tables
    attribute_stats_setter.set_empty
    true
  end
end
formatter() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 63
def formatter
  @formatter ||= begin
    output = case options[:formatter].to_s.downcase
    when 'json'
      JSONFormatter
    when 'hash'
      HashFormatter
    else
      TabularFormatter
    end.new(options: options, table_info: table_info, migration: @migration)
  end
end
initialize_tables() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 113
def initialize_tables
  return unless table_info.nil?
  @table_info = []
  tables.sort.each{|table| setup_table_and_model(table) }
end
output(value) click to toggle source
# File lib/stats_generation/stats_generator.rb, line 76
def output(value)
  return if value.nil?
  if options[:source] == :cli || value.is_a?(String)
    puts value
  else
    value
  end
end
setup_table_and_model(table) click to toggle source
# File lib/stats_generation/stats_generator.rb, line 119
def setup_table_and_model(table)
  ar_class = ActiveRecord::Base.descendants.detect{|klass| klass.table_name == table }
  return if ar_class.name == 'ActiveRecord::InternalMetadata' # Table for Rails 5 internal use only
  @table_info << TableData.new(ar_class || table.classify.constantize)
rescue NameError => ex
end
tables() click to toggle source
# File lib/stats_generation/stats_generator.rb, line 126
def tables
  ActiveRecord::Base.connection.data_sources
end