Class: Google::HashUtils

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

Overview

Helper class to process and mutate hashes.

Class Method Summary collapse

Class Method Details

.camelize_keys(source) ⇒ Object

Converts all keys to symbols



20
21
22
23
24
25
26
# File 'lib/google/hash_utils.rb', line 20

def self.camelize_keys(source)
  result = source.clone
  result.keys.each do |k|
    result[Google::StringUtils.camelize(k.to_s)] = result.delete(k)
  end
  result
end

Allows fetching objects within a tree path.



29
30
31
32
33
34
35
36
# File 'lib/google/hash_utils.rb', line 29

def self.navigate(source, path, default = nil)
  key = path.take(1)[0]
  path = path.drop(1)
  return default unless source.key?(key)
  result = source.fetch(key)
  return HashUtils.navigate(result, path, default) unless path.empty?
  return result if path.empty?
end

.path2navigate(path) ⇒ Object

Converts a path in the form a/b/c/d into %w(a b c d)



39
40
41
# File 'lib/google/hash_utils.rb', line 39

def self.path2navigate(path)
  "%w[#{path.split('/').join(' ')}]"
end