Class: StudFinder::Scorer

Inherits:
Object
  • Object
show all
Defined in:
lib/stud_finder/scorer.rb

Defined Under Namespace

Classes: ValidationError

Constant Summary collapse

DEFAULT_WEIGHTS =
{ fan_in: 0.25, fan_out: 0.10, complexity: 0.25, churn: 0.25, coverage: 0.15 }.freeze
RENORMALIZED_KEYS =
%i[fan_in fan_out complexity churn].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files:, fan_in:, fan_out:, complexity:, churn:, churn_lines: nil, coverage: nil, weights: DEFAULT_WEIGHTS, branch_threshold: 50, trunk_threshold: 85, coupling: nil) ⇒ Scorer

Returns a new instance of Scorer.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/stud_finder/scorer.rb', line 14

def initialize(files:, fan_in:, fan_out:, complexity:, churn:, churn_lines: nil, coverage: nil,
               weights: DEFAULT_WEIGHTS, branch_threshold: 50, trunk_threshold: 85, coupling: nil)
  @files = files
  @fan_in = fan_in
  @fan_out = fan_out
  @complexity = complexity
  @churn = churn
  @churn_lines = churn_lines || churn
  @coverage = coverage
  @weights = weights
  @branch_threshold = branch_threshold
  @trunk_threshold = trunk_threshold
  @coupling = coupling
  validate!
  @normalized_weights = normalize_weights
end

Instance Attribute Details

#normalized_weightsObject (readonly)

Returns the value of attribute normalized_weights.



12
13
14
# File 'lib/stud_finder/scorer.rb', line 12

def normalized_weights
  @normalized_weights
end

Instance Method Details

#callObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stud_finder/scorer.rb', line 31

def call
  pcts = {
    fan_in: Normalizer.percentile_rank(@fan_in, @files),
    fan_out: Normalizer.percentile_rank(@fan_out, @files),
    complexity: Normalizer.percentile_rank(@complexity, @files),
    churn: composite_churn_pct,
    instability: instability_pct,
    coupling: coupling_pct
  }

  rows = @files.each_with_index.map do |file, index|
    score = weighted_score(file, pcts)
    [index, result_row(file, score, pcts)]
  end

  rows.sort_by { |index, row| [-row[:score], index] }
      .map.with_index(1) do |(_index, row), rank|
    row.merge(rank: rank)
  end
end