Sha256: e3f5aefc22601f28328633e64d4389471dc6efcdb3ee7f54a9d7f0feac2cf3f2

Contents?: true

Size: 1.99 KB

Versions: 19

Compression:

Stored size: 1.99 KB

Contents

module Mongoo
  class Mongohash
    extend Forwardable

    def_delegators :@raw_hash, :==, :[], :[], :[]=, :clear, :default, :default=, :default_proc, :delete, :delete_if,
                   :each, :each_key, :each_pair, :each_value, :empty?, :fetch, :has_key?, :has_value?, :include?,
                   :index, :indexes, :indices, :initialize_copy, :inspect, :invert, :key?, :keys, :length, :member?,
                   :merge, :merge!, :pretty_print, :pretty_print_cycle, :rehash, :reject, :reject!, :replace, :select,
                   :shift, :size, :sort, :store, :to_a, :to_hash, :to_s, :update, :value?, :values, :values_at

    attr_reader :raw_hash

    def initialize(hash={})
      hash = hash.to_hash unless hash.class.to_s == "Hash"
      @raw_hash = hash.deep_stringify_keys!
    end

    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

    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

    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

    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

    def to_key_value
      kv = {}; dot_list.collect { |k| kv[k] = dot_get(k) }; kv
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
mongoo-0.5.7 lib/mongoo/mongohash.rb
mongoo-0.5.6 lib/mongoo/mongohash.rb
mongoo-0.5.5 lib/mongoo/mongohash.rb
mongoo-0.5.4 lib/mongoo/mongohash.rb
mongoo-0.5.3 lib/mongoo/mongohash.rb
mongoo-0.5.2 lib/mongoo/mongohash.rb
mongoo-0.5.1 lib/mongoo/mongohash.rb
mongoo-0.5.0 lib/mongoo/mongohash.rb
mongoo-0.4.10 lib/mongoo/mongohash.rb
mongoo-0.4.9 lib/mongoo/mongohash.rb
mongoo-0.4.8 lib/mongoo/mongohash.rb
mongoo-0.4.7 lib/mongoo/mongohash.rb
mongoo-0.4.6 lib/mongoo/mongohash.rb
mongoo-0.4.5 lib/mongoo/mongohash.rb
mongoo-0.4.4 lib/mongoo/mongohash.rb
mongoo-0.4.3 lib/mongoo/mongohash.rb
mongoo-0.4.2 lib/mongoo/mongohash.rb
mongoo-0.4.1 lib/mongoo/mongohash.rb
mongoo-0.4.0 lib/mongoo/mongohash.rb