Module: Diakonos::KeyMap
- Defined in:
- lib/diakonos/key-map.rb
Instance Method Summary collapse
-
#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.
- #get_leaf(path) ⇒ Object
- #get_node(path) ⇒ Object
- #set_key_path(path, leaf) ⇒ Object
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.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/diakonos/key-map.rb', line 8 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
51 52 53 54 55 56 57 58 59 |
# File 'lib/diakonos/key-map.rb', line 51 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
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/diakonos/key-map.rb', line 38 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
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/diakonos/key-map.rb', line 24 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 ] ] = {}.extend( KeyMap ) end node.set_key_path( path[ 1..-1 ], leaf ) elsif path.length == 1 self[ path[ 0 ] ] = leaf end self end |