Class: Tree::TreeNode

Inherits:
Object
  • Object
show all
Defined in:
lib/hmenu/extensions/tree.rb

Direct Known Subclasses

HMenu::Node

Instance Method Summary collapse

Instance Method Details

#add_recursive(path, content) ⇒ Object Also known as: add_path

Has the same effect of using Tree::TreeNode#<< multiple time but with a filepath-like syntax:

node.add_recursive '/path/to/a/deep/node', "MyContent"

A leading slash in the path means that it is absolute (i.e. refers to the tree root)

path may be an Array (instead of String), but this is intended for “private” use only (i.e. recursive calls)



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hmenu/extensions/tree.rb', line 20

def add_recursive(path, content)
  if path.respond_to? :split
    path = path.split('/') 
  end
  if path[0] == "" # as a result of a leading slash in path string
    path.shift
    return self.root.add_recursive(path, content)        
  end
  if path.length == 1
    begin
      node = self.class.new(path[0], content) # it may be a derived class
      add node
      return node
    rescue RuntimeError # node already exists
      self[path[0]].content = content
    end
  else
    begin 
      add self.class.new(path[0], nil) # it may be a derived class
    rescue RuntimeError # node already exists
      # do nothing
    end
    first = path.shift
    self[first].add_recursive(path, content) 
  end
end