ParserFile: use a File instead of a filename

- spec: can use StringIOs instead of loose files
This commit is contained in:
Olle Jonsson
2016-03-19 13:59:58 +01:00
parent f9ad19285e
commit 93df2d5646
6 changed files with 35 additions and 26 deletions
+17 -6
View File
@@ -6,21 +6,32 @@ module GitHubChangelogGenerator
class ParserFile
FILENAME = ".github_changelog_generator"
def initialize(options)
attr_reader :file
# @param options [Hash]
# @param file [nil,IO]
def initialize(options, file = read_default_file)
@options = options
@file = file
end
def read_default_file
File.open(path) if path.exist?
end
def path
@path ||= Pathname(File.expand_path(FILENAME))
end
# Destructively change @options using data in configured options file.
def parse!
file.each_line { |line| parse_line!(line) } if file.exist?
return unless file
file.each_line { |line| parse_line!(line) }
file.close
end
private
def file
@file ||= Pathname(File.expand_path(@options[:params_file] || FILENAME))
end
def parse_line!(line)
option_name, value = extract_pair(line)
@options[option_key_for(option_name)] = convert_value(value, option_name)