class Doing::Changes

A collection of Changes

Attributes

changes[R]
changes_only[W]

Public Class Methods

new(lookup: nil, search: nil, changes: false, sort: :desc, prefix: false, only: %i[changed new improved fixed]) click to toggle source
# File lib/doing/changelog/changes.rb, line 9
def initialize(lookup: nil, search: nil, changes: false, sort: :desc, prefix: false, only: %i[changed new improved fixed])
  @changes_only = changes
  @prefix = prefix
  @only = only
  changelog = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'CHANGELOG.md'))
  raise 'Error locating changelog' unless File.exist?(changelog)

  @content = IO.read(changelog)
  parse_changes(lookup, search)

  @changes.reverse! if sort == :asc
end

Public Instance Methods

interactive() click to toggle source
# File lib/doing/changelog/changes.rb, line 34
def interactive
  Doing::Prompt.choose_from(versions,
                            prompt: 'Select a version to see its changelog',
                            sorted: false,
                            fzf_args: [
                              %(--preview='doing changes --render -l {1}'),
                              '--disabled',
                              '--height=50',
                              '--preview-window="right,70%"'
                            ])
end
latest() click to toggle source
# File lib/doing/changelog/changes.rb, line 22
def latest
  if @changes_only
    @changes[0].changes_only.force_encoding('utf-8')
  else
    @changes[0].to_s.force_encoding('utf-8')
  end
end
to_s() click to toggle source
# File lib/doing/changelog/changes.rb, line 46
def to_s
  if @changes_only
    @changes.map(&:changes_only).delete_if(&:empty?).join().gsub(/\n+/, "\n").force_encoding('utf-8')
  else
    @changes.map(&:to_s).join("\n\n").force_encoding('utf-8')
  end
end
versions() click to toggle source
# File lib/doing/changelog/changes.rb, line 30
def versions
  @changes.select { |change| change.entries&.count > 0 }.map { |change| change.version }
end

Private Instance Methods

lookup(lookup_version) click to toggle source
# File lib/doing/changelog/changes.rb, line 69
def lookup(lookup_version)
  range = []

  if lookup_version =~ /([\d.]+) *(?:-|to)+ *([\d.]+)/
    m = Regexp.last_match
    lookup("> #{m[1]}")
    lookup("< #{m[2]}")
  elsif lookup_version.scan(/(?:<=?|prior|before|older|>=?|since|after|newer) *[0-9*?.]+/).count > 1
    params = lookup_version.scan(/(?:<=?|prior|before|older|>=?|since|after|newer) *[0-9*?.]+/)
    params.each { |query| lookup(query) }
  else
    inclusive = lookup_version =~ /=/ ? true : false
    comp = case lookup_version
           when /(<|prior|before|older)/
             :older
           when /(>|since|after|newer)/
             :newer
           else
             :equal
           end
    version = Version.new(lookup_version)

    @changes.select! do |change|
      change.version.compare(version, comp, inclusive: inclusive)
    end
  end
end
parse_changes(lookup, search) click to toggle source
# File lib/doing/changelog/changes.rb, line 56
def parse_changes(lookup, search)
  change_rx = /(?<=\n|\A)### (\d+\.\d+\.\d+(?:\w*))(.*?)(?=\n### |\Z)/m
  @changes = @content.scan(change_rx).each_with_object([]) do |m, a|
    next if m[0].nil? || m[1].nil?

    a << Change.new(m[0], m[1].strip, prefix: @prefix)
  end

  select_type
  lookup(lookup) unless lookup.nil?
  search(search) unless search.nil?
end
select_type() click to toggle source
# File lib/doing/changelog/changes.rb, line 105
def select_type
  @changes.map do |c|
    c.entries.delete_if { |e| !@only.include?(e.type.normalize_change_type) }
  end
end