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.



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

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

Instance Attribute Details

#matrixObject

Returns the value of attribute matrix.



9
10
11
# File 'lib/schema_dev/readme.rb', line 9

def matrix
  @matrix
end

#readmeObject

Returns the value of attribute readme.



9
10
11
# File 'lib/schema_dev/readme.rb', line 9

def readme
  @readme
end

Class Method Details

.update(config) ⇒ Object



5
6
7
# File 'lib/schema_dev/readme.rb', line 5

def self.update(config)
  new(config.matrix).update
end

Instance Method Details

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

Yields:

  • (contents)


59
60
61
62
63
64
65
66
# File 'lib/schema_dev/readme.rb', line 59

def replace_block(lines, pattern)
  before = lines.take_while(&it !~ pattern)
  return lines if before == lines
  after = lines.reverse.take_while(&it !~ pattern).reverse
  contents = []
  yield contents
  before + contents + after
end

#sub_matrix(lines) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/schema_dev/readme.rb', line 28

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"
    self.matrix.group_by(&it.slice(:ruby, :activerecord)).each do |pair, items|
      contents << "* ruby **#{pair[:ruby]}** with activerecord **#{pair[:activerecord]}**, using #{items.map{|item| "**#{item[:db]}**"}.to_sentence(last_word_connector: ' or ')}\n"
    end
    contents << "\n"
    contents << "<!-- SCHEMA_DEV: MATRIX - end -->\n"
  end
end

#sub_template(template, lines) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/schema_dev/readme.rb', line 47

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



40
41
42
43
44
45
# File 'lib/schema_dev/readme.rb', line 40

def sub_templates(lines)
  Pathname.glob(SchemaDev::Templates.root + "README" + "*.md.erb").each do |template|
    lines = sub_template(template, lines)
  end
  lines
end

#updateObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/schema_dev/readme.rb', line 16

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
    return true
  end
end