class CountVonCount::Counter

Attributes

code_paths[RW]
formatter[RW]
path[RW]
test_paths[RW]

Public Class Methods

new(path, code_paths, test_paths, formatter) click to toggle source
# File lib/count_von_count/counter.rb, line 6
def initialize(path, code_paths, test_paths, formatter)
  @path = path
  @formatter = formatter
  @code_paths = expand_globs(code_paths)
  raise "No files found in globs" if @code_paths.empty?
  @test_paths = expand_globs(test_paths)
end

Public Instance Methods

expand_globs(globs) click to toggle source
# File lib/count_von_count/counter.rb, line 14
def expand_globs(globs)
  Dir.chdir(@path) do
    globs.flat_map do |g|
      Pathname.glob(g).reject{|f| f.directory? }.map(&:realpath)
    end
  end
end
process_files(files) click to toggle source
# File lib/count_von_count/counter.rb, line 29
def process_files(files)
  files_by_dir = files.group_by(&:dirname)
  all_stats = []
  by_dir = {}
  by_file = {}
  files_by_dir.each do |dir, dir_files|
    stats_for_dir = []
    dir_files.each do |file|
      stat = Stat.new
      stat.process(file)
      unless stat.empty?
        by_file[rel_path(file)] = stat.to_h
        stats_for_dir.push(stat)
        all_stats.push(stat)
      end
    end
    dir_sum = Stat.sum(stats_for_dir)
    by_dir[rel_path(dir)] = dir_sum.to_h unless dir_sum.empty?
  end
  total = Stat.sum(all_stats).to_h
  {total: total, by_dir: sort_by_loc(by_dir), by_file: sort_by_loc(by_file)}
end
rel_path(p) click to toggle source
# File lib/count_von_count/counter.rb, line 56
def rel_path(p)
  Pathname.new(p).relative_path_from(@path).to_s
end
run() click to toggle source
# File lib/count_von_count/counter.rb, line 22
def run
  results = {}
  results[:code] = process_files(code_paths)
  results[:tests] = process_files(test_paths) unless test_paths.empty?
  formatter.write_output(results)
end
sort_by_loc(hash) click to toggle source
# File lib/count_von_count/counter.rb, line 52
def sort_by_loc(hash)
  hash.sort_by { |_k, stat| stat[:loc] }.reverse.to_h
end