Class: Arboretum::DocTree::Elements::AdjacentElementGroup
- Inherits:
-
ElementGroup
- Object
- ElementGroup
- Arboretum::DocTree::Elements::AdjacentElementGroup
- Extended by:
- Forwardable
- Includes:
- Contextualized, Enumerable
- Defined in:
- lib/arboretum/doctree.rb
Overview
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
Instance Attribute Summary collapse
-
#group ⇒ Object
Returns the value of attribute group.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #adjacent? ⇒ Boolean
-
#base(element) ⇒ Object
Assign starting element of the AdjacentElementGroup, if there is not already one.
-
#copy ⇒ Object
Deep copy for AdjacentElementGroup.
-
#detach ⇒ Object
(also: #prune, #delete)
Detach from current parent/siblings, but maintain internal references between siblings.
-
#extend_next ⇒ Object
Extend the adjacent group to the last element’s next sibling.
-
#extend_prev ⇒ Object
Extend the adjacent group to the first element’s previous sibling.
-
#fill ⇒ Object
Extend group as much as it can be extended.
-
#graft_first_onto(graft_parent) ⇒ Object
Graft onto another element of the tree as the first child.
-
#graft_last_onto(graft_parent) ⇒ Object
Graft onto another element of the tree as the last child.
-
#graft_onto(graft_parent, index = -1)) ⇒ Object
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.
- #index_in_parent ⇒ Object
-
#initialize(element = nil) ⇒ AdjacentElementGroup
constructor
A new instance of AdjacentElementGroup.
-
#listing ⇒ Object
Provides a listing/array containing the elements of the group.
- #parent ⇒ Object
- #sibling_next ⇒ Object
- #sibling_prev ⇒ Object
Methods included from Contextualized
Methods inherited from ElementGroup
#only, #text_elements, #text_string
Constructor Details
#initialize(element = nil) ⇒ AdjacentElementGroup
Returns a new instance of AdjacentElementGroup.
1478 1479 1480 |
# File 'lib/arboretum/doctree.rb', line 1478 def initialize(element=nil) @group = element.nil? ? [] : [element] end |
Instance Attribute Details
#group ⇒ Object
Returns the value of attribute group.
1476 1477 1478 |
# File 'lib/arboretum/doctree.rb', line 1476 def group @group end |
Instance Method Details
#+(other) ⇒ Object
1490 1491 1492 |
# File 'lib/arboretum/doctree.rb', line 1490 def +(other) ElementGroup.new(self.group + other.group) end |
#adjacent? ⇒ Boolean
1494 1495 1496 |
# File 'lib/arboretum/doctree.rb', line 1494 def adjacent? true end |
#base(element) ⇒ Object
Assign starting element of the AdjacentElementGroup, if there is not already one
1483 1484 1485 1486 1487 1488 |
# 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 |
#copy ⇒ Object
Deep copy for AdjacentElementGroup
1499 1500 1501 1502 1503 1504 1505 1506 1507 |
# 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 ⇒ Object Also known as: prune, delete
Detach from current parent/siblings, but maintain internal references between siblings
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 |
# 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_next ⇒ Object
Extend the adjacent group to the last element’s next sibling
1535 1536 1537 1538 |
# File 'lib/arboretum/doctree.rb', line 1535 def extend_next @group << @group.last.sibling_next unless @group.last.sibling_next.nil? self end |
#extend_prev ⇒ Object
Extend the adjacent group to the first element’s previous sibling
1540 1541 1542 1543 |
# File 'lib/arboretum/doctree.rb', line 1540 def extend_prev @group.unshift(@group.first.sibling_prev) unless @group.first.sibling_prev.nil? self end |
#fill ⇒ Object
Extend group as much as it can be extended
1510 1511 1512 1513 1514 1515 1516 |
# 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_first_onto(graft_parent) ⇒ Object
Graft onto another element of the tree as the first child
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 |
# 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_last_onto(graft_parent) ⇒ Object
Graft onto another element of the tree as the last child
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 |
# 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(graft_parent, index = -1)) ⇒ Object
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
1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 |
# 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 |
#index_in_parent ⇒ Object
1518 1519 1520 |
# File 'lib/arboretum/doctree.rb', line 1518 def index_in_parent self.parent.children.index(@group.first) end |
#listing ⇒ Object
Provides a listing/array containing the elements of the group
1546 1547 1548 |
# File 'lib/arboretum/doctree.rb', line 1546 def listing @group end |
#parent ⇒ Object
1530 1531 1532 |
# File 'lib/arboretum/doctree.rb', line 1530 def parent @group.first.parent end |
#sibling_next ⇒ Object
1526 1527 1528 |
# File 'lib/arboretum/doctree.rb', line 1526 def sibling_next @group.last.sibling_next end |
#sibling_prev ⇒ Object
1522 1523 1524 |
# File 'lib/arboretum/doctree.rb', line 1522 def sibling_prev @group.first.sibling_prev end |