Class: Uniword::Quality::QualityReport

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/quality/quality_report.rb

Overview

Manages and reports document quality violations.

Responsibility: Collect, organize, and export quality check results. Single Responsibility - only handles report generation and export.

Examples:

Generate and use report

report = QualityReport.new
report.add_violation(violation)
puts "Valid: #{report.valid?}"
puts "Errors: #{report.error_count}"
report.export_html('report.html')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQualityReport

Initialize quality report



23
24
25
26
# File 'lib/uniword/quality/quality_report.rb', line 23

def initialize
  @violations = []
  @checked_at = Time.now
end

Instance Attribute Details

#checked_atObject (readonly)

Returns the value of attribute checked_at.



20
21
22
# File 'lib/uniword/quality/quality_report.rb', line 20

def checked_at
  @checked_at
end

#violationsObject (readonly)

Returns the value of attribute violations.



20
21
22
# File 'lib/uniword/quality/quality_report.rb', line 20

def violations
  @violations
end

Instance Method Details

#add_violation(violation) ⇒ void

This method returns an undefined value.

Add a violation to the report

Parameters:



32
33
34
# File 'lib/uniword/quality/quality_report.rb', line 32

def add_violation(violation)
  @violations << violation
end

#add_violations(violations) ⇒ void

This method returns an undefined value.

Add multiple violations to the report

Parameters:



40
41
42
# File 'lib/uniword/quality/quality_report.rb', line 40

def add_violations(violations)
  @violations.concat(violations)
end

#by_ruleHash{String => Array<QualityViolation>}

Get violations grouped by rule

Returns:



93
94
95
# File 'lib/uniword/quality/quality_report.rb', line 93

def by_rule
  violations.group_by(&:rule)
end

#by_severityHash{Symbol => Array<QualityViolation>}

Get violations grouped by severity

Returns:



82
83
84
85
86
87
88
# File 'lib/uniword/quality/quality_report.rb', line 82

def by_severity
  {
    error: violations.select(&:error?),
    warning: violations.select(&:warning?),
    info: violations.select(&:info?),
  }
end

#error_countInteger

Get count of error-level violations

Returns:

  • (Integer)

    Number of errors



54
55
56
# File 'lib/uniword/quality/quality_report.rb', line 54

def error_count
  violations.count(&:error?)
end

#export_html(file_path) ⇒ String

Export report as HTML

Parameters:

  • file_path (String)

    Path to save HTML file

Returns:

  • (String)

    HTML string



149
150
151
152
153
# File 'lib/uniword/quality/quality_report.rb', line 149

def export_html(file_path)
  html = generate_html
  File.write(file_path, html)
  html
end

#info_countInteger

Get count of info-level violations

Returns:

  • (Integer)

    Number of info items



68
69
70
# File 'lib/uniword/quality/quality_report.rb', line 68

def info_count
  violations.count(&:info?)
end

#summaryString

Get formatted summary text

Returns:

  • (String)

    Human-readable summary



158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/uniword/quality/quality_report.rb', line 158

def summary
  [
    "Document Quality Report",
    "=" * 50,
    "Checked at: #{checked_at}",
    "Status: #{valid? ? 'VALID' : 'INVALID'}",
    "",
    "Summary:",
    "  Total violations: #{total_count}",
    "  Errors: #{error_count}",
    "  Warnings: #{warning_count}",
    "  Info: #{info_count}",
  ].join("\n")
end

#to_json(file_path = nil) ⇒ String

Export report as JSON

Parameters:

  • file_path (String) (defaults to: nil)

    Path to save JSON file (optional)

Returns:

  • (String)

    JSON string



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/uniword/quality/quality_report.rb', line 101

def to_json(file_path = nil)
  json_data = {
    checked_at: checked_at.iso8601,
    summary: {
      total: total_count,
      errors: error_count,
      warnings: warning_count,
      info: info_count,
      valid: valid?,
    },
    violations: violations.map(&:to_h),
  }

  json_string = JSON.pretty_generate(json_data)

  File.write(file_path, json_string) if file_path

  json_string
end

#to_yaml(file_path = nil) ⇒ String

Export report as YAML

Parameters:

  • file_path (String) (defaults to: nil)

    Path to save YAML file (optional)

Returns:

  • (String)

    YAML string



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/uniword/quality/quality_report.rb', line 125

def to_yaml(file_path = nil)
  yaml_data = {
    "checked_at" => checked_at.iso8601,
    "summary" => {
      "total" => total_count,
      "errors" => error_count,
      "warnings" => warning_count,
      "info" => info_count,
      "valid" => valid?,
    },
    "violations" => violations.map(&:to_h),
  }

  yaml_string = YAML.dump(yaml_data)

  File.write(file_path, yaml_string) if file_path

  yaml_string
end

#total_countInteger

Get total violation count

Returns:

  • (Integer)

    Total number of violations



75
76
77
# File 'lib/uniword/quality/quality_report.rb', line 75

def total_count
  violations.count
end

#valid?Boolean

Check if document is valid (no errors)

Returns:

  • (Boolean)

    true if no error-level violations



47
48
49
# File 'lib/uniword/quality/quality_report.rb', line 47

def valid?
  error_count.zero?
end

#warning_countInteger

Get count of warning-level violations

Returns:

  • (Integer)

    Number of warnings



61
62
63
# File 'lib/uniword/quality/quality_report.rb', line 61

def warning_count
  violations.count(&:warning?)
end