Class: Attractor::BaseCalculator

Inherits:
Object
  • Object
show all
Defined in:
lib/attractor/calculators/base_calculator.rb

Overview

calculates churn and complexity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_prefix: "", ignores: "", file_extension: "rb", minimum_churn_count: 3, start_ago: "5y", verbose: false) ⇒ BaseCalculator

Returns a new instance of BaseCalculator.



12
13
14
15
16
17
18
19
# File 'lib/attractor/calculators/base_calculator.rb', line 12

def initialize(file_prefix: "", ignores: "", file_extension: "rb", minimum_churn_count: 3, start_ago: "5y", verbose: false)
  @file_prefix = file_prefix
  @file_extension = file_extension
  @minimum_churn_count = minimum_churn_count
  @start_date = Date.today - Attractor::DurationParser.new(start_ago).duration
  @ignores = ignores
  @verbose = verbose
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/attractor/calculators/base_calculator.rb', line 10

def type
  @type
end

Instance Method Details

#calculateObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/attractor/calculators/base_calculator.rb', line 21

def calculate
  churn = ::Churn::ChurnCalculator.new(
    file_extension: @file_extension,
    file_prefix: @file_prefix,
    minimum_churn_count: @minimum_churn_count,
    start_date: @start_date,
    ignores: @ignores
  ).report(false)

  puts "Calculating churn and complexity values for #{churn[:churn][:changes].size} #{type} files" if @verbose

  values = churn[:churn][:changes].map do |change|
    history = git_history_for_file(file_path: change[:file_path])
    commit = history&.first&.first

    cached_value = Cache.read(file_path: change[:file_path])

    if !cached_value.nil? && !cached_value.current_commit.nil? && cached_value.current_commit == commit
      value = cached_value
    else
      complexity, details = yield(change)

      value = Value.new(file_path: change[:file_path],
        churn: change[:times_changed],
        complexity: complexity,
        details: details,
        history: history)
      Cache.write(file_path: change[:file_path], value: value)
    end

    print "." if @verbose
    value
  end

  Cache.persist!

  print "\n\n" if @verbose

  values
end