class Hash

Public Instance Methods

flat_keys(separator = ".") click to toggle source
# File lib/flat_keys.rb, line 4
def flat_keys(separator = ".")
  hash = {}
  visit_flat_keys(hash, "", separator)
  return hash
end
unflat_keys(separator = '.') click to toggle source
# File lib/flat_keys.rb, line 10
def unflat_keys(separator = '.')
  hash = {}
  self.each do |k, v|
    names = k.split(separator)
    last_index = names.count - 1
    local_hash = hash
    names.each_with_index do |name, index|
      last = index == last_index
      if last
        local_hash[name] = v
      else
        local_hash = local_hash[name] || (local_hash[name] = Hash.new)
      end
    end
  end
  hash
end

Protected Instance Methods

visit_flat_keys(hash, prefix, separator) click to toggle source
# File lib/flat_keys.rb, line 30
def visit_flat_keys(hash, prefix, separator)
  self.each do |k, v|
    key = "#{prefix}#{k}"
    if v.is_a?(Hash)
      v.visit_flat_keys(hash, "#{key}#{separator}", separator)
    else
      hash[key] = v
    end
  end
end