Module: Railbow::MigrationFormatter

Defined in:
lib/railbow/migration_formatter.rb

Constant Summary collapse

FORMATTER =
Formatters::Base.new.freeze

Instance Method Summary collapse

Instance Method Details

#announce(message) ⇒ Object



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
# File 'lib/railbow/migration_formatter.rb', line 10

def announce(message)
  return super if Railbow.plain?

  f = FORMATTER
  migration_name = self.class.name&.demodulize || "Migration"

  line = case message
  when /migrating/i
    f.cyan("#{f.emoji(:migrating)} #{migration_name}: migrating...")
  when /migrated\s*\((\d+\.\d+)s\)/i
    raw_seconds = Regexp.last_match(1).to_f
    formatted = f.format_timing(raw_seconds)
    f.green("#{f.emoji(:migrated)} #{migration_name}: migrated") + " (#{formatted} total)"
  when /reverting/i
    f.yellow("#{f.emoji(:reverting)} #{migration_name}: reverting...")
  when /reverted\s*\((\d+\.\d+)s\)/i
    raw_seconds = Regexp.last_match(1).to_f
    formatted = f.format_timing(raw_seconds)
    f.green("#{f.emoji(:reverted)} #{migration_name}: reverted") + " (#{formatted} total)"
  else
    "#{migration_name}: #{message}"
  end

  write ""
  write line
end

#say(message, subitem = false) ⇒ Object



51
52
53
54
55
56
# File 'lib/railbow/migration_formatter.rb', line 51

def say(message, subitem = false)
  return super if Railbow.plain?

  prefix = subitem ? "     " : "  "
  write("#{prefix}#{message}")
end

#say_with_time(message) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/railbow/migration_formatter.rb', line 37

def say_with_time(message)
  return super { yield } if Railbow.plain?

  f = FORMATTER
  result = nil
  time = Benchmark.measure { result = yield }

  timing = f.format_timing(time.real)
  write "  #{f.green(f.emoji(:check))} #{message}#{timing}"
  say("#{result} rows", :subitem) if result.is_a?(Integer)

  result
end

#write(text = "") ⇒ Object



58
59
60
61
62
# File 'lib/railbow/migration_formatter.rb', line 58

def write(text = "")
  return super if Railbow.plain?

  puts text
end