class DocuBot::LinkTree::Node

Attributes

page[RW]
parent[RW]
title[RW]

Public Class Methods

new( title=nil, link=nil, page=nil ) click to toggle source
# File lib/docubot/link_tree.rb, line 7
def initialize( title=nil, link=nil, page=nil )
        @title,@link,@page = title,link,page
        @children = []
end

Public Instance Methods

<<( node ) click to toggle source
# File lib/docubot/link_tree.rb, line 39
def <<( node )
        node.parent = self
        @children << node
end
[]( child_index ) click to toggle source
# File lib/docubot/link_tree.rb, line 44
def []( child_index )
        @children[child_index]
end
ancestors() click to toggle source
# File lib/docubot/link_tree.rb, line 71
def ancestors
        # Cached assuming no one is going to shuffle the nodes after placement
        return @ancestors if @ancestors
        @ancestors = []
        node = self
        @ancestors << node while node = node.parent
        @ancestors.reverse!
end
anchor() click to toggle source
# File lib/docubot/link_tree.rb, line 12
def anchor
        @link[/#(.+)/,1]
end
children( parent_link=nil, &block ) click to toggle source
# File lib/docubot/link_tree.rb, line 48
def children( parent_link=nil, &block )
        if parent_link
                root = find( parent_link )
                root ? root.children( &block ) : []
        else
                @children
        end
end
depth() click to toggle source
# File lib/docubot/link_tree.rb, line 66
def depth
        # Cached assuming no one is going to shuffle the nodes after placement
        @depth ||= ancestors.length
end
descendants() click to toggle source
# File lib/docubot/link_tree.rb, line 57
def descendants
        ( @children + @children.map{ |child| child.descendants } ).flatten
end
file() click to toggle source
# File lib/docubot/link_tree.rb, line 16
def file
        @link.sub(/#.+/,'')
end
find( link ) click to toggle source
# File lib/docubot/link_tree.rb, line 61
def find( link )
        # TODO: this is eminently cachable
        descendants.find{ |node| node.link==link }
end
leaf?() click to toggle source
# File lib/docubot/link_tree.rb, line 20
def leaf?
        !@children.any?{ |node| node.page != @page }
end
to_s() click to toggle source
# File lib/docubot/link_tree.rb, line 80
def to_s
        "#{@title} (#{@link}) - #{@page && @page.title}"
end
to_txt( depth=0 ) click to toggle source
# File lib/docubot/link_tree.rb, line 84
def to_txt( depth=0 )
        indent = "  "*depth
        [
                indent+to_s,
                children.map{|kid|kid.to_txt(depth+1)}
        ].flatten.join("\n")
end