Class: ConsistencyFail::Reporters::Base

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

Direct Known Subclasses

HasOne, Polymorphic, ValidatesUniquenessOf

Constant Summary collapse

TERMINAL_WIDTH =
80
RED =
31
GREEN =
32

Instance Method Summary collapse

Instance Method Details

#column_1(model) ⇒ Object



46
47
48
# File 'lib/consistency_fail/reporters/base.rb', line 46

def column_1(model)
  model.name
end

#column_headersObject



50
51
52
# File 'lib/consistency_fail/reporters/base.rb', line 50

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

#divider(pad_to = TERMINAL_WIDTH) ⇒ Object



23
24
25
# File 'lib/consistency_fail/reporters/base.rb', line 23

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

#report(indexes_by_model) ⇒ Object



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
# File 'lib/consistency_fail/reporters/base.rb', line 54

def report(indexes_by_model)
  if indexes_by_model.empty?
    report_success(macro)
  else
    indexes_by_table_name = indexes_by_model.map do |model, indexes|
      [column_1(model), model, indexes]
    end.sort_by(&:first)
    longest_model_length = indexes_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)

    indexes_by_table_name.each do |table_name, model, indexes|
      indexes.each do |index|
        report_index(model, index, longest_model_length)
      end
    end
    divider(longest_model_length * 2)
  end
  puts
end

#report_failure_header(macro, longest_model_length) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/consistency_fail/reporters/base.rb', line 27

def report_failure_header(macro, longest_model_length)
  puts
  use_color(RED)
  puts "There are calls to #{macro} that aren't backed by unique indexes."
  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_index(model, index, column_1_length) ⇒ Object



41
42
43
44
# File 'lib/consistency_fail/reporters/base.rb', line 41

def report_index(model, index, column_1_length)
  print model.name.ljust(column_1_length + 2)
  puts "#{index.table_name} (#{index.columns.join(", ")})"
end

#report_success(macro) ⇒ Object



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

def report_success(macro)
  use_color(GREEN)
  puts "Hooray! All calls to #{macro} are correctly backed by a unique index."
  use_default_color
end

#use_color(code) ⇒ Object



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

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

#use_default_colorObject



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

def use_default_color
  use_color(0)
end