Class: Nullalign::Reporters::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/nullalign/reporters/base.rb

Direct Known Subclasses

ValidatesPresenceOf

Constant Summary collapse

TERMINAL_WIDTH =
80
RED =
31
GREEN =
32

Instance Method Summary collapse

Instance Method Details

#column_1(model) ⇒ Object



45
46
47
# File 'lib/nullalign/reporters/base.rb', line 45

def column_1(model)
  model.name
end

#column_headersObject



49
50
51
# File 'lib/nullalign/reporters/base.rb', line 49

def column_headers
  ["Model", "Table Columns"]
end

#divider(pad_to = TERMINAL_WIDTH) ⇒ Object



25
26
27
# File 'lib/nullalign/reporters/base.rb', line 25

def divider(pad_to = TERMINAL_WIDTH)
  puts "-" * [pad_to, TERMINAL_WIDTH].max
end

#report(null_constraints_by_model) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nullalign/reporters/base.rb', line 53

def report(null_constraints_by_model)
  if null_constraints_by_model.empty?
    report_success(macro)
  else
    null_constraints_by_table_name = null_constraints_by_model.map do |model, columns|
      [column_1(model), model, columns]
    end.sort_by(&:first)
    longest_model_length = null_constraints_by_table_name.map(&:first).
                                                 sort_by(&:length).
                                                 last.
                                                 length
    column_1_header_length = column_headers.first.length
    longest_model_length = [longest_model_length, column_1_header_length].max

    report_failure_header(macro, longest_model_length)

    null_constraints_by_table_name.each do |table_name, model, columns|
      print model.name.ljust(longest_model_length + 2)
      puts columns.first.table_name + ": " + columns.map {|x| x.column }.join(', ')
    end
    divider(longest_model_length * 2)
  end
  puts
end

#report_failure_header(macro, longest_model_length) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nullalign/reporters/base.rb', line 29

def report_failure_header(macro, longest_model_length)
  puts
  use_color(RED)
  # TODO not using 'macro' but leaving it here in case this gets
  # rolled into consistency_fail
  puts "There are presence validators that aren't backed by non-null constraints."
  use_default_color
  divider(longest_model_length * 2)

  column_1_header, column_2_header = column_headers
  print column_1_header.ljust(longest_model_length + 2)
  puts column_2_header

  divider(longest_model_length * 2)
end

#report_success(macro) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/nullalign/reporters/base.rb', line 17

def report_success(macro)
  use_color(GREEN)
  # TODO not using 'macro' but leaving it here in case this gets
  # rolled into consistency_fail
  puts "Hooray! All presence validators are backed by a non-null constraint."
  use_default_color
end

#use_color(code) ⇒ Object



9
10
11
# File 'lib/nullalign/reporters/base.rb', line 9

def use_color(code)
  print "\e[#{code}m"
end

#use_default_colorObject



13
14
15
# File 'lib/nullalign/reporters/base.rb', line 13

def use_default_color
  use_color(0)
end