Refactor Reader class to conform with Rubocop
This commit is contained in:
parent
1eee5bffd8
commit
9f06a20741
|
@ -23,6 +23,21 @@ module GitHubChangelogGenerator
|
||||||
# reader = GitHubChangelogGenerator::Reader.new
|
# reader = GitHubChangelogGenerator::Reader.new
|
||||||
# content = reader.read('./CHANGELOG.md')
|
# content = reader.read('./CHANGELOG.md')
|
||||||
class Reader
|
class Reader
|
||||||
|
def initialize(options = {})
|
||||||
|
defaults = {
|
||||||
|
heading_level: '##',
|
||||||
|
heading_structures: [
|
||||||
|
/^## \[(?<version>.+?)\]\((?<url>.+?)\)( \((?<date>.+?)\))?$/,
|
||||||
|
/^## (?<version>.+?)( \((?<date>.+?)\))?$/
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
@options = options.merge(defaults)
|
||||||
|
|
||||||
|
@heading_level = @options[:heading_level]
|
||||||
|
@heading_structures = @options[:heading_structures]
|
||||||
|
end
|
||||||
|
|
||||||
# Parse a single heading and return a Hash
|
# Parse a single heading and return a Hash
|
||||||
#
|
#
|
||||||
# The following heading structures are currently valid:
|
# The following heading structures are currently valid:
|
||||||
|
@ -34,16 +49,14 @@ module GitHubChangelogGenerator
|
||||||
# @param [String] heading Heading from the ChangeLog File
|
# @param [String] heading Heading from the ChangeLog File
|
||||||
# @return [Hash] Returns a structured Hash with version, url and date
|
# @return [Hash] Returns a structured Hash with version, url and date
|
||||||
def parse_heading(heading)
|
def parse_heading(heading)
|
||||||
structures = [
|
captures = { 'version' => nil, 'url' => nil, 'date' => nil }
|
||||||
/^## \[(?<version>v.+?)\]\((?<url>.+?)\)( \((?<date>.+?)\))?$/,
|
|
||||||
/^## (?<version>v.+?)( \((?<date>.+?)\))?$/,
|
|
||||||
]
|
|
||||||
|
|
||||||
captures = {'version' => nil, 'url' => nil, 'date' => nil}
|
@heading_structures.each do |regexp|
|
||||||
|
|
||||||
structures.each do |regexp|
|
|
||||||
matches = Regexp.new(regexp).match(heading)
|
matches = Regexp.new(regexp).match(heading)
|
||||||
captures.merge!(Hash[matches.names.map.zip(matches.captures)]) unless matches.nil?
|
captures.merge!(Hash[matches.names.map.zip(matches.captures)]) unless matches.nil?
|
||||||
|
|
||||||
|
# Try Regular Expressions until you find one that delivers results
|
||||||
|
break unless matches.nil?
|
||||||
end
|
end
|
||||||
|
|
||||||
captures
|
captures
|
||||||
|
|
Loading…
Reference in New Issue
Block a user