|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "skunk/generators/html/file_data" |
| 4 | +require "skunk/generators/html/path_truncator" |
| 5 | + |
| 6 | +module Skunk |
| 7 | + module Generator |
| 8 | + module Html |
| 9 | + # Data object for the HTML overview report |
| 10 | + class OverviewData |
| 11 | + attr_reader :generated_at, :skunk_version, |
| 12 | + :analysed_modules_count, :skunk_score_total, :skunk_score_average, |
| 13 | + :worst_pathname, :worst_score, :files |
| 14 | + |
| 15 | + def initialize(analysed_modules) |
| 16 | + @analysed_modules = analysed_modules |
| 17 | + @generated_at = Time.now.strftime("%Y-%m-%d %H:%M:%S") |
| 18 | + @skunk_version = Skunk::VERSION |
| 19 | + |
| 20 | + @analysed_modules_count = non_test_modules.count |
| 21 | + @skunk_score_total = non_test_modules.sum(&:skunk_score) |
| 22 | + @skunk_score_average = calculate_average |
| 23 | + @worst_pathname = PathTruncator.truncate(find_worst_module&.pathname) |
| 24 | + @worst_score = find_worst_module&.skunk_score |
| 25 | + @files = build_files |
| 26 | + end |
| 27 | + |
| 28 | + private |
| 29 | + |
| 30 | + def non_test_modules |
| 31 | + @non_test_modules ||= @analysed_modules.reject do |a_module| |
| 32 | + module_path = a_module.pathname.dirname.to_s |
| 33 | + module_path.start_with?("test", "spec") || module_path.end_with?("test", "spec") |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def calculate_average |
| 38 | + return 0 if @analysed_modules_count.zero? |
| 39 | + |
| 40 | + (@skunk_score_total.to_f / @analysed_modules_count).round(2) |
| 41 | + end |
| 42 | + |
| 43 | + def find_worst_module |
| 44 | + @find_worst_module ||= sorted_modules.first |
| 45 | + end |
| 46 | + |
| 47 | + def sorted_modules |
| 48 | + @sorted_modules ||= non_test_modules.sort_by(&:skunk_score).reverse! |
| 49 | + end |
| 50 | + |
| 51 | + def build_files |
| 52 | + @build_files ||= sorted_modules.map do |module_data| |
| 53 | + FileData.new(module_data) |
| 54 | + end |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + end |
| 59 | +end |
0 commit comments