Class: Uniword::Warnings::WarningReport

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/warnings/warning_report.rb

Overview

Aggregated warning report.

Responsibility: Aggregate and report warnings. Single Responsibility: Only manages warning reporting.

A warning report includes:

  • Collection of all warnings

  • Element occurrence counts

  • Grouping by severity

  • Summary statistics

Examples:

Create a report

report = WarningReport.new(
  warnings: warnings,
  element_counts: { 'chart' => 5, 'smartArt' => 2 }
)
puts report.summary

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(warnings:, element_counts:, config: {}) ⇒ WarningReport

Initialize a new warning report.

Examples:

Create a report

report = WarningReport.new(
  warnings: [warning1, warning2],
  element_counts: { 'chart' => 3 }
)

Parameters:

  • warnings (Array<Warning>)

    Warnings to include

  • element_counts (Hash<String, Integer>)

    Element counts

  • config (Hash) (defaults to: {})

    Configuration



42
43
44
45
46
# File 'lib/uniword/warnings/warning_report.rb', line 42

def initialize(warnings:, element_counts:, config: {})
  @warnings = warnings
  @element_counts = element_counts
  @config = config
end

Instance Attribute Details

#element_countsHash<String, Integer> (readonly)

Returns Element occurrence counts.

Returns:

  • (Hash<String, Integer>)

    Element occurrence counts



29
30
31
# File 'lib/uniword/warnings/warning_report.rb', line 29

def element_counts
  @element_counts
end

#warningsArray<Warning> (readonly)

Returns All warnings.

Returns:

  • (Array<Warning>)

    All warnings



26
27
28
# File 'lib/uniword/warnings/warning_report.rb', line 26

def warnings
  @warnings
end

Instance Method Details

#any?Boolean

Check if report has any warnings.

Returns:

  • (Boolean)

    true if warnings exist



51
52
53
# File 'lib/uniword/warnings/warning_report.rb', line 51

def any?
  @warnings.any?
end

#error_countInteger

Get count of error warnings.

Returns:

  • (Integer)

    Error count



79
80
81
# File 'lib/uniword/warnings/warning_report.rb', line 79

def error_count
  errors.count
end

#errorsArray<Warning>

Get error-level warnings.

Returns:

  • (Array<Warning>)

    Error warnings



58
59
60
# File 'lib/uniword/warnings/warning_report.rb', line 58

def errors
  @warnings.select(&:error?)
end

#info_countInteger

Get count of info warnings.

Returns:

  • (Integer)

    Info count



93
94
95
# File 'lib/uniword/warnings/warning_report.rb', line 93

def info_count
  infos.count
end

#infosArray<Warning>

Get info-level warnings.

Returns:

  • (Array<Warning>)

    Info warnings



72
73
74
# File 'lib/uniword/warnings/warning_report.rb', line 72

def infos
  @warnings.select(&:info?)
end

#summaryString

Generate summary text.

Returns:

  • (String)

    Summary text



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/uniword/warnings/warning_report.rb', line 107

def summary
  return "No warnings" unless any?

  lines = []
  lines << "Found #{total_count} warning(s):"
  lines << "  Errors: #{error_count}"
  lines << "  Warnings: #{warning_count}"
  lines << "  Info: #{info_count}"

  if @element_counts.any?
    lines << ""
    lines << "Unsupported elements encountered:"

    @element_counts.sort_by { |_, count| -count }.each do |element, count|
      lines << "  #{element}: #{count} occurrence(s)"
    end
  end

  lines.join("\n")
end

#to_hHash

Convert to hash representation.

Returns:

  • (Hash)

    Hash representation



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/uniword/warnings/warning_report.rb', line 138

def to_h
  {
    total: total_count,
    by_severity: {
      errors: error_count,
      warnings: warning_count,
      infos: info_count,
    },
    element_counts: @element_counts,
    warnings: @warnings.map(&:to_h),
  }
end

#to_json(*_args) ⇒ String

Export to JSON string.

Returns:

  • (String)

    JSON representation



131
132
133
# File 'lib/uniword/warnings/warning_report.rb', line 131

def to_json(*_args)
  JSON.pretty_generate(to_h)
end

#to_sString

Convert to string for display.

Returns:

  • (String)

    String representation



154
155
156
# File 'lib/uniword/warnings/warning_report.rb', line 154

def to_s
  summary
end

#total_countInteger

Get total warning count.

Returns:

  • (Integer)

    Total count



100
101
102
# File 'lib/uniword/warnings/warning_report.rb', line 100

def total_count
  @warnings.count
end

#warning_countInteger

Get count of warning-level warnings.

Returns:

  • (Integer)

    Warning count



86
87
88
# File 'lib/uniword/warnings/warning_report.rb', line 86

def warning_count
  warnings_only.count
end

#warnings_onlyArray<Warning>

Get warning-level warnings.

Returns:

  • (Array<Warning>)

    Warning-level warnings



65
66
67
# File 'lib/uniword/warnings/warning_report.rb', line 65

def warnings_only
  @warnings.select(&:warning?)
end