Class: Yard::Lint::Results::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/lint/results/aggregate.rb

Overview

Aggregates multiple validator results into a single result object

Constant Summary collapse

SEVERITY_ERROR =

Error severity level constant

'error'
SEVERITY_WARNING =

Warning severity level constant

'warning'
SEVERITY_CONVENTION =

Convention severity level constant

'convention'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(results, config = nil) ⇒ Aggregate

Initialize aggregate result with array of validator results

Parameters:

  • results (Array<Results::Base>)

    array of validator result objects

  • config (Config, nil) (defaults to: nil)

    configuration object



21
22
23
24
# File 'lib/yard/lint/results/aggregate.rb', line 21

def initialize(results, config = nil)
  @results = Array(results)
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



16
17
18
# File 'lib/yard/lint/results/aggregate.rb', line 16

def config
  @config
end

Instance Method Details

#clean?Boolean

Check if there are no offenses

Returns:

  • (Boolean)

    true if no offenses found



40
41
42
# File 'lib/yard/lint/results/aggregate.rb', line 40

def clean?
  offenses.empty?
end

#countInteger

Total number of offenses

Returns:

  • (Integer)

    offense count



34
35
36
# File 'lib/yard/lint/results/aggregate.rb', line 34

def count
  offenses.count
end

#exit_codeInteger

Determine exit code based on configured fail_on_severity Uses the config object stored during initialization

Returns:

  • (Integer)

    0 for success, 1 for failure



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yard/lint/results/aggregate.rb', line 66

def exit_code
  return 0 if offenses.empty?
  return 0 unless @config # No config means don't fail

  fail_on = @config.fail_on_severity

  case fail_on
  when SEVERITY_ERROR
    statistics[:error].positive? ? 1 : 0
  when SEVERITY_WARNING
    (statistics[:error] + statistics[:warning]).positive? ? 1 : 0
  when SEVERITY_CONVENTION
    offenses.any? ? 1 : 0
  else
    0
  end
end

#offensesArray<Hash>

Get all offenses from all validators

Returns:

  • (Array<Hash>)

    flattened array of all offenses



28
29
30
# File 'lib/yard/lint/results/aggregate.rb', line 28

def offenses
  @results.flat_map(&:offenses)
end

#statisticsHash

Get offense statistics by severity

Returns:

  • (Hash)

    hash with severity counts (using symbol keys)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/yard/lint/results/aggregate.rb', line 46

def statistics
  stats = {
    error: 0,
    warning: 0,
    convention: 0,
    total: 0
  }

  offenses.each do |offense|
    severity = offense[:severity].to_sym
    stats[severity] += 1 if stats.key?(severity)
    stats[:total] += 1
  end

  stats
end