Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/core_ext.rb

Overview

Hash

Instance Method Summary collapse

Instance Method Details

#underscore_keysObject

:nodoc



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/locomotive/core_ext.rb', line 23

def underscore_keys
  new_hash = {}

  self.each_pair do |key, value|
    if value.respond_to?(:collect!) # Array
      value.collect do |item|
        if item.respond_to?(:each_pair) # Hash item within
          item.underscore_keys
        else
          item
        end
      end
    elsif value.respond_to?(:each_pair) # Hash
      value = value.underscore_keys
    end

    new_key = key.is_a?(String) ? key.underscore : key # only String keys

    new_hash[new_key] = value
  end

  self.replace(new_hash)
end