From 9f06a20741c5911d346fb4fc5236cb448191f76d Mon Sep 17 00:00:00 2001 From: Enrico Stahn Date: Thu, 26 Mar 2015 15:57:51 +1100 Subject: [PATCH] Refactor Reader class to conform with Rubocop --- lib/github_changelog_generator/reader.rb | 27 ++++++++++++++++++------ 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/github_changelog_generator/reader.rb b/lib/github_changelog_generator/reader.rb index 82d5526..fe1f61a 100644 --- a/lib/github_changelog_generator/reader.rb +++ b/lib/github_changelog_generator/reader.rb @@ -23,6 +23,21 @@ module GitHubChangelogGenerator # reader = GitHubChangelogGenerator::Reader.new # content = reader.read('./CHANGELOG.md') class Reader + def initialize(options = {}) + defaults = { + heading_level: '##', + heading_structures: [ + /^## \[(?.+?)\]\((?.+?)\)( \((?.+?)\))?$/, + /^## (?.+?)( \((?.+?)\))?$/ + ] + } + + @options = options.merge(defaults) + + @heading_level = @options[:heading_level] + @heading_structures = @options[:heading_structures] + end + # Parse a single heading and return a Hash # # The following heading structures are currently valid: @@ -34,16 +49,14 @@ module GitHubChangelogGenerator # @param [String] heading Heading from the ChangeLog File # @return [Hash] Returns a structured Hash with version, url and date def parse_heading(heading) - structures = [ - /^## \[(?v.+?)\]\((?.+?)\)( \((?.+?)\))?$/, - /^## (?v.+?)( \((?.+?)\))?$/, - ] + captures = { 'version' => nil, 'url' => nil, 'date' => nil } - captures = {'version' => nil, 'url' => nil, 'date' => nil} - - structures.each do |regexp| + @heading_structures.each do |regexp| matches = Regexp.new(regexp).match(heading) 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 captures