/
converter_base.rb
70 lines (64 loc) · 1.89 KB
/
converter_base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
module Rails
module Converter
class Base
def contruct_for_arel(options)
options.map do |key,value|
case key
when :conditions, :where
key = 'where'
when :include
key = 'includes'
end
case
when value.is_a?(Array)
value = value.inspect
when value.is_a?(Hash)
value = value.inspect
when value.is_a?(Symbol)
value = ":#{value}"
when value.is_a?(String) && key != 'where'
value = %Q{"#{value}"}
end
"#{key}(#{value})"
end
end
# Terminal colors, borrowed from Thor
CLEAR = "\e[0m"
BOLD = "\e[1m"
RED = "\e[31m"
YELLOW = "\e[33m"
CYAN = "\e[36m"
WHITE = "\e[37m"
# Show an upgrade alert to the user
def alert(title, culprits, errors=nil)
if Config::CONFIG['host_os'].downcase =~ /mswin|windows|mingw/
basic_alert(title, culprits, errors)
else
color_alert(title, culprits, errors)
end
end
# Show an upgrade alert to the user. If we're on Windows, we can't
# use terminal colors, hence this method.
def basic_alert(title, culprits, errors=nil)
puts "** " + title
puts "\t** " + error if error
Array(culprits).each do |c|
puts "\t- #{c}"
end
puts
end
# Show a colorful alert to the user
def color_alert(file, culprits, errors=nil)
puts "#{RED}#{BOLD}#{file}#{CLEAR}"
Array(errors).each do |error|
puts "#{CYAN}#{BOLD}\t- #{error}#{CLEAR}"
end
Array(culprits).each do |c|
puts c.is_a?(Array) ? "#{YELLOW}\tFROM: #{c[0]}\n\t TO: #{c[1]}" : "#{YELLOW}\t#{c[0]}"
end
ensure
puts "#{CLEAR}"
end
end
end
end