Class: Hash

Inherits:
Object show all
Defined in:
lib/diakonos/core-ext/hash.rb

Direct Known Subclasses

BufferHash

Instance Method Summary collapse

Instance Method Details

#delete_key_path(path) ⇒ Object

path is an array of hash keys This method deletes a path of hash keys, with each step in the path being a recursively deeper key in a hash tree. Returns the possibly modified hash.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/diakonos/core-ext/hash.rb', line 6

def delete_key_path( path )
  if path.length > 1
    subtree = self[ path[ 0 ] ]
    if subtree.respond_to?( :delete_key_path )
      subtree.delete_key_path( path[ 1..-1 ] )
      if subtree.empty?
        delete( path[ 0 ] )
      end
    end
  elsif path.length == 1
    delete( path[ 0 ] )
  end

  self
end

#get_leaf(path) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/diakonos/core-ext/hash.rb', line 49

def get_leaf( path )
  node = get_node( path )
  if node.respond_to?( :get_node )
    # Only want a leaf node
    nil
  else
    node
  end
end

#get_node(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/diakonos/core-ext/hash.rb', line 36

def get_node( path )
  node = self[ path[ 0 ] ]
  if path.length > 1
    if node && node.respond_to?( :get_node )
      return node.get_node( path[ 1..-1 ] )
    end
  elsif path.length == 1
    return node
  end

  nil
end

#set_key_path(path, leaf) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/diakonos/core-ext/hash.rb', line 22

def set_key_path( path, leaf )
  if path.length > 1
    node = self[ path[ 0 ] ]
    if ! node.respond_to?( :set_key_path )
      node = self[ path[ 0 ] ] = Hash.new
    end
    node.set_key_path( path[ 1..-1 ], leaf )
  elsif path.length == 1
    self[ path[ 0 ] ] = leaf
  end

  self
end