Skip to content

Instantly share code, notes, and snippets.

@tommeier
Created December 15, 2011 00:38
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tommeier/1479299 to your computer and use it in GitHub Desktop.
Save tommeier/1479299 to your computer and use it in GitHub Desktop.
Add methods for Hash to deep_stringify_keys or deep_symbolize_keys
#Load in rails via:
# config/initializers/core_extensions.rb
#Forcibly require our core extensions after rails has loaded
Dir.glob(Rails.root.join('lib', 'core_ext', '*.rb')).each { |extension| require extension }
#lib/core_ext/hash.rb
class Hash
def deep_symbolize_keys
dup.deep_symbolize_keys!
end
def deep_stringify_keys
dup.deep_stringify_keys!
end
def deep_symbolize_keys!
deep_change_keys!(:to_sym)
end
def deep_stringify_keys!
deep_change_keys!(:to_s)
end
def deep_change_keys!(change_method = :to_s)
self.keys.each do |k|
new_key = k.send(change_method)
current_value = self.delete(k)
self[new_key] = current_value.is_a?(Hash) ? current_value.dup.deep_change_keys!(change_method) : current_value
end
self
end
end
require 'spec_helper'
describe Hash do
before(:all) do
@example_deep_hash = {
:sym_key => {
:level_1 => {
:level_2 => {
:level_3 => 'Some Value'
}
}
},
'str_key' => {
'level_1' => {
'level_2' => {
'level_3' => 'Some Value'
}
}
}
}
@example_shallow_hash = {
:simple_key => 'simple value',
'simpleStringKey' => 'simple second value'
}
end
describe "#deep_symbolize_keys" do
before(:all) do
@expected_deep_hash = {
:sym_key => {
:level_1 => {
:level_2 => {
:level_3 => 'Some Value'
}
}
},
:str_key => {
:level_1 => {
:level_2 => {
:level_3 => 'Some Value'
}
}
}
}
@expected_shallow_hash = {
:simple_key => 'simple value',
:simpleStringKey => 'simple second value'
}
end
it 'should be able to run without destructive update' do
@example_deep_hash.deep_symbolize_keys.should == @expected_deep_hash
@example_deep_hash.should == @example_deep_hash
end
it 'should convert deep hashes to symbolized keys' do
test_hash = @example_deep_hash.dup
test_hash.deep_symbolize_keys!
test_hash.should == @expected_deep_hash
end
it 'should convert simple hashes to symbolized keys' do
test_hash = @example_shallow_hash.dup
test_hash.deep_symbolize_keys!
test_hash.should == @expected_shallow_hash
end
end
describe "#deep_stringify_keys" do
before(:all) do
@expected_deep_hash = {
'sym_key' => {
'level_1' => {
'level_2' => {
'level_3' => 'Some Value'
}
}
},
'str_key' => {
'level_1' => {
'level_2' => {
'level_3' => 'Some Value'
}
}
}
}
@expected_shallow_hash = {
'simple_key' => 'simple value',
'simpleStringKey' => 'simple second value'
}
end
it 'should be able to run without destructive update' do
@example_deep_hash.deep_stringify_keys.should == @expected_deep_hash
@example_deep_hash.should == @example_deep_hash
end
it 'should convert deep hashes to string keys' do
test_hash = @example_deep_hash.dup
test_hash.deep_stringify_keys!
test_hash.should == @expected_deep_hash
end
it 'should convert simple hashes to string keys' do
test_hash = @example_shallow_hash.dup
test_hash.deep_stringify_keys!
test_hash.should == @expected_shallow_hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment