Skip to content

Commit

Permalink
Merge pull request #38 from goosetav/master
Browse files Browse the repository at this point in the history
symbolize_keys for issue #33
  • Loading branch information
binarylogic committed Dec 30, 2012
2 parents 52722f1 + f890392 commit c510aff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/settingslogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions spec/settingslogic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down

0 comments on commit c510aff

Please sign in to comment.