Method: TableWarnings#table_warnings

Defined in:
lib/table_warnings.rb

#table_warningsObject

Get current warning messages on the table.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/table_warnings.rb', line 30

def table_warnings
  messages = []

  TableWarnings.registry.nonexclusive(self).each do |warning|
    messages << warning.messages
  end

  pool = column_names.map do |column_name|
    TableWarnings::Column.new self, column_name
  end
  exclusive_warnings = TableWarnings.registry.exclusive(self)

  assignments = {}
  # pass 1 - exclusives and covers
  exclusive_warnings.each do |warning|
    disposition = Disposition.new
    disposition.exclusives = warning.exclusives pool
    disposition.covers = warning.covers pool
    assignments[warning] = disposition
    pool -= disposition.exclusives
  end
  if ENV['TABLE_WARNINGS_DEBUG'] == 'true'
    $stderr.puts "pass 1"
    assignments.each do |warning, disposition|
      $stderr.puts "  #{warning.scout.matcher} - exclusives=#{disposition.exclusives.map(&:name)} covers=#{disposition.covers.map(&:name)}"
    end
  end
  # pass 2 - allow regexp matching, but only if somebody else didn't cover it
  exclusive_warnings.each do |warning|
    disposition = assignments[warning]
    disposition.matches = warning.matches(pool).select do |match|
      assignments.except(warning).none? { |_, other| other.covers.include?(match) }
    end
    pool -= disposition.matches
  end
  if ENV['TABLE_WARNINGS_DEBUG'] == 'true'
    $stderr.puts "pass 2"
    assignments.each do |warning, disposition|
      $stderr.puts "  #{warning.scout.matcher} - exclusives=#{disposition.exclusives.map(&:name)} covers=#{disposition.covers.map(&:name)} matches=#{disposition.matches.map(&:name)}"
    end
  end
  if ENV['TABLE_WARNINGS_STRICT'] == 'true'
    $stderr.puts "uncovered columns"
    $stderr.puts pool.join("\n")
  end

  # now you can generate messages
  assignments.each do |warning, disposition|
    messages << warning.messages(disposition.exclusives+disposition.matches)
  end

  messages.flatten.compact
end