Skip to content

Commit 184afc7

Browse files
committed
Merge pull request #43 from AndreyChernyh/arrays-support
Support array of hashes
2 parents a905605 + 23db3f7 commit 184afc7

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/settingslogic.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,13 @@ def #{key}
169169
return @#{key} if @#{key}
170170
return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
171171
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
173179
end
174180
EndEval
175181
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: 5 additions & 1 deletion
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
@@ -171,4 +175,4 @@ class NoSource < Settingslogic; end
171175
end
172176
end
173177

174-
end
178+
end

0 commit comments

Comments
 (0)