class SchemaDev::Readme

Attributes

matrix[RW]
readme[RW]

Public Class Methods

new(matrix) click to toggle source
# File lib/schema_dev/readme.rb, line 13
def initialize(matrix)
  self.matrix = matrix
  self.readme = Pathname.new('README.md')
end
update(config) click to toggle source
# File lib/schema_dev/readme.rb, line 7
def self.update(config)
  new(config.matrix(with_dbversion: true)).update
end

Public Instance Methods

replace_block(lines, pattern) { |contents| ... } click to toggle source
# File lib/schema_dev/readme.rb, line 67
def replace_block(lines, pattern)
  before = lines.take_while { |e| e !~ pattern }
  return lines if before == lines

  after = lines.reverse.take_while { |e| e !~ pattern }.reverse
  contents = []
  yield contents
  before + contents + after
end
sub_matrix(lines) click to toggle source
# File lib/schema_dev/readme.rb, line 31
def sub_matrix(lines)
  replace_block(lines, %r{^\s*<!-- SCHEMA_DEV: MATRIX}) do |contents|
    contents << "<!-- SCHEMA_DEV: MATRIX - begin -->\n"
    contents << "<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->\n"
    matrix.group_by { |e| e.slice(:ruby, :activerecord) }.each do |pair, items|
      dbs = items.map do |item|
        db = item[:db]
        db = "#{db}:#{item[:dbversion]}" if item.key?(:dbversion)
        "**#{db}**"
      end.to_sentence(last_word_connector: ' or ')
      contents << "* ruby **#{pair[:ruby]}** with activerecord **#{pair[:activerecord]}**, using #{dbs}\n"
    end
    contents << "\n"
    contents << "<!-- SCHEMA_DEV: MATRIX - end -->\n"
  end
end
sub_template(template, lines) click to toggle source
# File lib/schema_dev/readme.rb, line 55
def sub_template(template, lines)
  key = template.basename('.md.erb').to_s.upcase.tr('.', ' ')

  replace_block(lines, %r{^\s*<!-- SCHEMA_DEV: TEMPLATE #{key}}) do |contents|
    contents << "<!-- SCHEMA_DEV: TEMPLATE #{key} - begin -->\n"
    contents << "<!-- These lines are auto-inserted from a schema_dev template -->\n"
    contents << template.readlines
    contents << "\n"
    contents << "<!-- SCHEMA_DEV: TEMPLATE #{key} - end -->\n"
  end
end
sub_templates(lines) click to toggle source
# File lib/schema_dev/readme.rb, line 48
def sub_templates(lines)
  Pathname.glob(SchemaDev::Templates.root + 'README' + '*.md.erb').each do |template|
    lines = sub_template(template, lines)
  end
  lines
end
update() click to toggle source
# File lib/schema_dev/readme.rb, line 18
def update
  return false unless readme.exist?

  lines = readme.readlines
  newlines = sub_matrix(lines.dup)
  newlines = sub_templates(newlines)
  newreadme = Gem.new(Pathname.pwd.basename.to_s).erb(newlines.join)
  if newreadme != lines.join
    readme.write newreadme
    true
  end
end