Class: Chemicalml::Convention::Violation

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

Overview

Value object describing one constraint violation. Severity is either :error (document is invalid under the convention) or :warning (document is valid but problematic).

value carries the offending value where natural (e.g. the duplicated atom id, the malformed unit symbol). Optional — constraints that flag a missing attribute leave value nil.

Constant Summary collapse

SEVERITIES =
%i[error warning].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, message:, severity: :error, constraint: nil, value: nil) ⇒ Violation

Returns a new instance of Violation.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
# File 'lib/chemicalml/convention/violation.rb', line 17

def initialize(path:, message:, severity: :error, constraint: nil, value: nil)
  raise ArgumentError, "unknown severity: #{severity.inspect}" unless SEVERITIES.include?(severity.to_sym)

  @path       = path
  @message    = message
  @severity   = severity.to_sym
  @constraint = constraint
  @value      = value
  freeze
end

Instance Attribute Details

#constraintObject (readonly)

Returns the value of attribute constraint.



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

def constraint
  @constraint
end

#messageObject (readonly)

Returns the value of attribute message.



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

def message
  @message
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#severityObject (readonly)

Returns the value of attribute severity.



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

def severity
  @severity
end

#valueObject (readonly)

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/chemicalml/convention/violation.rb', line 28

def error?
  severity == :error
end

#to_sObject



36
37
38
39
# File 'lib/chemicalml/convention/violation.rb', line 36

def to_s
  suffix = value.nil? ? '' : " (value=#{value.inspect})"
  "#{severity.upcase} #{path}: #{message}#{suffix}"
end

#warning?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/chemicalml/convention/violation.rb', line 32

def warning?
  severity == :warning
end