Module: TableWarnings

Defined in:
lib/table_warnings.rb,
lib/table_warnings/null.rb,
lib/table_warnings/size.rb,
lib/table_warnings/blank.rb,
lib/table_warnings/range.rb,
lib/table_warnings/scout.rb,
lib/table_warnings/column.rb,
lib/table_warnings/version.rb,
lib/table_warnings/registry.rb,
lib/table_warnings/arbitrary.rb,
lib/table_warnings/exclusive.rb,
lib/table_warnings/nonexistent_owner.rb

Defined Under Namespace

Classes: Arbitrary, Blank, Column, Disposition, Exclusive, NonexistentOwner, Null, Range, Registry, Scout, Size

Constant Summary collapse

VERSION =
"1.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registryObject



20
21
22
23
24
# File 'lib/table_warnings.rb', line 20

def TableWarnings.registry
  @registry || Thread.exclusive do
    @registry ||= Registry.new
  end
end

Instance Method Details

#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

#warn_if(&blk) ⇒ Object

An arbitrary warning.



128
129
130
# File 'lib/table_warnings.rb', line 128

def warn_if(&blk)
  TableWarnings.registry.add_warning self, TableWarnings::Arbitrary.new(self, blk)
end

#warn_if_any_blanksObject

Warn if there are blanks in ANY column.



110
111
112
# File 'lib/table_warnings.rb', line 110

def warn_if_any_blanks
  TableWarnings.registry.add_warning self, TableWarnings::Blank.new(self, /.*/)
end

#warn_if_any_nullsObject

Warn if there are nulls in ANY column.



115
116
117
# File 'lib/table_warnings.rb', line 115

def warn_if_any_nulls
  TableWarnings.registry.add_warning self, TableWarnings::Null.new(self, /.*/)
end

#warn_if_blanks(*args) ⇒ Object

Warn if there are blanks in a certain column.

Blank includes both NULL and “” (empty string)



94
95
96
97
98
99
# File 'lib/table_warnings.rb', line 94

def warn_if_blanks(*args)
  options = args.extract_options!
  args.flatten.each do |matcher|
    TableWarnings.registry.add_warning self, TableWarnings::Blank.new(self, matcher, options)
  end
end

#warn_if_blanks_except(*args) ⇒ Object



139
140
141
142
143
144
# File 'lib/table_warnings.rb', line 139

def warn_if_blanks_except(*args)
  options = args.extract_options!
  args.flatten.each do |matcher|
    TableWarnings.registry.add_warning self, TableWarnings::Blank.new(self, matcher, options.merge(:negative => true))
  end
end

#warn_if_nonexistent_owner(*args) ⇒ Object



146
147
148
149
150
151
# File 'lib/table_warnings.rb', line 146

def warn_if_nonexistent_owner(*args)
  options = args.extract_options!
  args.flatten.each do |belongs_to_association_name|
    TableWarnings.registry.add_warning self, TableWarnings::NonexistentOwner.new(self, belongs_to_association_name, options)
  end
end

#warn_if_nonexistent_owner_except(*args) ⇒ Object



153
154
155
156
157
158
# File 'lib/table_warnings.rb', line 153

def warn_if_nonexistent_owner_except(*args)
  options = args.extract_options!
  args.flatten.each do |belongs_to_association_name|
    TableWarnings.registry.add_warning self, TableWarnings::NonexistentOwner.new(self, belongs_to_association_name, options.merge(:negative => true))
  end
end

#warn_if_nulls(*args) ⇒ Object

Warn if there are NULLs in a certain column.



102
103
104
105
106
107
# File 'lib/table_warnings.rb', line 102

def warn_if_nulls(*args)
  options = args.extract_options!
  args.flatten.each do |matcher|
    TableWarnings.registry.add_warning self, TableWarnings::Null.new(self, matcher, options)
  end
end

#warn_if_nulls_except(*args) ⇒ Object



132
133
134
135
136
137
# File 'lib/table_warnings.rb', line 132

def warn_if_nulls_except(*args)
  options = args.extract_options!
  args.flatten.each do |matcher|
    TableWarnings.registry.add_warning self, TableWarnings::Null.new(self, matcher, options.merge(:negative => true))
  end
end

#warn_unless_range(*args) ⇒ Object



84
85
86
87
88
89
# File 'lib/table_warnings.rb', line 84

def warn_unless_range(*args)
  options = args.extract_options!
  args.flatten.each do |matcher|
    TableWarnings.registry.add_warning self, TableWarnings::Range.new(self, matcher, options)
  end
end

#warn_unless_size(approximate_size, options = {}) ⇒ Object

Warn if the number of records falls out of an (approximate) size.

Approximations: :few, :tens, :dozens, :hundreds, :thousands, :hundreds_of_thousands, :millions Exact: pass a Range or a Numeric



123
124
125
# File 'lib/table_warnings.rb', line 123

def warn_unless_size(approximate_size, options = {})
  TableWarnings.registry.add_warning self, TableWarnings::Size.new(self, approximate_size, options)
end