Permalink
Browse files

symbolize_keys method. fix for #33

  • Loading branch information...
1 parent 4884d45 commit f8903920fb6c3239c6b73b1c1c7580c44631347c @goosetav goosetav committed Jun 22, 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
@@ -152,6 +152,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
# Put this test last or else call to .instance will load @instance,
# masking bugs.

0 comments on commit f890392

Please sign in to comment.