Class: SheepAChangelog::Document

Inherits:
Node
  • Object
show all
Defined in:
lib/sheep-a-changelog/document.rb

Instance Attribute Summary

Attributes inherited from Node

#anchors, #lines, #nodes, #title

Instance Method Summary collapse

Methods inherited from Node

#all_lines, #all_lines_wo_heading, #build_nodes, #build_tree, #format_heading, #h_regexp, #hashes, parse, pick_lines

Constructor Details

#initialize(lines) ⇒ Document

Returns a new instance of Document.



5
6
7
# File 'lib/sheep-a-changelog/document.rb', line 5

def initialize(lines)
  super(lines, :empty, 0)
end

Instance Method Details

#add_anchor(name, from, to) ⇒ Object



49
50
51
# File 'lib/sheep-a-changelog/document.rb', line 49

def add_anchor(name, from, to)
  anchors.unshift(v: name, url: "#{diff_prefix}#{from}...#{to}")
end

#add_empty_version(name) ⇒ Object



24
25
26
# File 'lib/sheep-a-changelog/document.rb', line 24

def add_empty_version(name)
  version_root.nodes.unshift(Node.new([''], name, 2))
end

#diff_prefixObject



28
29
30
31
32
33
34
# File 'lib/sheep-a-changelog/document.rb', line 28

def diff_prefix
  anchors.map { |a| a[:url].match(%r{^(.*\/)(.*\.\.\..*)$}).to_a[1] }
         .each_with_object(Hash.new(0)) { |word, counts| counts[word] += 1 }
         .to_a
         .min { |a, b| b[1] <=> a[1] }
         .first
end

#latest_version(n = 0) ⇒ Object

Return the latest version



20
21
22
# File 'lib/sheep-a-changelog/document.rb', line 20

def latest_version(n = 0)
  latest_version_title(n).match(/^\[(\S+)\].*/).to_a[1]
end

#latest_version_title(n = 0) ⇒ Object

Return title of the latest version node



15
16
17
# File 'lib/sheep-a-changelog/document.rb', line 15

def latest_version_title(n = 0)
  version_root.nodes[n].title
end

#release(version, tag_prefix, t = Time.now) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/sheep-a-changelog/document.rb', line 61

def release(version, tag_prefix, t = Time.now)
  remove_anchor_unreleased
  rename_version('[Unreleased]', "[#{version}] - #{t.strftime('%Y-%m-%d')}")
  add_anchor(version, tag_prefix + latest_version(1), tag_prefix + version)
  add_empty_version('[Unreleased]')
  add_anchor('Unreleased', tag_prefix + version, 'HEAD')
end

#remove_anchor_unreleasedObject



53
54
55
# File 'lib/sheep-a-changelog/document.rb', line 53

def remove_anchor_unreleased
  anchors.reject! { |a| a[:v] == 'Unreleased' }
end

#rename_version(from, to) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sheep-a-changelog/document.rb', line 36

def rename_version(from, to)
  parent = version_root
  parent.nodes.map! do |node|
    if node.title == from
      cloned = node.clone
      cloned.title = to
      cloned
    else
      node
    end
  end
end

#to_sObject



57
58
59
# File 'lib/sheep-a-changelog/document.rb', line 57

def to_s
  (all_lines + ['']).join("\n")
end

#version_rootObject

Return parent node to version nodes



10
11
12
# File 'lib/sheep-a-changelog/document.rb', line 10

def version_root
  nodes.first
end