File tree Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Expand file tree Collapse file tree 2 files changed +34
-2
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ class << self
10
10
def name # :nodoc:
11
11
self . superclass != Hash && instance . key? ( "name" ) ? instance . name : super
12
12
end
13
-
13
+
14
14
# Enables Settings.get('nested.key.name') for dynamic access
15
15
def get ( key )
16
16
parts = key . split ( '.' )
@@ -167,7 +167,22 @@ def #{key}
167
167
end
168
168
EndEval
169
169
end
170
-
170
+
171
+ def symbolize_keys
172
+
173
+ inject ( { } ) do |memo , tuple |
174
+
175
+ k = ( tuple . first . to_sym rescue tuple . first ) || tuple . first
176
+
177
+ v = k . is_a? ( Symbol ) ? send ( k ) : tuple . last # make sure the value is accessed the same way Settings.foo.bar works
178
+
179
+ memo [ k ] = v && v . respond_to? ( :symbolize_keys ) ? v . symbolize_keys : v #recurse for nested hashes
180
+
181
+ memo
182
+ end
183
+
184
+ end
185
+
171
186
def missing_key ( msg )
172
187
return nil if self . class . suppress_errors
173
188
Original file line number Diff line number Diff line change @@ -168,6 +168,23 @@ class NoSource < Settingslogic; end
168
168
it "should allow a name setting to be overriden" do
169
169
Settings . name . should == 'test'
170
170
end
171
+
172
+ it "should allow symbolize_keys" do
173
+ Settings . reload!
174
+ result = Settings . language . haskell . symbolize_keys
175
+ result . class . should == Hash
176
+ result . should == { :paradigm => "functional" }
177
+ end
178
+
179
+ it "should allow symbolize_keys on nested hashes" do
180
+ Settings . reload!
181
+ result = Settings . language . symbolize_keys
182
+ result . class . should == Hash
183
+ result . should == {
184
+ :haskell => { :paradigm => "functional" } ,
185
+ :smalltalk => { :paradigm => "object oriented" }
186
+ }
187
+ end
171
188
172
189
it "should handle empty file" do
173
190
SettingsEmpty . keys . should eql ( [ ] )
You can’t perform that action at this time.
0 commit comments