Class: Chemicalml::Convention::ValidationReport

Inherits:
Object
  • Object
show all
Defined in:
lib/chemicalml/convention/validation_report.rb

Overview

Value object wrapping the violations returned by validate. Gives callers severity-based views without losing the raw array (still exposed as #violations).

Constructed by Convention.validate_report (new API) — Convention.validate keeps its array return shape so existing callers don't break.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(violations) ⇒ ValidationReport

Returns a new instance of ValidationReport.



15
16
17
# File 'lib/chemicalml/convention/validation_report.rb', line 15

def initialize(violations)
  @violations = violations.to_a.freeze
end

Instance Attribute Details

#violationsObject (readonly)

Returns the value of attribute violations.



13
14
15
# File 'lib/chemicalml/convention/validation_report.rb', line 13

def violations
  @violations
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
44
45
# File 'lib/chemicalml/convention/validation_report.rb', line 39

def +(other)
  return other if other.nil?

  raise ArgumentError, "cannot combine ValidationReport with #{other.class}" unless other.is_a?(ValidationReport)

  ValidationReport.new(violations + other.violations)
end

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/chemicalml/convention/validation_report.rb', line 47

def ==(other)
  other.is_a?(ValidationReport) && violations == other.violations
end

#errorsObject



19
20
21
# File 'lib/chemicalml/convention/validation_report.rb', line 19

def errors
  @errors ||= violations.select(&:error?).freeze
end

#has_warnings?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/chemicalml/convention/validation_report.rb', line 31

def has_warnings?
  warnings.any?
end

#hashObject



52
53
54
# File 'lib/chemicalml/convention/validation_report.rb', line 52

def hash
  violations.hash
end

#ok?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/chemicalml/convention/validation_report.rb', line 27

def ok?
  errors.empty?
end

#sizeObject



35
36
37
# File 'lib/chemicalml/convention/validation_report.rb', line 35

def size
  violations.length
end

#summaryString

Human-readable multi-line summary. Suitable for CLI output and logging. Each violation is rendered with severity, path, and message (and value when present).

Returns:

  • (String)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chemicalml/convention/validation_report.rb', line 65

def summary
  return 'OK — no violations' if ok? && !has_warnings?

  lines = []
  lines << "Errors: #{errors.size}, Warnings: #{warnings.size}"
  unless errors.empty?
    lines << ''
    lines << 'Errors:'
    errors.each { |v| lines << "  ERROR #{v.path}: #{v.message}#{value_suffix(v)}" }
  end
  unless warnings.empty?
    lines << ''
    lines << 'Warnings:'
    warnings.each { |v| lines << "  WARN  #{v.path}: #{v.message}#{value_suffix(v)}" }
  end
  lines.join("\n")
end

#to_sObject



56
57
58
# File 'lib/chemicalml/convention/validation_report.rb', line 56

def to_s
  "#{size} violation(s): #{errors.length} error(s), #{warnings.length} warning(s)"
end

#warningsObject



23
24
25
# File 'lib/chemicalml/convention/validation_report.rb', line 23

def warnings
  @warnings ||= violations.select(&:warning?).freeze
end