Sha256: 4dd6eac12b20edd521831834636a4404c1bec04a5fa6333b4b61f63a580f80d9

Contents?: true

Size: 932 Bytes

Versions: 1

Compression:

Stored size: 932 Bytes

Contents

require 'key_tree/path'

module KeyTree
  # A tree of key-value lookup tables (hashes)
  class Tree < Hash
    #
    # KeyTree::Tree.new(+hash+)
    #
    # Initialize a new KeyTree from nested Hash:es
    #
    def self.[](hash = {})
      keytree = Tree.new
      hash.each do |key, value|
        keytree[key] = value
      end
      keytree
    end

    def [](key_or_path)
      super(Path[key_or_path])
    end

    def fetch(key_or_path, *args, **kvargs, &proc)
      super(Path[key_or_path], *args, **kvargs, &proc)
    end

    def values_at(*keys)
      super(keys.map { |key_or_path| Path[key_or_path] })
    end

    def []=(key_or_path, new_value)
      path = Path[key_or_path]

      each_key { |key| delete(key) if path.conflict?(key) }

      case new_value
      when Hash
        new_value.each { |suffix, value| super(path + suffix, value) }
      else
        super(path, new_value)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
key_tree-0.1.0 lib/key_tree/tree.rb