Class: CountVonCount::Counter
- Inherits:
-
Object
- Object
- CountVonCount::Counter
- Defined in:
- lib/count_von_count/counter.rb
Instance Attribute Summary collapse
-
#code_paths ⇒ Object
Returns the value of attribute code_paths.
-
#formatter ⇒ Object
Returns the value of attribute formatter.
-
#path ⇒ Object
Returns the value of attribute path.
-
#test_paths ⇒ Object
Returns the value of attribute test_paths.
Instance Method Summary collapse
- #expand_globs(globs) ⇒ Object
-
#initialize(path, code_paths, test_paths, formatter) ⇒ Counter
constructor
A new instance of Counter.
- #process_files(files) ⇒ Object
- #rel_path(p) ⇒ Object
- #run ⇒ Object
- #sort_by_loc(hash) ⇒ Object
Constructor Details
#initialize(path, code_paths, test_paths, formatter) ⇒ Counter
Returns a new instance of Counter.
6 7 8 9 10 11 12 |
# File 'lib/count_von_count/counter.rb', line 6 def initialize(path, code_paths, test_paths, formatter) @path = path @formatter = formatter @code_paths = (code_paths) raise "No files found in globs" if @code_paths.empty? @test_paths = (test_paths) end |
Instance Attribute Details
#code_paths ⇒ Object
Returns the value of attribute code_paths.
4 5 6 |
# File 'lib/count_von_count/counter.rb', line 4 def code_paths @code_paths end |
#formatter ⇒ Object
Returns the value of attribute formatter.
4 5 6 |
# File 'lib/count_von_count/counter.rb', line 4 def formatter @formatter end |
#path ⇒ Object
Returns the value of attribute path.
4 5 6 |
# File 'lib/count_von_count/counter.rb', line 4 def path @path end |
#test_paths ⇒ Object
Returns the value of attribute test_paths.
4 5 6 |
# File 'lib/count_von_count/counter.rb', line 4 def test_paths @test_paths end |
Instance Method Details
#expand_globs(globs) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/count_von_count/counter.rb', line 14 def (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) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# 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) ⇒ Object
56 57 58 |
# File 'lib/count_von_count/counter.rb', line 56 def rel_path(p) Pathname.new(p).relative_path_from(@path).to_s end |
#run ⇒ Object
22 23 24 25 26 27 |
# 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) ⇒ Object
52 53 54 |
# 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 |