Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a method-based accessor for SimpleConfig hashes #1544

Merged
merged 1 commit into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/utils/simpleconfig.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ def parse_group_line(line, opts)
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)
Expand Down
7 changes: 7 additions & 0 deletions test/unit/utils/simpleconfig_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,11 @@
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