Class: SchemaDev::Readme

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_dev/readme.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matrix) ⇒ Readme

Returns a new instance of Readme.



13
14
15
16
# File 'lib/schema_dev/readme.rb', line 13

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

Instance Attribute Details

#matrixObject

Returns the value of attribute matrix.



11
12
13
# File 'lib/schema_dev/readme.rb', line 11

def matrix
  @matrix
end

#readmeObject

Returns the value of attribute readme.



11
12
13
# File 'lib/schema_dev/readme.rb', line 11

def readme
  @readme
end

Class Method Details

.update(config) ⇒ Object



7
8
9
# File 'lib/schema_dev/readme.rb', line 7

def self.update(config)
  new(config.matrix(with_dbversion: true)).update
end

Instance Method Details

#replace_block(lines, pattern) {|contents| ... } ⇒ Object

Yields:

  • (contents)


67
68
69
70
71
72
73
74
75
# 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) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



48
49
50
51
52
53
# 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

#updateObject



18
19
20
21
22
23
24
25
26
27
28
29
# 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