Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

symbolize_keys for issue #33 #38

Merged
merged 1 commit into from

2 participants

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 22, 2012
  1. @goosetav
This page is out of date. Refresh to see the latest.
Showing with 34 additions and 2 deletions.
  1. +17 −2 lib/settingslogic.rb
  2. +17 −0 spec/settingslogic_spec.rb
View
19 lib/settingslogic.rb
@@ -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
17 spec/settingslogic_spec.rb
@@ -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.
Something went wrong with that request. Please try again.