Class: Webgen::Tree

Inherits:
Object
  • Object
show all
Includes:
WebsiteAccess
Defined in:
lib/webgen/tree.rb

Overview

Represents a tree of nodes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from WebsiteAccess

included, website

Constructor Details

#initializeTree

Create a new Tree object.



27
28
29
30
31
# File 'lib/webgen/tree.rb', line 27

def initialize
  @node_access = {:alcn => {}, :acn => {}, :path => {}}
  @node_info = {}
  @dummy_root = Node.new(self, '', '')
end

Instance Attribute Details

#dummy_rootObject (readonly)

The dummy root. This is the default node that gets created when the Tree is created sothat the real root node can be treated like any other node. It has only one child, namely the real root node of the tree.



16
17
18
# File 'lib/webgen/tree.rb', line 16

def dummy_root
  @dummy_root
end

#node_accessObject (readonly)

Direct access to the hashes for node resolving. Only use this for reading purposes! If you just want to get a specific node for an alcn/acn/output path, use #node instead.



20
21
22
# File 'lib/webgen/tree.rb', line 20

def node_access
  @node_access
end

#node_infoObject (readonly)

The hash containing processing information for each node. This is normally not accessed directly but via the Node#node_info method.



24
25
26
# File 'lib/webgen/tree.rb', line 24

def node_info
  @node_info
end

Instance Method Details

#delete_node(node_or_alcn) ⇒ Object

Delete the node identified by node_or_alcn and all of its children from the tree.

The message :before_node_deleted is sent with the to-be-deleted node before this node is actually deleted from the tree.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/webgen/tree.rb', line 73

def delete_node(node_or_alcn)
  n = node_or_alcn.kind_of?(Node) ? node_or_alcn : @node_access[:alcn][node_or_alcn]
  return if n.nil? || n == @dummy_root

  n.children.dup.each {|child| delete_node(child)}

  website.blackboard.dispatch_msg(:before_node_deleted, n)
  n.parent.children.delete(n)
  @node_access[:alcn].delete(n.absolute_lcn)
  @node_access[:acn][n.absolute_cn].delete(n)
  @node_access[:path].delete(n.path)

  node_info.delete(n.absolute_lcn)
end

#node(path, type = :alcn) ⇒ Object Also known as: []

Access a node via a path of a specific type. If type is alcn then path has to be an absolute localized canonical name, if type is acn then path has to be an absolute canonical name and if type is path then path needs to be an output path.

Returns the requested Node or nil if such a node does not exist.



43
44
45
# File 'lib/webgen/tree.rb', line 43

def node(path, type = :alcn)
  (type == :acn ? @node_access[type][path] && @node_access[type][path].first : @node_access[type][path])
end

#register_node(node) ⇒ Object

A utility method called by Node#initialize. This method should not be used directly!



49
50
51
52
53
54
55
56
57
# File 'lib/webgen/tree.rb', line 49

def register_node(node)
  if @node_access[:alcn].has_key?(node.absolute_lcn)
    raise "Can't have two nodes with same absolute lcn: #{node.absolute_lcn}"
  else
    @node_access[:alcn][node.absolute_lcn] = node
  end
  (@node_access[:acn][node.absolute_cn] ||= []) << node
  register_path(node)
end

#register_path(node) ⇒ Object

A utility method called by Node#reinit. This method should not be used directly!



60
61
62
63
64
65
66
67
# File 'lib/webgen/tree.rb', line 60

def register_path(node)
  return if node['no_output']
  if @node_access[:path].has_key?(node.path)
    raise "Can't have two nodes with same output path: #{node.path}"
  else
    @node_access[:path][node.path] = node
  end
end

#rootObject

The real root node of the tree.



34
35
36
# File 'lib/webgen/tree.rb', line 34

def root
  @dummy_root.children.first
end