Update util functions

Cribbed from http://api.rubyonrails.org/classes/Hash.html#method-i-symbolize_keys
This commit is contained in:
John Piasetzki 2014-04-28 02:06:41 -04:00
parent b5b55c90de
commit 3e6a6ffb52
1 changed files with 10 additions and 10 deletions

View File

@ -48,17 +48,21 @@ module Jekyll
array || [] array || []
end end
def transform_keys(hash)
result = {}
hash.each_key do |key|
result[yield(key)] = hash[key]
end
result
end
# Apply #to_sym to all keys in the hash # Apply #to_sym to all keys in the hash
# #
# hash - the hash to which to apply this transformation # hash - the hash to which to apply this transformation
# #
# Returns a new hash with symbolized keys # Returns a new hash with symbolized keys
def symbolize_hash_keys(hash) def symbolize_hash_keys(hash)
target = hash.dup transform_keys(hash) { |key| key.to_sym rescue key }
target.keys.each do |key|
target[(key.to_sym rescue key) || key] = target.delete(key)
end
target
end end
# Apply #to_s to all keys in the Hash # Apply #to_s to all keys in the Hash
@ -67,11 +71,7 @@ module Jekyll
# #
# Returns a new hash with stringified keys # Returns a new hash with stringified keys
def stringify_hash_keys(hash) def stringify_hash_keys(hash)
target = hash.dup transform_keys(hash) { |key| key.to_s rescue key }
target.keys.each do |key|
target[(key.to_s rescue key) || key] = target.delete(key)
end
target
end end
end end