Commit 23db3f773ff167fb8b83e2e942d0df046d32e73e
1 parent
6be535aa
Exists in
master
Support array of hashes
Showing
3 changed files
with
18 additions
and
2 deletions
Show diff stats
lib/settingslogic.rb
... | ... | @@ -164,7 +164,13 @@ class Settingslogic < Hash |
164 | 164 | return @#{key} if @#{key} |
165 | 165 | return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}' |
166 | 166 | value = fetch('#{key}') |
167 | - @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value | |
167 | + @#{key} = if value.is_a?(Hash) | |
168 | + self.class.new(value, "'#{key}' section in #{@section}") | |
169 | + elsif value.is_a?(Array) && value.all?{|v| v.is_a? Hash} | |
170 | + value.map{|v| self.class.new(v)} | |
171 | + else | |
172 | + value | |
173 | + end | |
168 | 174 | end |
169 | 175 | EndEval |
170 | 176 | end | ... | ... |
spec/settings.yml
spec/settingslogic_spec.rb
... | ... | @@ -9,6 +9,10 @@ describe "Settingslogic" do |
9 | 9 | Settings.setting1.setting1_child.should == "saweet" |
10 | 10 | end |
11 | 11 | |
12 | + it "should access settings in nested arrays" do | |
13 | + Settings.array.first.name.should == "first" | |
14 | + end | |
15 | + | |
12 | 16 | it "should access deep nested settings" do |
13 | 17 | Settings.setting1.deep.another.should == "my value" |
14 | 18 | end | ... | ... |