class Arboretum::DocTree::Elements::AdjacentElementGroup
A group of adjacent/sibling elements Must strictly be connected to one another continuously (in order) AdjacentElementGroups have all functionality of ElementGroups They can also wrapped and swapped with other Elements/AdjacentElementGroups
Attributes
Public Class Methods
# File lib/arboretum/doctree.rb, line 1478 def initialize(element=nil) @group = element.nil? ? [] : [element] end
Public Instance Methods
# File lib/arboretum/doctree.rb, line 1490 def +(other) ElementGroup.new(self.group + other.group) end
# File lib/arboretum/doctree.rb, line 1494 def adjacent? true end
Assign starting element of the AdjacentElementGroup
, if there is not already one
# File lib/arboretum/doctree.rb, line 1483 def base(element) raise IndexError.new("Base element already selected") if not @group.empty? raise TypeError.new("Base element cannot be nil") if element.nil? @group << element self end
Deep copy for AdjacentElementGroup
# File lib/arboretum/doctree.rb, line 1499 def copy group_copy = @group.map {|member| member.copy} Element.stitch!(nil, group_copy.first) group_copy.each_cons(2) {|current, following| Element.stitch!(current,following)} Element.stitch(group_copy.last, nil) adj_group_copy = AdjacentElementGroup.new adj_group_copy.base(group_copy.first) adj_group_copy.fill end
Detach from current parent/siblings, but maintain internal references between siblings
# File lib/arboretum/doctree.rb, line 1551 def detach next_sibling = @group[-1].nil? ? nil : @group[-1].sibling_next prev_sibling = @group[0].nil? ? nil : @group[0].sibling_prev Element.stitch!(prev_sibling, next_sibling) @group[0].parent.set_children!(@group[0].parent.children - @group) unless (@group[0].nil? or @group[0].parent.nil?) @group.each {|member| member.set_parent!(nil)} @group.first.set_sibling_prev!(nil) @group.last.set_sibling_next!(nil) end
Extend the adjacent group to the last element's next sibling
# File lib/arboretum/doctree.rb, line 1535 def extend_next @group << @group.last.sibling_next unless @group.last.sibling_next.nil? self end
Extend the adjacent group to the first element's previous sibling
# File lib/arboretum/doctree.rb, line 1540 def extend_prev @group.unshift(@group.first.sibling_prev) unless @group.first.sibling_prev.nil? self end
Extend group as much as it can be extended
# File lib/arboretum/doctree.rb, line 1510 def fill if not @group.empty? extend_prev until @group.first.sibling_prev.nil? extend_next until @group.last.sibling_next.nil? end self end
Graft onto another element of the tree as the first child
# File lib/arboretum/doctree.rb, line 1595 def graft_first_onto(graft_parent) # Detach from current context self.detach # Update context @group.each do |member| member.set_parent!(graft_parent) end next_child = graft_parent.children[0] Element.stitch!(nil, @group[0]) Element.stitch!(@group[-1], next_child) # Insert graft group at the beginning of parent children graft_parent.children.insert(0, *@group) end
Graft onto another element of the tree as the last child
# File lib/arboretum/doctree.rb, line 1613 def graft_last_onto(graft_parent) # Detach from current context self.detach # Update context @group.each do |member| member.set_parent!(graft_parent) end previous_child = graft_parent.children[-1] Element.stitch!(previous_child, @group[0]) Element.stitch!(@group[-1], nil) # Push graft group onto parent children graft_parent.children.push(*@group) end
Graft onto another element of the tree at any index of its children By default, it will graft as the last element of the other element's children
# File lib/arboretum/doctree.rb, line 1566 def graft_onto(graft_parent, index=-1) # If index to small or large, graft to edges of graft_parent children if index.abs > graft_parent.children.length index = graft_parent.children.length * (index > 1 ? 1 : 0) end if index == graft_parent.children.length or index == -1 self.graft_last_onto(graft_parent) elsif index == 0 self.graft_first_onto(graft_parent) else # Detach from current context self.detach # Update context @group.each do |member| member.set_parent!(graft_parent) end previous_child = graft_parent.children[index-1] next_child = graft_parent.children[index] Element.stitch!(previous_child, @group[0]) Element.stitch!(@group[-1], next_child) # Graft group at index graft_parent.children.insert(index, *@group) end end
# File lib/arboretum/doctree.rb, line 1518 def index_in_parent self.parent.children.index(@group.first) end
Provides a listing/array containing the elements of the group
# File lib/arboretum/doctree.rb, line 1546 def listing @group end
# File lib/arboretum/doctree.rb, line 1530 def parent @group.first.parent end
# File lib/arboretum/doctree.rb, line 1526 def sibling_next @group.last.sibling_next end
# File lib/arboretum/doctree.rb, line 1522 def sibling_prev @group.first.sibling_prev end