Module: Moult::Formatters::DuplicationTable

Defined in:
lib/moult/formatters/duplication_table.rb

Overview

Human-readable table of duplication candidates. Renders from the same DuplicationReport as the JSON formatter so the two cannot disagree. Sorting already happened in Duplication; this layer owns column formatting only.

The heading is deliberate: these are confidence-graded candidates, never certainties.

Constant Summary collapse

MAX_LOCATIONS =
3
RIGHT_ALIGNED =

CONF, MASS, COUNT

[0, 2, 4].freeze

Class Method Summary collapse

Class Method Details

.conf(value) ⇒ Object



50
51
52
# File 'lib/moult/formatters/duplication_table.rb', line 50

def conf(value)
  format("%.2f", value)
end

.heading(count) ⇒ Object



29
30
31
# File 'lib/moult/formatters/duplication_table.rb', line 29

def heading(count)
  "Duplication candidates (confidence-graded — not certainties): #{count} clone sets"
end

.locations(occurrences) ⇒ Object



44
45
46
47
48
# File 'lib/moult/formatters/duplication_table.rb', line 44

def locations(occurrences)
  shown = occurrences.first(MAX_LOCATIONS).map { |o| "#{o.path}:#{o.line}" }
  extra = occurrences.size - shown.size
  extra.positive? ? "#{shown.join(", ")} (+#{extra} more)" : shown.join(", ")
end

.render(report) ⇒ String

Parameters:

Returns:

  • (String)


20
21
22
23
24
25
26
27
# File 'lib/moult/formatters/duplication_table.rb', line 20

def render(report)
  findings = report.findings
  return "No duplication found." if findings.empty?

  headers = %w[CONF KIND MASS NODE COUNT LOCATIONS]
  rows = findings.map { |f| row(f) }
  [heading(findings.size), "", TextTable.render(headers, rows, right_aligned: RIGHT_ALIGNED)].join("\n")
end

.row(finding) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/moult/formatters/duplication_table.rb', line 33

def row(finding)
  [
    conf(finding.confidence),
    finding.kind.to_s,
    finding.mass.to_s,
    finding.node_type,
    finding.occurrences.size.to_s,
    locations(finding.occurrences)
  ]
end