Module: Traverse
- Included in:
- Rind::Cdata, Rind::Comment, Rind::Element, Rind::ProcessingInstruction, Rind::Text
- Defined in:
- lib/rind/traverse.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
Returns the value of attribute parent.
Instance Method Summary collapse
-
#ancestors(path = nil) ⇒ Object
Creates a Rind::Nodes list of all ancestors.
-
#descendants(path = nil) ⇒ Object
Creates a Rind::Nodes list of all descendants.
-
#down(path = nil) ⇒ Object
Returns the first descendant node.
-
#next(path = nil) ⇒ Object
Returns the first sibling that follows the current node in the list of siblings.
-
#next_siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings that follow the current node in the list of siblings.
-
#prev(path = nil) ⇒ Object
Returns the first sibling that proceeds the current node in the list of siblings.
-
#prev_siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings that proceed the current node in the list of siblings.
-
#siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings.
-
#up(path = nil) ⇒ Object
Returns the first ancestor node.
Instance Attribute Details
#parent ⇒ Object
Returns the value of attribute parent.
2 3 4 |
# File 'lib/rind/traverse.rb', line 2 def parent @parent end |
Instance Method Details
#ancestors(path = nil) ⇒ Object
Creates a Rind::Nodes list of all ancestors. If an Xpath is provided it will only return the nodes that match.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/rind/traverse.rb', line 6 def ancestors(path = nil) if not self.parent.nil? ancestors = Rind::Nodes.new([self.parent]) parent_ancestors = self.parent.ancestors ancestors.push(*parent_ancestors) if not parent_ancestors.nil? path.nil? ? ancestors : ancestors.filter(path) end end |
#descendants(path = nil) ⇒ Object
Creates a Rind::Nodes list of all descendants. If an Xpath is provided it will only return the nodes that match.
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rind/traverse.rb', line 19 def descendants(path = nil) if self.respond_to? :children and not self.children.empty? descendants = Rind::Nodes.new(self.children) self.children.each do |child| child_descendants = child.descendants descendants.push(*child_descendants) if not child_descendants.nil? end path.nil? ? descendants : descendants.filter(path) end end |
#down(path = nil) ⇒ Object
Returns the first descendant node. If an Xpath is provided it will return the first one that matches.
34 35 36 37 |
# File 'lib/rind/traverse.rb', line 34 def down(path = nil) descendants = self.descendants(path) descendants.first if not descendants.nil? end |
#next(path = nil) ⇒ Object
Returns the first sibling that follows the current node in the list of siblings. If an Xpath is provided it will return the first one that matches.
42 43 44 45 |
# File 'lib/rind/traverse.rb', line 42 def next(path = nil) siblings = self.next_siblings(path) siblings.first if not siblings.nil? end |
#next_siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings that follow the current node in the list of siblings. If an Xpath is provided it will only return the nodes that match.
50 51 52 53 54 |
# File 'lib/rind/traverse.rb', line 50 def next_siblings(path = nil) children = self.parent.children siblings = Rind::Nodes.new(children[children.exact_index(self)+1..children.length-1]) path.nil? ? siblings : siblings.filter(path) end |
#prev(path = nil) ⇒ Object
Returns the first sibling that proceeds the current node in the list of siblings. If an Xpath is provided it will return the first one that matches.
59 60 61 62 |
# File 'lib/rind/traverse.rb', line 59 def prev(path = nil) siblings = self.prev_siblings(path) siblings.last if not siblings.nil? end |
#prev_siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings that proceed the current node in the list of siblings. If an Xpath is provided it will only return the nodes that match.
67 68 69 70 71 |
# File 'lib/rind/traverse.rb', line 67 def prev_siblings(path = nil) children = self.parent.children siblings = Rind::Nodes.new(children[0...children.exact_index(self)]) path.nil? ? siblings : siblings.filter(path) end |
#siblings(path = nil) ⇒ Object
Creates a Rind::Nodes list of all siblings. If an Xpath is provided it will only return the nodes that match.
75 76 77 78 |
# File 'lib/rind/traverse.rb', line 75 def siblings(path = nil) siblings = Rind::Nodes.new(self.parent.children.find_all{|child| not child.equal? self}) path.nil? ? siblings : siblings.filter(path) end |
#up(path = nil) ⇒ Object
Returns the first ancestor node. If an Xpath is provided it will return the first one that matches.
82 83 84 85 |
# File 'lib/rind/traverse.rb', line 82 def up(path = nil) ancestors = self.ancestors(path) ancestors.first if not ancestors.nil? end |