class KeepAChangelogManager::Changelog::ChangeData

Data Structure of a Changelog document:

Attributes

header[RW]

@return Array<String>

releases[RW]

@return Hash

Public Class Methods

bare() click to toggle source

content of a fresh (empty) changelog

@return String

# File lib/keepachangelog_manager/changelog.rb, line 62
def self.bare
  header_lines = [
    "All notable changes to this project will be documented in this file.",
    "",
    "The format is based on [Keep a Changelog](http://keepachangelog.com/)",
    "and this project adheres to [Semantic Versioning](http://semver.org/).",
  ]
  ChangeData.new(header_lines, UNRELEASED => self.bare_unreleased_data)
end
bare_unreleased_data() click to toggle source

Default “unreleased” structure

@return Hash

# File lib/keepachangelog_manager/changelog.rb, line 53
def self.bare_unreleased_data
  {
    sections: Hash[SECTION_ORDER.map { |i| [i, []] }]
  }
end
new(header, releases) click to toggle source
# File lib/keepachangelog_manager/changelog.rb, line 35
def initialize(header, releases)
  @header = header
  @releases = releases
end

Public Instance Methods

update(inc_patch: nil, abs_patch: nil, inc_minor: nil, abs_minor: nil, inc_major: nil, abs_major: nil) click to toggle source

Update a changelog by transforming Unreleased into a release

Only one argument at a time should be supplied, all others nil

@param inc_patch boolean whether to increment the patch version @param abs_patch int an absolute value for the patch version @param inc_minor boolean whether to increment the minor version @param abs_minor int an absolute value for the minor version @param inc_major boolean whether to increment the major version @param abs_major int an absolute value for the major version @return String the new version

# File lib/keepachangelog_manager/changelog.rb, line 83
def update(inc_patch: nil, abs_patch: nil,
           inc_minor: nil, abs_minor: nil,
           inc_major: nil, abs_major: nil)
  num_args_supplied = binding.local_variables.count { |p| !binding.local_variable_get(p).nil? }
  raise ArgumentError, "Only one update option should be specified" unless 1 == num_args_supplied

  version = @releases.keys.reject { |k| k == UNRELEASED }.map { |k| SemVer.parse(k) }.max
  version = SemVer.parse("0.0.0") if version.nil?

  if !inc_patch.nil?
    version.patch += 1
  elsif !abs_patch.nil?
    validate(version, :patch, abs_patch)
    version.patch = abs_patch
  elsif !inc_minor.nil?
    version.minor += 1
    version.patch = 0
  elsif !abs_minor.nil?
    validate(version, :minor, abs_minor)
    version.minor = abs_minor
    version.patch = 0
  elsif !inc_major.nil?
    version.major += 1
    version.minor = 0
    version.patch = 0
  elsif !abs_major.nil?
    validate(version, :major, abs_major)
    version.major = abs_major
    version.minor = 0
    version.patch = 0
  end

  new_version = version.format("%M.%m.%p")
  @releases[new_version] = @releases[UNRELEASED]
  @releases[new_version][:date] = Date.today.strftime("%Y-%m-%d")
  @releases[UNRELEASED] = self.class.bare_unreleased_data

  new_version
end
validate(version, dimension, newval) click to toggle source

validate an attempted change to a version – make sure no versions decrease

@param version SemVer @param dimension Symbol @param newval Int

# File lib/keepachangelog_manager/changelog.rb, line 45
def validate(version, dimension, newval)
  oldval = version.send(dimension)
  raise ArgumentError, "Tried to set #{dimension} to #{newval}, which isn't greater than #{oldval}" unless newval > oldval
end