Class: MyJohnDeere::HashUtils

Inherits:
Object
  • Object
show all
Defined in:
lib/myjohndeere/hash_utils.rb

Class Method Summary collapse

Class Method Details

.deep_stringify_keys(in_hash) ⇒ Object

Convert keys to strings, recursively



27
28
29
30
31
# File 'lib/myjohndeere/hash_utils.rb', line 27

def self.deep_stringify_keys(in_hash)
  transform_hash(in_hash, :deep => true) {|hash, key, value|
    hash[key.to_s] = value
  }
end

.stringify_keys(in_hash) ⇒ Object

Convert keys to strings



20
21
22
23
24
# File 'lib/myjohndeere/hash_utils.rb', line 20

def self.stringify_keys(in_hash)
  transform_hash(in_hash) {|hash, key, value|
    hash[key.to_s] = value
  }
end

.transform_hash(original, options = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/myjohndeere/hash_utils.rb', line 3

def self.transform_hash(original, options={}, &block)
  original.inject({}){|result, (key,value)|
    value = if (options[:deep] && Hash === value) 
              transform_hash(value, options, &block)
            else 
              if Array === value
                value.map{|v| transform_hash(v, options, &block)}
              else
                value
              end
            end
    block.call(result,key,value)
    result
  }
end