module Midpay::HashExtensions::StringifyKeys

Public Instance Methods

stringify_keys() click to toggle source

Return a new hash with all keys converted to strings.

# File lib/midpay/hash/key_conversion.rb, line 20
def stringify_keys
  dup.stringify_keys!
end
stringify_keys!() click to toggle source

Convert all keys in the hash to strings.

@example

test = {:abc => 'def'}
test.stringify_keys!
test # => {'abc' => 'def'}
# File lib/midpay/hash/key_conversion.rb, line 10
def stringify_keys!
  keys.each do |k|
    stringify_keys_recursively!(self[k])
    self[k.to_s] = self.delete(k)
  end
  self
end

Protected Instance Methods

stringify_keys_recursively!(object) click to toggle source

Stringify all keys recursively within nested hashes and arrays.

# File lib/midpay/hash/key_conversion.rb, line 28
def stringify_keys_recursively!(object)
  if self.class === object
    object.stringify_keys!
  elsif ::Array === object
    object.each do |i|
      stringify_keys_recursively!(i)
    end
    object
  elsif object.respond_to?(:stringify_keys!)
    object.stringify_keys!
  else
    object
  end
end