class Mongoo::Mongohash

Attributes

raw_hash[R]

Public Class Methods

new(hash={}) click to toggle source
# File lib/mongoo/mongohash.rb, line 13
def initialize(hash={})
  hash = hash.to_hash unless hash.class.to_s == "Hash"
  @raw_hash = hash.deep_stringify_keys!
end

Public Instance Methods

dot_delete(k) click to toggle source
# File lib/mongoo/mongohash.rb, line 44
def dot_delete(k)
  parts    = k.to_s.split(".")
  curr_val = to_hash
  while !parts.empty?
    part = parts.shift
    if parts.empty?
      curr_val.delete(part)
      return true
    else
      curr_val = curr_val[part]
    end
  end
  false
end
dot_get(k) click to toggle source
# File lib/mongoo/mongohash.rb, line 33
def dot_get(k)
  parts    = k.to_s.split(".")
  curr_val = to_hash
  while !parts.empty?
    part = parts.shift
    curr_val = curr_val[part]
    return curr_val unless curr_val.is_a?(Hash)
  end
  curr_val
end
dot_list(curr_hash=self.to_hash, path=[]) click to toggle source
# File lib/mongoo/mongohash.rb, line 59
def dot_list(curr_hash=self.to_hash, path=[])
  list = []
  curr_hash.each do |k,v|
    if v.is_a?(Hash)
      list.concat dot_list(v, (path + [k]))
    else
      list << (path + [k]).join(".")
    end
  end
  list
end
dot_set(k,v) click to toggle source
# File lib/mongoo/mongohash.rb, line 18
def dot_set(k,v)
  parts    = k.to_s.split(".")
  curr_val = to_hash
  while !parts.empty?
    part = parts.shift
    if parts.empty?
      curr_val[part] = v
    else
      curr_val[part] ||= {}
      curr_val = curr_val[part]
    end
  end
  true
end
to_key_value() click to toggle source
# File lib/mongoo/mongohash.rb, line 71
def to_key_value
  kv = {}; dot_list.collect { |k| kv[k] = dot_get(k) }; kv
end