class AttributeStats::SetAttributeReferences

Attributes

table_info[R]

Public Class Methods

new(table_info: [], options: {}) click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 7
def initialize(table_info: [], options: {})
  @table_info, @options = table_info, options
end

Public Instance Methods

execute() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 11
def execute
  @executed ||= begin
    lookup_and_set_attribute_counts
    true
  end
end

Private Instance Methods

fetch_table_stats() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 32
def fetch_table_stats
  print("Finding Code References #{in_color(@current_table.name,@table_count)} ") if @options[:verbose]
  set_reference_counts
  erase_line if @options[:verbose]
end
lookup_and_set_attribute_counts() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 20
def lookup_and_set_attribute_counts
  @table_info.each_with_index do |t,index|
    @table_count = index
    @current_table = t
    fetch_table_stats
  end
  erase_line if @options[:verbose]
  true
end
regex_base_path() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 101
def regex_base_path
  @options[:rails_root]
end
regex_exclude_paths() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 80
def regex_exclude_paths
  directories = @options[:exclude_directories] || %w(app/assets db public)
  if regex_base_path
    directories.map do |directory|
      File.join regex_base_path, directory
    end
  else
    directories
  end
end
regex_target_paths() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 69
def regex_target_paths
  directories = @options[:include_directories] || %w(app lib config spec)
  if regex_base_path
    directories.map do |directory|
      File.join regex_base_path, directory
    end
  else
    directories
  end
end
regexes(attribute_info) click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 59
def regexes(attribute_info)
  [
    "\\\"#{attribute_info.name}\\\"",                # "my_attribute_name"
    "'#{attribute_info.name}'",                      # 'my_attribute_name'
    "\\:#{attribute_info.name}[^A-Za-z_]?",           # :my_attribute_name
    "\\.#{attribute_info.name}[^A-Za-z_]?",           # .my_attribute_name
    # "[^A-Za-z_]#{attribute_info.name}[^A-Za-z_]",  #  my_attribute_name (standalone word)
  ]
end
section(filename) click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 91
def section(filename)
  if filename =~ /\A#{regex_base_path}\/spec\//
    'spec'
  elsif filename =~ /\A#{regex_base_path}\/app\/views\//
    'views'
  else
    'app'
  end
end
set_attribute_reference_count(attribute_info) click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 46
def set_attribute_reference_count(attribute_info)
  files = `grep -rcE "#{regexes(attribute_info).join('|')}" --exclude-dir={#{regex_exclude_paths.join(',')}} #{regex_target_paths.join(' ')}`
  attribute_total = 0
  files.scan(/(.*):(\d+)(?:\n|\z)/).each do |matches|
    next if matches[0].nil?
    filename, count = matches[0], matches[1].to_i
    next if count == 0
    attribute_total += count
    attribute_info.set_reference section(filename), count
  end
  attribute_total
end
set_reference_counts() click to toggle source
# File lib/stats_generation/set_attribute_references.rb, line 38
def set_reference_counts
  @current_table.unused_attribute_info.each do |attribute_info|
    count = set_attribute_reference_count(attribute_info)
    next unless @options[:verbose]
    print count.zero? ? green('✓') : red('x')
  end
end