Class: Reissue::DirectoryFragmentHandler

Inherits:
FragmentHandler show all
Defined in:
lib/reissue/fragment_handler/directory_fragment_handler.rb

Overview

Handler for reading fragments from a directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from FragmentHandler

for

Constructor Details

#initialize(directory, valid_sections: Reissue.changelog_sections) ⇒ DirectoryFragmentHandler

Initialize the handler with a directory path

Parameters:

  • directory (String)

    The path to the fragments directory

  • valid_sections (Array<String>, nil) (defaults to: Reissue.changelog_sections)

    List of valid section names, or nil to allow all



14
15
16
17
18
# File 'lib/reissue/fragment_handler/directory_fragment_handler.rb', line 14

def initialize(directory, valid_sections: Reissue.changelog_sections)
  @directory = directory
  @fragment_directory = Pathname.new(directory)
  @valid_sections = valid_sections
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



8
9
10
# File 'lib/reissue/fragment_handler/directory_fragment_handler.rb', line 8

def directory
  @directory
end

#valid_sectionsObject (readonly)

Returns the value of attribute valid_sections.



8
9
10
# File 'lib/reissue/fragment_handler/directory_fragment_handler.rb', line 8

def valid_sections
  @valid_sections
end

Instance Method Details

#clearnil

Clear all fragment files from the directory

Returns:

  • (nil)


52
53
54
55
56
# File 'lib/reissue/fragment_handler/directory_fragment_handler.rb', line 52

def clear
  return unless @fragment_directory.exist?

  @fragment_directory.glob("*.*.md").each(&:delete)
end

#readHash

Read fragments from the directory

Returns:

  • (Hash)

    A hash of changelog entries organized by category



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/reissue/fragment_handler/directory_fragment_handler.rb', line 23

def read
  return {} unless @fragment_directory.exist?

  fragments = {}

  @fragment_directory.glob("*.*.md").each do |fragment_file|
    filename = fragment_file.basename.to_s
    parts = filename.split(".")

    next unless parts.length == 3

    section = parts[1].downcase
    next unless valid_section?(section)

    content = fragment_file.read.strip
    next if content.empty?

    # Capitalize section name for changelog format
    section_key = section.capitalize
    fragments[section_key] ||= []
    fragments[section_key] << content
  end

  fragments
end