Class: Bundler::Whatsup::ChangelogParser

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/whatsup/changelog_parser.rb

Overview

Parses content of changelog file by versions

Constant Summary collapse

VERSION_REGEXP =
/
  ((.+)?[#]+(.+)?)                          # requires that line to be a title
  (?<version>(\d{1,3}\.\d{1,3}\.\d{1,3})    # requires three digits splitted by dots
  (\.pre((.|-)?\d{1,3})?|                   # version can contains a modificators, like: pre, rc, alpha, beta
  .rc((.|-)?\d{1,3})?|
  .alpha((.|-)?\d{1,3})?|
  .beta((.|-)?\d{1,3})?)?)
/x

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ ChangelogParser

Initializer

Parameters:

  • content (String)

20
21
22
23
# File 'lib/bundler/whatsup/changelog_parser.rb', line 20

def initialize(content)
  @content = content
  @parsed = Hash.new {|h, k| h[k] = []}
end

Instance Attribute Details

#parsedObject (readonly)

Returns the value of attribute parsed


6
7
8
# File 'lib/bundler/whatsup/changelog_parser.rb', line 6

def parsed
  @parsed
end

Instance Method Details

#runNilClass|Hash

parse changelog content return nil if @content empty

Returns:

  • (NilClass|Hash)

29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/bundler/whatsup/changelog_parser.rb', line 29

def run
  return if @content.empty?

  # remove empty line and split to array by newline symbol
  lines = @content.split("\n").reject(&:empty?)
  current_version = nil

  lines.map do |line|

    # check if line matches regexp and if line has valid version
    if (m = line.match(VERSION_REGEXP))
      current_version = m[:version]
      next
    end

    parsed[current_version] << line unless current_version.nil?
  end

  parsed
end