Skip to content

Commit

Permalink
Merge pull request #37 from jasonwbarnett/fix-comment-issue
Browse files Browse the repository at this point in the history
Fix #36
  • Loading branch information
dmitryshagin authored Sep 30, 2021
2 parents b920983 + 770afaa commit dae22ab
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
8 changes: 6 additions & 2 deletions lib/parseconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def import_config
else
add(var_name, new_value)
end
elsif /^\[(.+)\]$/.match(line).to_a != []
group = /^\[(.+)\]$/.match(line).to_a[1]
elsif /^\[(.+)\](\s*#{escaped_comment_regex}+.*)?$/.match(line).to_a != []
group = /^\[(.+)\](\s*#{escaped_comment_regex}+.*)?$/.match(line).to_a[1]
add(group, {})
elsif /\w+/.match(line)
add(line.to_s.chomp.strip, true)
Expand All @@ -104,6 +104,10 @@ def import_config
end
end

def escaped_comment_regex
/[#{Regexp.escape(@comments.join(''))}]/
end

# This method will provide the value held by the object "@param"
# where "@param" is actually the name of the param in the config
# file.
Expand Down
8 changes: 7 additions & 1 deletion tests/files/groups.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
[group1]
[group1]; other
foo = bar

[group2]
baz = quux

[group3]# this is a comment
blah = dah

[group4] # this is a comment
sha = ma
6 changes: 6 additions & 0 deletions tests/files/groups.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
},
"group2" => {
"baz" => "quux"
},
"group3" => {
"blah" => "dah"
},
"group4" => {
"sha" => "ma"
}
}

0 comments on commit dae22ab

Please sign in to comment.