Class: LiquidXlsx::Validation::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid_xlsx/validation/result.rb

Overview

Outcome of validating a template: what is wrong with it, and what it uses.

Validation never raises for a broken template — a broken template is the expected input here, and a caller wants the whole list, not the first failure. Only programming errors (a missing file handed to the validator, for instance) still raise.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(issues: [], usages: [], sheets: []) ⇒ Result

Returns a new instance of Result.



14
15
16
17
18
# File 'lib/liquid_xlsx/validation/result.rb', line 14

def initialize(issues: [], usages: [], sheets: [])
  @issues = issues
  @usages = usages
  @sheets = sheets
end

Instance Attribute Details

#issuesObject (readonly)

Returns the value of attribute issues.



12
13
14
# File 'lib/liquid_xlsx/validation/result.rb', line 12

def issues
  @issues
end

#sheetsObject (readonly)

Returns the value of attribute sheets.



12
13
14
# File 'lib/liquid_xlsx/validation/result.rb', line 12

def sheets
  @sheets
end

#usagesObject (readonly)

Returns the value of attribute usages.



12
13
14
# File 'lib/liquid_xlsx/validation/result.rb', line 12

def usages
  @usages
end

Instance Method Details

#add(code:, severity: :error, sheet: nil, row: nil, cell: nil, message: nil, source: nil) ⇒ Object



32
33
34
35
36
# File 'lib/liquid_xlsx/validation/result.rb', line 32

def add(code:, severity: :error, sheet: nil, row: nil, cell: nil, message: nil, source: nil)
  issues << Issue.new(code: code, severity: severity, sheet: sheet, row: row,
                      cell: cell, message: message, source: source)
  self
end

#add_usage(usage) ⇒ Object



38
39
40
41
# File 'lib/liquid_xlsx/validation/result.rb', line 38

def add_usage(usage)
  usages << usage
  self
end

#bound_namesObject

Names the template binds itself, in any sheet: loop variables, assigns, captures. Scope is deliberately ignored — a name bound anywhere counts as known everywhere. Tracking real scopes would let the validator report "unknown variable" for a name that is bound two rows above, and a false error costs more than a missed one here.



69
70
71
# File 'lib/liquid_xlsx/validation/result.rb', line 69

def bound_names
  @bound_names ||= usages.flat_map { |usage| usage.assigns + usage.locals }.uniq
end

#errorsObject



24
25
26
# File 'lib/liquid_xlsx/validation/result.rb', line 24

def errors
  issues.select(&:error?)
end

#filtersObject



60
61
62
# File 'lib/liquid_xlsx/validation/result.rb', line 60

def filters
  usages.flat_map(&:filters).uniq
end

#rootsObject

Root variable names the template expects to find in the data. Names bound by the template itself (for %, assign %) are not roots — they are excluded, otherwise every loop variable would look like a missing input.



54
55
56
57
58
# File 'lib/liquid_xlsx/validation/result.rb', line 54

def roots
  bound = bound_names

  variables.map(&:root).uniq.reject { |name| bound.include?(name) }
end

#to_hObject



73
74
75
76
77
# File 'lib/liquid_xlsx/validation/result.rb', line 73

def to_h
  { valid: valid?, issues: issues.map(&:to_h), sheets: sheets,
    roots: roots, filters: filters,
    variables: variables.map(&:path) }
end

#valid?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/liquid_xlsx/validation/result.rb', line 20

def valid?
  errors.empty?
end

#variablesObject

Every variable reference in the workbook, deduplicated by path. Root names alone are rarely enough for a caller: invoice.customer.name and invoice.customer.inn are different questions to ask of a model.



46
47
48
# File 'lib/liquid_xlsx/validation/result.rb', line 46

def variables
  usages.flat_map(&:variables).uniq(&:path)
end

#warningsObject



28
29
30
# File 'lib/liquid_xlsx/validation/result.rb', line 28

def warnings
  issues.select(&:warning?)
end