Class: ArelConverter::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/arel_converter/formatter.rb

Constant Summary collapse

CLEAR =

Terminal colors, borrowed from Thor

"\e[0m"
BOLD =
"\e[1m"
RED =
"\e[31m"
YELLOW =
"\e[33m"
CYAN =
"\e[36m"
WHITE =
"\e[37m"

Class Method Summary collapse

Class Method Details

.alert(title, culprits, errors = nil) ⇒ Object

Show an upgrade alert to the user



13
14
15
16
17
18
19
# File 'lib/arel_converter/formatter.rb', line 13

def self.alert(title, culprits, errors=nil)
  if RbConfig::CONFIG['host_os'].downcase =~ /mswin|windows|mingw/
    Formatter.basic_alert(title, culprits)
  else
    Formatter.color_alert(title, culprits)
  end
end

.basic_alert(title, culprits) ⇒ Object

Show an upgrade alert to the user. If we’re on Windows, we can’t use terminal colors, hence this method.



23
24
25
26
27
28
29
30
# File 'lib/arel_converter/formatter.rb', line 23

def self.basic_alert(title, culprits)
  puts "** " + title
  Array(culprits).each do |c|
    puts c.valid? ? "  FROM: #{c.old_content}\n    TO: #{c.new_content}\n" :
                    "** ERROR - #{c.error}"
  end
  puts
end

.color_alert(file, culprits) ⇒ Object

Show a colorful alert to the user



33
34
35
36
37
38
39
40
41
42
# File 'lib/arel_converter/formatter.rb', line 33

def self.color_alert(file, culprits )
  puts "#{RED}#{BOLD}#{file}#{CLEAR}"
  Array(culprits).each do |c|
    puts c.valid? ? "#{YELLOW}  FROM: #{c.old_content}\n    TO: #{c.new_content}\n" :
                    "#{CYAN}#{BOLD}  - #{c.error}#{CLEAR}"

  end
ensure
  puts "#{CLEAR}"
end