class Changelog::MarkdownGenerator

Attributes

entries[R]
version[R]

Public Class Methods

new(version, entries, include_date: false) click to toggle source
# File lib/changelog/markdown_generator.rb, line 9
def initialize(version, entries, include_date: false)
    @version = version
    @entries = entries.select(&:valid?)
    @include_date = include_date
end

Public Instance Methods

to_s() click to toggle source
# File lib/changelog/markdown_generator.rb, line 15
def to_s
    markdown = StringIO.new
    markdown.puts header
    markdown.puts

    if entries.empty?
        markdown.puts "-   No changes.\n\n"
    else
        markdown.puts formatted_entries
    end

    markdown.string
end

Private Instance Methods

date() click to toggle source
# File lib/changelog/markdown_generator.rb, line 37
def date
    Date.today.strftime("%Y-%m-%d")
end
entries_grouped_by_type(type) click to toggle source
# File lib/changelog/markdown_generator.rb, line 41
def entries_grouped_by_type(type) 
    entries.select { |entry| entry.type == type }
end
formatted_entries() click to toggle source

Group entries by type found in the `Changelog::Entry::TYPES`. Output example:

### Fixed (52 changes)

  • Fix 404 errors in API caused when the branch name had a dot. #42 !14462 (ajoly)

# File lib/changelog/markdown_generator.rb, line 50
def formatted_entries
    result = +''

    Changelog::Entry::TYPES.map(&:name).each do |type|
        grouped_entries = entries_grouped_by_type(type)
        changes_count = grouped_entries.size

        # Do nothing if no changes are presented for the current type.
        next unless changes_count.positive?

        # Prepare the group header.
        # Example:
        # ### Added (54 changes)
        changes = [changes_count, 'change'.pluralize(changes_count)].join("\s")

        result << "### #{type.capitalize} (#{changes})\n\n"

        # Add entries to the group.
        grouped_entries.each { |entry| result << "-   #{entry}\n" }

        result << "\n"
    end
    
    result
end
header() click to toggle source
# File lib/changelog/markdown_generator.rb, line 31
def header
    head = "## #{version.to_patch}"
    head += " (#{date})" if @include_date
    head
end