Class: Doing::Change

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

Overview

A single version's entries

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, content, prefix: false, only: %i[changed new improved fixed]) ⇒ Change

Returns a new instance of Change.



12
13
14
15
16
17
18
# File 'lib/doing/changelog/change.rb', line 12

def initialize(version, content, prefix: false, only: %i[changed new improved fixed])
  @version = Version.new(version)
  @content = content
  @prefix = prefix
  @only = only
  parse_entries
end

Instance Attribute Details

#change_dateObject

Returns the value of attribute change_date.



8
9
10
# File 'lib/doing/changelog/change.rb', line 8

def change_date
  @change_date
end

#contentObject (readonly)

Returns the value of attribute content.



6
7
8
# File 'lib/doing/changelog/change.rb', line 6

def content
  @content
end

#entriesObject

Returns the value of attribute entries.



8
9
10
# File 'lib/doing/changelog/change.rb', line 8

def entries
  @entries
end

#prefix=(value) ⇒ Object (writeonly)

Sets the attribute prefix

Parameters:

  • value

    the value to set the attribute prefix to.



10
11
12
# File 'lib/doing/changelog/change.rb', line 10

def prefix=(value)
  @prefix = value
end

#versionObject (readonly)

Returns the value of attribute version.



6
7
8
# File 'lib/doing/changelog/change.rb', line 6

def version
  @version
end

Instance Method Details

#changes_onlyObject



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/doing/changelog/change.rb', line 94

def changes_only
  out = []

  split_items.each do |type, members|
    next unless @only.include?(type)

    out << members.map(&:to_s).join("\n")
  end

  out.join("\n")
end

#parse_entriesObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/doing/changelog/change.rb', line 20

def parse_entries
  date = @content.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/)
  @change_date = Time.parse(date[0]) if date

  @entries = []
  types = @content.scan(/(?<=\n|\A)#### (CHANGED|NEW|IMPROVED|FIXED)(.*?)(?=\n####|\Z)/m)
  types.each do |type|
    type[1].scan(/\s*- +(.*?)$/).each do |entry|
      @entries << Entry.new(entry[0].strip, type[0], prefix: @prefix)
    end
  end
end

#search_entries(search_string) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/doing/changelog/change.rb', line 33

def search_entries(search_string)
  case_type = :ignore

  matches = []

  if search_string.rx?
    matches = @entries.select { |e| e.string =~ search_string.to_rx(distance: 2, case_type: case_type) }
  else
    query = search_string.gsub(/(-)?--/, '\1]]').to_phrase_query

    if query[:must].nil? && query[:must_not].nil?
      query[:must] = query[:should]
      query[:should] = []
    end

    @entries.each do |entry|
      m = no_searches?(entry.string, query[:must_not])
      m &&= all_searches?(entry.string, query[:must])
      m &&= any_searches?(entry.string, query[:should])
      matches << entry if m
    end
  end

  @entries = matches.count.positive? ? matches : nil
end

#split_itemsObject



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

def split_items
  items = { changed: [], new: [], improved: [], fixed: [], other: [] }

  @entries.each do |e|
    type = e.type.downcase.to_sym
    if items.key?(type)
      items[type] << e
    else
      items[:other] << e
    end
  end

  items
end

#to_hObject



59
60
61
# File 'lib/doing/changelog/change.rb', line 59

def to_h
  { version: @version, content: @content }
end

#to_sObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/doing/changelog/change.rb', line 78

def to_s
  date = @change_date.nil? ? '' : " _(#{@change_date.strftime('%F')})_"
  out = ["### __#{@version}__#{date}"]

  split_items.each do |type, members|
    next unless @only.include?(type)

    if members.count.positive?
      out << "#### #{type.to_s.capitalize}"
      out << members.map(&:to_s).join("\n")
    end
  end

  out.join("\n\n")
end