Skip to content
This repository
Browse code

Merge pull request #43 from AndreyChernyh/arrays-support

Support array of hashes
  • Loading branch information...
commit 184afc7b9ef4e3a2d1a1065823c7e9c30ce1e376 2 parents a905605 + 23db3f7
Ben Johnson authored December 30, 2012
8  lib/settingslogic.rb
@@ -169,7 +169,13 @@ def #{key}
169 169
         return @#{key} if @#{key}
170 170
         return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
171 171
         value = fetch('#{key}')
172  
-        @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
  172
+        @#{key} = if value.is_a?(Hash)
  173
+          self.class.new(value, "'#{key}' section in #{@section}")
  174
+        elsif value.is_a?(Array) && value.all?{|v| v.is_a? Hash}
  175
+          value.map{|v| self.class.new(v)}
  176
+        else
  177
+          value
  178
+        end
173 179
       end
174 180
     EndEval
175 181
   end
8  spec/settings.yml
@@ -19,4 +19,10 @@ collides:
19 19
   does: not
20 20
 nested:
21 21
   collides:
22  
-    does: not either
  22
+    does: not either
  23
+
  24
+array:
  25
+  -
  26
+    name: first
  27
+  -
  28
+    name: second
6  spec/settingslogic_spec.rb
@@ -9,6 +9,10 @@
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
@@ -171,4 +175,4 @@ class NoSource < Settingslogic; end
171 175
     end
172 176
   end
173 177
 
174  
-end
  178
+end

0 notes on commit 184afc7

Please sign in to comment.
Something went wrong with that request. Please try again.