class GitHubChangelogGenerator::ParserFile

ParserFile is a configuration file reader which sets options in the given Hash.

In your project's root, you can put a file named .github_changelog_generator to override defaults.

Example:

header_label=# My Super Changelog
; Comments are allowed
future-release=5.0.0
# Ruby-style comments, too
since-tag=1.0.0

The configuration format is some-key=value or some_key=value.

Constants

IRREGULAR_OPTIONS
KNOWN_ARRAY_KEYS
KNOWN_INTEGER_KEYS

Public Class Methods

new(options, io = nil) click to toggle source

@param options [Hash] options to be configured from file contents @param io [nil, IO] configuration file handle

# File lib/github_changelog_generator/parser_file.rb, line 48
def initialize(options, io = nil)
  @options = options
  @io = io
end

Public Instance Methods

parse!() click to toggle source

Sets options using configuration file content

# File lib/github_changelog_generator/parser_file.rb, line 54
def parse!
  return unless @io

  @io.each_with_index { |line, i| parse_line!(line, i + 1) }
  @io.close
end

Private Instance Methods

convert_value(value, option_name) click to toggle source
# File lib/github_changelog_generator/parser_file.rb, line 92
def convert_value(value, option_name)
  if KNOWN_ARRAY_KEYS.include?(option_name)
    value.split(",")
  elsif KNOWN_INTEGER_KEYS.include?(option_name)
    value.to_i
  elsif value =~ /^(true|t|yes|y|1)$/i
    true
  elsif value =~ /^(false|f|no|n|0)$/i
    false
  else
    value
  end
end
extract_pair(line) click to toggle source

Returns a the option name as a symbol and its string value sans newlines.

@param line [String] unparsed line from config file @return [Array<Symbol, String>]

# File lib/github_changelog_generator/parser_file.rb, line 81
def extract_pair(line)
  key, value = line.split("=", 2)
  [key.tr("-", "_").to_sym, value.gsub(/[\n\r]+/, "")]
end
non_configuration_line?(line) click to toggle source

Returns true if the line starts with a pound sign or a semi-colon.

# File lib/github_changelog_generator/parser_file.rb, line 73
def non_configuration_line?(line)
  line =~ /^[\#;]/ || line =~ /^\s+$/
end
option_key_for(option_name) click to toggle source
# File lib/github_changelog_generator/parser_file.rb, line 121
def option_key_for(option_name)
  IRREGULAR_OPTIONS.fetch(option_name) { option_name }
end
parse_line!(line, line_number) click to toggle source
# File lib/github_changelog_generator/parser_file.rb, line 63
def parse_line!(line, line_number)
  return if non_configuration_line?(line)

  option_name, value = extract_pair(line)
  @options[option_key_for(option_name)] = convert_value(value, option_name)
rescue StandardError
  raise ParserError, "Failed on line ##{line_number}: \"#{line.gsub(/[\n\r]+/, '')}\""
end