Class: CHANGELOG

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

Defined Under Namespace

Modules: ReverseHashMixin

Instance Method Summary collapse

Constructor Details

#initialize(path = "CHANGELOG") ⇒ CHANGELOG

Returns a new instance of CHANGELOG.

Parameters:

  • path (String) (defaults to: "CHANGELOG")

    Path to your CHANGELOG file

Raises:

  • (ArgumentError)

    if given path doesn’t exist

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



22
23
24
25
# File 'lib/changelog.rb', line 22

def initialize(path = "CHANGELOG")
  raise ArgumentError, "Path '#{path}' doesn't exist!" unless File.exist?(path)
  @path = path
end

Instance Method Details

#[](version) ⇒ Array<String>?

Returns List of changes in the given version or nil if given version doesn’t exist.

Examples:

changelog[“Version 0.1”]

Parameters:

  • version (String, Regexp)

    Full name of the version or pattern matching the version.

Returns:

  • (Array<String>, nil)

    List of changes in the given version or nil if given version doesn’t exist

Raises:

  • (StandardError)

    if pattern matched more than one version

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/changelog.rb', line 63

def [](version)
  if version.is_a?(String)
    self.parse[version]
  else
    versions = self.select(version).keys
    if versions.length > 1
      raise StandardError, "More than one version was matched: #{versions.inspect}"
    elsif versions.empty?
      return nil
    else
      self[versions.first]
    end
  end
end

#last_version_nameString?

Returns Name of the last available version or nil if the changelog is empty.

Returns:

  • (String, nil)

    Name of the last available version or nil if the changelog is empty

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



38
39
40
# File 'lib/changelog.rb', line 38

def last_version_name
  self.versions.last
end

#parseHash

Returns Hash of ‘=> changes` for the whole changelog.

Returns:

  • (Hash)

    Hash of ‘=> changes` for the whole changelog.

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



92
93
94
95
96
97
98
# File 'lib/changelog.rb', line 92

def parse
  @hash ||= begin
    hash = __parse__.last
    hash.extend(ReverseHashMixin)
    hash.reverse
  end
end

#select(pattern) ⇒ Hash

Returns Hash of ‘=> changes` for matched versions.

Examples:

changelog.select(/^Version 0.1.d+$/)

Parameters:

  • version (String, Regexp)

    Pattern we want to match in version.

Returns:

  • (Hash)

    Hash of ‘=> changes` for matched versions.

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



83
84
85
86
87
# File 'lib/changelog.rb', line 83

def select(pattern)
  self.parse.select do |version, changes|
    version.match(pattern)
  end
end

#version_changes(version = self.last_version_name) ⇒ Array<String>?

Returns List of changes in the last version or nil if the changelog is empty.

Examples:

changelog.version_changes
changelog.version_changes("Version 0.1")
changelog.version_changes(/0\.1/)

Returns:

  • (Array<String>, nil)

    List of changes in the last version or nil if the changelog is empty

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.3



50
51
52
53
54
# File 'lib/changelog.rb', line 50

def version_changes(version = self.last_version_name)
  self.parse[version].inject(String.new) do |buffer, line|
    buffer += "[\e[32m#{version}\e[0m] #{line}\n"
  end
end

#versionsArray<String>

Returns List of all the available versions.

Returns:

  • (Array<String>)

    List of all the available versions

Author:

  • Jakub Stastny aka Botanicus

Since:

  • 0.0.1



30
31
32
# File 'lib/changelog.rb', line 30

def versions
  self.parse.keys
end