Commit 23db3f773ff167fb8b83e2e942d0df046d32e73e

Authored by Andrey Chernih
1 parent 6be535aa
Exists in master

Support array of hashes

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
... ... @@ -19,4 +19,10 @@ collides:
19 19 does: not
20 20 nested:
21 21 collides:
22   - does: not either
23 22 \ No newline at end of file
  23 + does: not either
  24 +
  25 +array:
  26 + -
  27 + name: first
  28 + -
  29 + name: second
... ...
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
... ...