Sha256: 518c911973b1117c60e650d8e5fa07266ead756a7b23ab17f3706be5a5a3ccb2

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

require 'pathname'

module SchemaDev
  class Readme
    def self.update(config)
      new(config.matrix).update
    end

    attr_accessor :matrix, :readme

    def initialize(matrix)
      self.matrix = matrix
      self.readme = Pathname.new('README.md')
    end

    def update
      return false unless readme.exist?
      lines = readme.readlines
      newlines = sub_matrix(lines)
      if lines != newlines
        readme.write newlines.join
        return true
      end
    end

    def sub_matrix(lines)
      pattern = %r{^\s*<!-- SCHEMA_DEV: MATRIX}
      before = lines.take_while(&it !~ pattern)

      return lines if before == lines

      after = lines.reverse.take_while(&it !~ pattern).reverse

      contents = []
      contents << "<!-- SCHEMA_DEV: MATRIX - begin -->\n"
      contents << "<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->\n"
      self.matrix.group_by(&it.slice(:ruby, :rails)).each do |pair, items|
        contents << "* ruby **#{pair[:ruby]}** with rails **#{pair[:rails]}**, using #{items.map{|item| "**#{item[:db]}**"}.to_sentence(last_word_connector: ' or ')}\n"
      end
      contents << "\n"
      contents << "<!-- SCHEMA_DEV: MATRIX - end -->\n"

      before + contents + after
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schema_dev-2.0.4 lib/schema_dev/readme.rb
schema_dev-2.0.3 lib/schema_dev/readme.rb