class RamperFileMetrics

Constants

CSV_COLS

self.method blows up unless it is after the functions are defined

METRICS

Public Class Methods

new(flags) click to toggle source
# File lib/ramper_file_metrics.rb, line 6
def initialize(flags)
  if flags.empty?
    selected = Array.new(CSV_COLS.count, true)
  else
    selected    = Array.new(CSV_COLS.count, false)
    selected[0] = true
    #TODO: use command line parser
    #TODO: change array to hash
    flags.each do |f|
      case f
        when '-a'
          selected[3] = true
        when '-c'
          selected[2] = true
        when '-f'
          selected[4] = true
        when '-l'
          selected[1] = true
      end
    end
  end

  @csv_header       = CSV_COLS.select.with_index { |_, ind| selected[ind] }
  @selected_metrics = METRICS.select.with_index { |_, ind| selected[ind] }
end

Private Class Methods

author_stats(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 65
def self.author_stats(file)
  authors            = get_git_author_stats(file)
  author_table       = authors.map { |line| /\s+(\d+)\t(.*) <(.*)>/.match(line) { |_| { :count => $1.to_i, :name => $2, :email => $3 } } }
  unique_email_table = combine_authors(author_table, :email)
  unique_table       = combine_authors(unique_email_table, :name)
  num_authors        = unique_table.count
  top_author         = unique_table.max { |a, b| a[:count] <=> b[:count] }
  top_author         = "#{top_author[:name]} <#{top_author[:email]}>"

  [top_author, num_authors]
end
combine_authors(original_table, combine_key) click to toggle source
# File lib/ramper_file_metrics.rb, line 77
def self.combine_authors(original_table, combine_key)
  other_key  = combine_key == :name ? :email : :name
  result_map = Hash.new { |h, k| h[k]=[] }
  original_table.each { |row| result_map[row[combine_key]].push(row.reject { |k, _| k==combine_key }) }
  result_map.map { |key, val| { :count      => val.inject(0) { |tot_count, x| tot_count + x[:count] },
                                combine_key => key,
                                other_key   => val.max { |a, b| a[:count] <=> b[:count] }[other_key] } }
end
flog_file(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 86
def self.flog_file(file)
  if /.*\.rb/.match(file)
    flogger = FlogCLI.new
    flogger.flog(file)
    flog_tot = flogger.total_score.round(2)
    flog_avg = flogger.average.round(2)

    [flog_tot, flog_avg]
  else
    Array.new(2, "N/A")
  end
end
get_git_author_stats(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 61
def self.get_git_author_stats(file)
  `git log --pretty=short --no-merges -- #{file} | git shortlog -sne`.split("\n")
end
lines_of_code(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 57
def self.lines_of_code(file)
  `wc -l #{file}`.split(" ").first.strip
end
start_end_dates_commits(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 48
def self.start_end_dates_commits(file)
  dates       = `git log -- #{file} | grep 'Date: '`.split("\n")
  start_date  = DateTime.parse(dates.last.strip.gsub(/Date:   /, ''))
  end_date    = DateTime.parse(dates.first.strip.gsub(/Date:   /, ''))
  num_commits = dates.count

  [start_date, end_date, num_commits]
end

Public Instance Methods

process_file(file) click to toggle source
# File lib/ramper_file_metrics.rb, line 36
def process_file(file)
  @selected_metrics.map.with_index do |m, ind|
    begin
      m.call(file)
    rescue => e
      Array.new(@csv_header[ind].count, "Error #{e.message}".gsub(",", ";"))
    end
  end.flatten.join(', ')
end
process_file_format() click to toggle source
# File lib/ramper_file_metrics.rb, line 32
def process_file_format
  @csv_header.flatten.join(', ')
end