class ActiveRecord::MigrationNotes::Handler

Attributes

notes[RW]

Public Class Methods

new() click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 10
def initialize
  @notes = []
end

Public Instance Methods

add(version, name, content, direction = :up) click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 14
def add(version, name, content, direction = :up)
  content = '   ' + content.split("\n").map(&:strip).join("\n   ")
  notes << { version: version, name: name, content: content,
             direction: direction }
rescue
  nil
end
output() click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 22
def output
  migration_notes unless notes.empty?
  @notes = []
end

Private Instance Methods

colorize(str, color_code) click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 65
def colorize(str, color_code)
  return str unless terminal_supports_colors?
  "\e[#{color_code}m#{str}\e[0m"
end
migration_notes() click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 29
def migration_notes
  migration_notes_header

  notes.each do |note|
    say_message(note)
  end

  migration_notes_footer
end
migration_notes_header() click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 55
def migration_notes_header
  text = 'MIGRATION NOTES:'
  len = [0, 75 - text.length].max
  puts format('== %s %s', colorize(text, 36), '=' * len)
end
say_message(note) click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 39
def say_message(note)
  puts title(note)
  puts note[:content]
  puts '' unless notes.last == note
end
terminal_supports_colors?() click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 70
def terminal_supports_colors?
  cmd = 'which tput>/dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]]'
  system(cmd)
end
title(note) click to toggle source
# File lib/active_record/migration_notes/handler.rb, line 45
def title(note)
  title = case note[:direction]
          when :up    then '»» MIGRATED'
          when :down  then '«« REVERTED'
          end
  title += " - #{note[:name]}"
  len = [0, 77 - (title.length + note[:version].to_s.length)].max
  format('%s %s %s', colorize(title, 33), ' ' * len, note[:version])
end