Class: Uniword::Validation::DocumentValidationReport

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/validation/document_validator.rb

Overview

Validation report for document validation.

Aggregates results from all validation layers.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ DocumentValidationReport

Initialize a new report.

Parameters:

  • file_path (String)

    Path to validated file



159
160
161
162
# File 'lib/uniword/validation/document_validator.rb', line 159

def initialize(file_path)
  @file_path = file_path
  @layer_results = {}
end

Instance Attribute Details

#file_pathString (readonly)

Returns Path to validated file.

Returns:

  • (String)

    Path to validated file



151
152
153
# File 'lib/uniword/validation/document_validator.rb', line 151

def file_path
  @file_path
end

#layer_resultsHash<String, LayerValidationResult> (readonly)

Returns Results by layer.

Returns:



154
155
156
# File 'lib/uniword/validation/document_validator.rb', line 154

def layer_results
  @layer_results
end

Instance Method Details

#add_layer_result(layer_name, result) ⇒ void

This method returns an undefined value.

Add a layer validation result.

Parameters:



169
170
171
# File 'lib/uniword/validation/document_validator.rb', line 169

def add_layer_result(layer_name, result)
  @layer_results[layer_name] = result
end

#critical_failures?Boolean

Check if any layer has critical failures.

Returns:

  • (Boolean)

    true if critical failures exist



210
211
212
# File 'lib/uniword/validation/document_validator.rb', line 210

def critical_failures?
  @layer_results.values.any?(&:critical_failures?)
end

#errorsArray<Hash>

Get all errors from all layers.

Returns:

  • (Array<Hash>)

    All errors with layer info



183
184
185
186
187
# File 'lib/uniword/validation/document_validator.rb', line 183

def errors
  @layer_results.flat_map do |layer, result|
    result.errors.map { |err| err.merge(layer: layer) }
  end
end

#infosArray<Hash>

Get all info messages from all layers.

Returns:

  • (Array<Hash>)

    All info messages with layer info



201
202
203
204
205
# File 'lib/uniword/validation/document_validator.rb', line 201

def infos
  @layer_results.flat_map do |layer, result|
    result.infos.map { |info| info.merge(layer: layer) }
  end
end

#summaryHash

Get summary statistics.

Returns:

  • (Hash)

    Summary data



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/uniword/validation/document_validator.rb', line 217

def summary
  {
    file: @file_path,
    valid: valid?,
    layers_validated: @layer_results.count,
    errors: errors.count,
    warnings: warnings.count,
    infos: infos.count,
    critical: critical_failures?,
  }
end

#to_json(*_args) ⇒ String

Export to JSON string.

Returns:

  • (String)

    JSON representation



232
233
234
235
236
237
# File 'lib/uniword/validation/document_validator.rb', line 232

def to_json(*_args)
  JSON.pretty_generate(
    summary: summary,
    layers: @layer_results.transform_values(&:to_h),
  )
end

#to_sString

Convert to string for display.

Returns:

  • (String)

    String representation



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/uniword/validation/document_validator.rb', line 242

def to_s
  lines = [
    "Document Validation Report: #{@file_path}",
    "=" * 60,
    "",
  ]

  @layer_results.each_value do |result|
    lines << result.to_s
  end

  lines << ""
  lines << "Summary:"
  lines << "  Valid: #{valid?}"
  lines << "  Errors: #{errors.count}"
  lines << "  Warnings: #{warnings.count}"
  lines << "  Info: #{infos.count}"

  if errors.any?
    lines << ""
    lines << "Errors:"
    errors.each do |error|
      lines << "  [#{error[:layer]}] #{error[:message]}"
    end
  end

  lines.join("\n")
end

#valid?Boolean

Check if document is valid overall.

Returns:

  • (Boolean)

    true if all layers passed



176
177
178
# File 'lib/uniword/validation/document_validator.rb', line 176

def valid?
  @layer_results.values.all?(&:valid?)
end

#warningsArray<Hash>

Get all warnings from all layers.

Returns:

  • (Array<Hash>)

    All warnings with layer info



192
193
194
195
196
# File 'lib/uniword/validation/document_validator.rb', line 192

def warnings
  @layer_results.flat_map do |layer, result|
    result.warnings.map { |warn| warn.merge(layer: layer) }
  end
end