Permalink
Browse files

The issue with 'nested keys' was fixed.

Signed-off-by: Ben Johnson <bjohnson@binarylogic.com>
  • Loading branch information...
MitinPavel authored and binarylogic committed Sep 1, 2009
1 parent e927113 commit 28dc69b0b32565742d6c03ea06995d31657576e3
Showing with 31 additions and 6 deletions.
  1. +18 −4 lib/settingslogic.rb
  2. BIN spec/.settings.yml.swp
  3. +7 −1 spec/settings.yml
  4. +6 −1 spec/settingslogic_spec.rb
View
@@ -53,15 +53,29 @@ def initialize(hash_or_file = self.class.source)
self.update hash
end
end
+
+ def singleton(obj)
+ class << obj; self; end
+ end
private
def method_missing(name, *args, &block)
if key?(name.to_s)
- value = self[name.to_s].is_a?(Hash) ? self.class.new(self[name.to_s]) : self[name.to_s]
- self.class.send(:define_method, name) { value }
- send(name)
+ find_and_define name
else
super
end
end
-end
+
+ def find_and_define(name)
+ node_or_value = self[name.to_s]
+ unless node_or_value.is_a? Hash
+ singleton(self).send(:define_method, name) { node_or_value }
+ node_or_value
+ else
+ node = self.class.new self[name.to_s]
+ singleton(self).send(:define_method, name) { node }
+ node
+ end
+ end
+end
View
Binary file not shown.
View
@@ -7,4 +7,10 @@ setting1:
setting2: 5
setting3: <%= 5 * 5 %>
-name: test
+name: test
+a:
+ b:
+ d: blah
+ c:
+ d: hah
+
@@ -28,4 +28,9 @@
it "should namespace settings" do
Settings2.setting1_child.should == "saweet"
end
-end
+
+ it "should distinguish nested keys" do
+ Settings.a.b.d.should == 'blah'
+ Settings.a.c.d.should == 'hah'
+ end
+end

0 comments on commit 28dc69b

Please sign in to comment.