Class: SchemaMigrationMonitor::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_migration_monitor/monitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(output_stream = $stdout) ⇒ Monitor

Returns a new instance of Monitor.



26
27
28
29
# File 'lib/schema_migration_monitor/monitor.rb', line 26

def initialize(output_stream = $stdout)
  @migration_path = MigrationPathService.execute
  @output_stream = output_stream
end

Instance Method Details

#executeObject



31
32
33
34
35
# File 'lib/schema_migration_monitor/monitor.rb', line 31

def execute
  @pending_migrations = get_pending_migrations
  return if @pending_migrations.empty?
  output_report
end

#get_longest_line(text_lines) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/schema_migration_monitor/monitor.rb', line 66

def get_longest_line(text_lines)
  text_lines.inject(0) do |memo, arg|
    line_length = arg.to_s.length
    memo = line_length if line_length > memo
    memo
  end
end

#get_pending_migrationsObject



74
75
76
# File 'lib/schema_migration_monitor/monitor.rb', line 74

def get_pending_migrations
  migrator.pending_migrations
end

#migratorObject



78
79
80
# File 'lib/schema_migration_monitor/monitor.rb', line 78

def migrator
  @migrator ||= ActiveRecord::Migrator.new(:up, @migration_path)
end

#output_reportObject



37
38
39
# File 'lib/schema_migration_monitor/monitor.rb', line 37

def output_report
  write_boxed_output(report_text.split("\n"))
end

#report_textObject



41
42
43
44
# File 'lib/schema_migration_monitor/monitor.rb', line 41

def report_text
  res = "The following migration(s) need to be run:"
  res << @pending_migrations.map { |migration| "\n  - #{migration.filename}" }.join
end

#wrap_output_in_redObject



60
61
62
63
64
# File 'lib/schema_migration_monitor/monitor.rb', line 60

def wrap_output_in_red
  @output_stream.write("\e[1;31m\n")
  yield if block_given?
  @output_stream.write("\n\e[0m") 
end

#write_boxed_output(text_lines) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/schema_migration_monitor/monitor.rb', line 46

def write_boxed_output(text_lines)
  longest_line = get_longest_line(text_lines)

  wrap_output_in_red do
    @output_stream.write("*" * (longest_line + 6) + "\n")

    text_lines.each do |line|
      @output_stream.write "** #{line}#{ " " * (longest_line - line.to_s.length) } **\n"
    end

    @output_stream.write("*" * (longest_line + 6) + "\n")
  end
end