class Hash

Public Instance Methods

at(path,opts={}) click to toggle source
# File lib/makeloc/core_extensions.rb, line 36
def at(path,opts={})
  array_path = path.to_s.split('.')
  current_key = array_path.shift
  # raise ArgumentError, "wrong key " unless self.keys.include?(current_key)
  raise ArgumentError, "wrong opts" unless (opts.empty? or opts[:set])
  if array_path.empty?
    case opts[:set]
    when :delete
      self.delete(current_key)
    when nil
      self[current_key]
    else
      self[current_key] = opts[:set]
    end
  else
    self[current_key] and self[current_key].at(array_path.join('.'),opts)
  end
end
delete_at(path) click to toggle source
# File lib/makeloc/core_extensions.rb, line 55
def delete_at(path)
  at(path, :set => :delete)
end
flatten_keys(prefix="") click to toggle source
# File lib/makeloc/core_extensions.rb, line 2
def flatten_keys(prefix="")
  keys = []
  self.keys.each do |key|
    keys << "#{prefix}#{key}"
    (keys << self[key].flatten_keys(prefix + "#{key}.")) if self[key].is_a? Hash
  end
  prefix == "" ? keys.flatten : keys
end
parse(only_leaves = false) { |k,v| ... } click to toggle source
# File lib/makeloc/core_extensions.rb, line 11
def parse(only_leaves = false, &block)
  each do |k,v|
    if v.is_a? Hash
      yield(k,v) unless only_leaves
      v.parse(only_leaves,&block)
    else
      yield(k,v)
    end
  end
end
parse_leaves(&block) click to toggle source
# File lib/makeloc/core_extensions.rb, line 22
def parse_leaves(&block)
  parse(only_leaves = true, &block)
end
update_leaves!() { |k,v| ... } click to toggle source
# File lib/makeloc/core_extensions.rb, line 26
def update_leaves!(&block)
  each do |k,v|
    if v.is_a? Hash
      v.update_leaves!(&block)
    else
      self[k] = yield(k,v)
    end
  end
end