Class: DocuBot::LinkTree::Node
- Inherits:
-
Object
- Object
- DocuBot::LinkTree::Node
- Defined in:
- lib/docubot/link_tree.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#link ⇒ Object
Returns the value of attribute link.
-
#page ⇒ Object
Returns the value of attribute page.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#title ⇒ Object
Returns the value of attribute title.
Instance Method Summary collapse
- #<<(node) ⇒ Object
- #[](child_index) ⇒ Object
-
#add_to_link_hierarchy(title, link, page = nil) ⇒ Object
Add a new link underneath a link to its logical parent.
- #ancestors ⇒ Object
- #anchor ⇒ Object
- #children(parent_link = nil, &block) ⇒ Object
- #depth ⇒ Object
- #descendants ⇒ Object
- #file ⇒ Object
- #find(link) ⇒ Object
-
#initialize(title = nil, link = nil, page = nil) ⇒ Node
constructor
A new instance of Node.
- #leaf? ⇒ Boolean
- #to_s ⇒ Object
- #to_txt(depth = 0) ⇒ Object
Constructor Details
#initialize(title = nil, link = nil, page = nil) ⇒ Node
Returns a new instance of Node.
7 8 9 10 |
# File 'lib/docubot/link_tree.rb', line 7 def initialize( title=nil, link=nil, page=nil ) @title,@link,@page = title,link,page @children = [] end |
Instance Attribute Details
#link ⇒ Object
Returns the value of attribute link.
5 6 7 |
# File 'lib/docubot/link_tree.rb', line 5 def link @link end |
#page ⇒ Object
Returns the value of attribute page.
5 6 7 |
# File 'lib/docubot/link_tree.rb', line 5 def page @page end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/docubot/link_tree.rb', line 5 def parent @parent end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/docubot/link_tree.rb', line 5 def title @title end |
Instance Method Details
#<<(node) ⇒ Object
39 40 41 42 |
# File 'lib/docubot/link_tree.rb', line 39 def <<( node ) node.parent = self @children << node end |
#[](child_index) ⇒ Object
44 45 46 |
# File 'lib/docubot/link_tree.rb', line 44 def []( child_index ) @children[child_index] end |
#add_to_link_hierarchy(title, link, page = nil) ⇒ Object
Add a new link underneath a link to its logical parent
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/docubot/link_tree.rb', line 25 def add_to_link_hierarchy( title, link, page=nil ) node = DocuBot::LinkTree::Node.new( title, link, page ) parent_link = if node.anchor node.file elsif File.basename(link)=='index.html' File.dirname(File.dirname(link))/'index.html' else (File.dirname(link) / 'index.html') end #puts "Adding #{title.inspect} (#{link}) to hierarchy under #{parent_link}" parent = descendants.find{ |n| n.link==parent_link } || self parent << node end |
#ancestors ⇒ Object
71 72 73 74 75 76 77 78 |
# 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 ⇒ Object
12 13 14 |
# File 'lib/docubot/link_tree.rb', line 12 def anchor @link[/#(.+)/,1] end |
#children(parent_link = nil, &block) ⇒ Object
48 49 50 51 52 53 54 55 |
# 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 ⇒ Object
66 67 68 69 |
# 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 ⇒ Object
57 58 59 |
# File 'lib/docubot/link_tree.rb', line 57 def descendants ( @children + @children.map{ |child| child.descendants } ).flatten end |
#file ⇒ Object
16 17 18 |
# File 'lib/docubot/link_tree.rb', line 16 def file @link.sub(/#.+/,'') end |
#find(link) ⇒ Object
61 62 63 64 |
# File 'lib/docubot/link_tree.rb', line 61 def find( link ) # TODO: this is eminently cachable descendants.find{ |node| node.link==link } end |
#leaf? ⇒ Boolean
20 21 22 |
# File 'lib/docubot/link_tree.rb', line 20 def leaf? !@children.any?{ |node| node.page != @page } end |
#to_s ⇒ Object
80 81 82 |
# File 'lib/docubot/link_tree.rb', line 80 def to_s "#{@title} (#{@link}) - #{@page && @page.title}" end |
#to_txt(depth = 0) ⇒ Object
84 85 86 87 88 89 90 |
# 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 |