Skip to content

Commit 23db3f7

Browse files
committed
Support array of hashes
1 parent 6be535a commit 23db3f7

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/settingslogic.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ def #{key}
164164
return @#{key} if @#{key}
165165
return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
166166
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
168174
end
169175
EndEval
170176
end

spec/settings.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ collides:
1919
does: not
2020
nested:
2121
collides:
22-
does: not either
22+
does: not either
23+
24+
array:
25+
-
26+
name: first
27+
-
28+
name: second

spec/settingslogic_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
Settings.setting1.setting1_child.should == "saweet"
1010
end
1111

12+
it "should access settings in nested arrays" do
13+
Settings.array.first.name.should == "first"
14+
end
15+
1216
it "should access deep nested settings" do
1317
Settings.setting1.deep.another.should == "my value"
1418
end

0 commit comments

Comments
 (0)