class Hash

Monkey patch hash to provide pathify method

Public Instance Methods

pathify(sep = '/', paths = [], path = '') click to toggle source

Returns a flat hash that show the full path to the value, so we don't have to recursively traverse a nested hash Examples:

> {foo: 'ff'}.pathify

=> {foo: 'ff'}

> {foo: {bar: 'gg', boo: 'hh'}}.pathify

=> {'foo/bar': 'gg', 'foo/boo': 'hh'}

Optionally, Specify a seperator as first argument (defaults to '/')

# File lib/plant/hash_pathify.rb, line 11
def pathify(sep = '/', paths = [], path = '')
  each do |key, value|
    if value.is_a? Hash
      value.pathify(sep, paths, "#{path}#{sep}#{key}")
    else
      paths << Hash["#{path}#{sep}#{key}".sub(/^#{sep}/, ''), value]
    end
  end
  paths.reduce({}, :merge)
end