Permalink
Browse files

Merge pull request #38 from goosetav/master

symbolize_keys for issue #33
  • Loading branch information...
2 parents 52722f1 + f890392 commit c510affe2e7ab96289525b95f50a5dd528854807 @binarylogic committed Dec 30, 2012
Showing with 34 additions and 2 deletions.
  1. +17 −2 lib/settingslogic.rb
  2. +17 −0 spec/settingslogic_spec.rb
View
@@ -10,7 +10,7 @@ class << self
def name # :nodoc:
self.superclass != Hash && instance.key?("name") ? instance.name : super
end
-
+
# Enables Settings.get('nested.key.name') for dynamic access
def get(key)
parts = key.split('.')
@@ -167,7 +167,22 @@ def #{key}
end
EndEval
end
-
+
+ def symbolize_keys
+
+ inject({}) do |memo, tuple|
+
+ k = (tuple.first.to_sym rescue tuple.first) || tuple.first
+
+ v = k.is_a?(Symbol) ? send(k) : tuple.last # make sure the value is accessed the same way Settings.foo.bar works
+
+ memo[k] = v && v.respond_to?(:symbolize_keys) ? v.symbolize_keys : v #recurse for nested hashes
+
+ memo
+ end
+
+ end
+
def missing_key(msg)
return nil if self.class.suppress_errors
View
@@ -168,6 +168,23 @@ class NoSource < Settingslogic; end
it "should allow a name setting to be overriden" do
Settings.name.should == 'test'
end
+
+ it "should allow symbolize_keys" do
+ Settings.reload!
+ result = Settings.language.haskell.symbolize_keys
+ result.class.should == Hash
+ result.should == {:paradigm => "functional"}
+ end
+
+ it "should allow symbolize_keys on nested hashes" do
+ Settings.reload!
+ result = Settings.language.symbolize_keys
+ result.class.should == Hash
+ result.should == {
+ :haskell => {:paradigm => "functional"},
+ :smalltalk => {:paradigm => "object oriented"}
+ }
+ end
it "should handle empty file" do
SettingsEmpty.keys.should eql([])

0 comments on commit c510aff

Please sign in to comment.