Merge pull request #1544 from chef/adamleff/hash-values-in-simpleconfig

Provide a method-based accessor for SimpleConfig hashes
This commit is contained in:
Adam Leff 2017-03-22 15:46:58 -04:00 committed by GitHub
commit 73e3bfda7a
2 changed files with 14 additions and 1 deletions

View file

@ -73,7 +73,13 @@ class SimpleConfig
m = opts[:group_re].match(line)
return nil if m.nil?
@groups.push(m[1])
@vals = @params[m[1]] = {}
# We use a Hashie::Mash to provide method syntax for retrieving
# values. For configs that have keys whose values may be hashes
# (such as a mysql config that has [sections]), providing
# a hash whose values are accessible via methods provide
# a better user experience with `its` blocks.
@vals = @params[m[1]] = Hashie::Mash.new
end
def parse_implicit_assignment_line(line, opts)

View file

@ -77,4 +77,11 @@ describe 'SimpleConfig Default Parser' do
cur.params.must_equal(res)
cur.groups.must_equal(['g', 'k'])
end
it 'provides methods to access returned hashes' do
cur = SimpleConfig.new("[section1]\nkey1 = value1\n\n[section2]\nkey2 = value2\n")
cur.params['section1'].key1.must_equal('value1')
cur.params['section2'].key2.must_equal('value2')
cur.params['section2'].missing_key.must_be_nil
end
end