class Webgen::Tree
Represents a tree of nodes.
Attributes
The dummy root.
This is the default node that gets created when the Tree
is created so that the real root node can be treated like any other node. It has only one child, namely the real root node of the tree.
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/destination path, use node
instead.
The website to which the Tree
object belongs.
Public Class Methods
Create a new Tree
object for the website.
# File lib/webgen/tree.rb 26 def initialize(website) 27 @website = website 28 @node_access = {:alcn => {}, :acn => {}, :dest_path => {}, :translation_key => {}} 29 @dummy_root = Node.new(self, '', '') 30 end
Public Instance Methods
Access a node via its :alcn
.
# File lib/webgen/tree.rb 38 def [](path) 39 @node_access[:alcn][path] 40 end
Delete the node and all of its children from the tree.
The message :before_node_deleted
is sent with the to-be-deleted node before the node is actually deleted from the tree.
# File lib/webgen/tree.rb 127 def delete_node(node) 128 return if node.nil? || !node.kind_of?(Node) || node == @dummy_root 129 130 node.children.dup.each {|child| delete_node(child)} 131 132 @website.blackboard.dispatch_msg(:before_node_deleted, node) 133 node.parent.children.delete(node) 134 @node_access[:alcn].delete(node.alcn) 135 @node_access[:acn][node.acn].delete(node) 136 @node_access[:translation_key][translation_key(node)].delete(node) 137 @node_access[:dest_path].delete(node.dest_path) unless node.meta_info['no_output'] 138 end
Access a node via a path
of a specific type
.
If type is :alcn
(default) 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 :dest_path
then path
needs to be a destination path.
Returns the requested Node
or nil
if such a node does not exist.
# File lib/webgen/tree.rb 49 def node(path, type = :alcn) 50 case type 51 when :alcn then @node_access[type][path] 52 when :acn then @node_access[type][path] && @node_access[type][path].first 53 when :dest_path then @node_access[type][path] 54 else 55 raise ArgumentError, "Unknown type '#{type}' for resolving path <#{path}>" 56 end 57 end
Utility method called by Node#initialize. This method should not be used directly!
# File lib/webgen/tree.rb 100 def register_node(node) 101 if @node_access[:alcn].has_key?(node.alcn) 102 raise "Can't have two nodes with same alcn: #{node}" 103 else 104 @node_access[:alcn][node.alcn] = node 105 end 106 (@node_access[:acn][node.acn] ||= []) << node 107 (@node_access[:translation_key][translation_key(node)] ||= []) << node 108 if node.meta_info['no_output'] 109 # ignore node dest path 110 elsif @node_access[:dest_path].has_key?(node.dest_path) 111 raise "Can't have two nodes with same destination path: #{node.dest_path}" 112 else 113 @node_access[:dest_path][node.dest_path] = node 114 end 115 end
Return the node representing the given path
which can be an alcn/acn/destination path (name resolution is done in the specified order). The path has to be absolute, i.e. starting with a slash.
If the path
is an alcn and a node is found, it is returned. If the path
is an acn, the correct localized node according to lang
is returned or if no such node exists but an unlocalized version does, the unlocalized node is returned. If the path
is a destination path, the node with this destination path is returned.
If no node is found for the given path or if the path is invalid, nil
is returned and, if msg_on_failure
is true
, the message :node_resolution_failed (with parameters path
and lang
) is dispatched.
# File lib/webgen/tree.rb 71 def resolve_node(path, lang, msg_on_failure = false) 72 node = self.node(path, :alcn) 73 if !node || node.acn == path 74 (node = (self.node(path, :acn) || self.node(path + '/', :acn))) && (node = translate_node(node, lang)) 75 end 76 node = self.node(path, :dest_path) if !node 77 @website.blackboard.dispatch_msg(:node_resolution_failed, path, lang) if !node && msg_on_failure 78 node 79 end
The real root node of the tree.
# File lib/webgen/tree.rb 33 def root 34 @dummy_root.children.first 35 end
Return the translation of the node to the language lang
or, if no such node exists, an unlocalized version of the node. If no such node is found either, nil
is returned.
# File lib/webgen/tree.rb 83 def translate_node(node, lang) 84 avail = @node_access[:translation_key][translation_key(node)] 85 avail.find do |n| 86 n = n.parent while n.is_fragment? 87 n.lang == lang 88 end || avail.find do |n| 89 n = n.parent while n.is_fragment? 90 n.lang.nil? 91 end 92 end
Return all translations of the node.
# File lib/webgen/tree.rb 95 def translations(node) 96 @node_access[:translation_key][translation_key(node)].dup 97 end
Private Instance Methods
Return the translation key for the node.
# File lib/webgen/tree.rb 118 def translation_key(node) 119 node.meta_info['translation_key'] || node.acn 120 end