Skip to content
This repository
Browse code

Merge pull request #38 from goosetav/master

symbolize_keys for issue #33
  • Loading branch information...
commit c510affe2e7ab96289525b95f50a5dd528854807 2 parents 52722f1 + f890392
Ben Johnson yourewelcome authored

Showing 2 changed files with 34 additions and 2 deletions. Show diff stats Hide diff stats

  1. +17 2 lib/settingslogic.rb
  2. +17 0 spec/settingslogic_spec.rb
19 lib/settingslogic.rb
@@ -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
17 spec/settingslogic_spec.rb
@@ -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([])

0 comments on commit c510aff

Please sign in to comment.
Something went wrong with that request. Please try again.