class Arboretum::DocTree::Elements::ElementGroup

An arbitrary group of elements ElementGroups can be placed and moved to a single location like standard elements

Attributes

group[RW]

Public Class Methods

new(elements=[]) click to toggle source
# File lib/arboretum/doctree.rb, line 1328
def initialize(elements=[])
  @group = (elements.nil? or elements.empty?) ? [] : elements
end

Public Instance Methods

+(other) click to toggle source
# File lib/arboretum/doctree.rb, line 1350
def +(other)
  ElementGroup.new(self.group + other.group)
end
adjacent?() click to toggle source
# File lib/arboretum/doctree.rb, line 1354
def adjacent?
  return true if @group.length <= 1
  @group.each_cons(2) do |current, following|
    return false if !current.sibling_next.eql? following or !following.sibling_prev.eql? current
  end
  @group.first.sibling_prev.nil? and @group.last.sibling_next.nil?
end
copy() click to toggle source

Deep copy for ElementGroup

# File lib/arboretum/doctree.rb, line 1369
def copy
  ElementGroup.new(@group.map {|member| member.copy})
end
delete()
Alias for: detach
detach() click to toggle source

Detach from current parent/siblings

# File lib/arboretum/doctree.rb, line 1374
def detach
  @group.each do |member|
    member.detach
  end
end
Also aliased as: prune, delete
graft_first_onto(graft_parent) click to toggle source

Graft onto another element of the tree as the first child

# File lib/arboretum/doctree.rb, line 1433
def graft_first_onto(graft_parent)
  # Detach from current context
  self.detach

  # Update context
  previous_child = nil
  next_child = graft_parent.children[0]
  @group.each do |member|
    member.set_parent!(graft_parent)

    Element.stitch!(previous_child, member)
    previous_child = member
  end
  Element.stitch!(@group[-1], next_child)

  # Graft group at index
  graft_parent.children.insert(0, *@group)
end
graft_last_onto(graft_parent) click to toggle source

Graft onto another element of the tree as the last child

# File lib/arboretum/doctree.rb, line 1414
def graft_last_onto(graft_parent)
  # Detach from current context
  self.detach

  # Update context
  previous_child = graft_parent.children[-1]
  @group.each do |member|
    member.set_parent!(graft_parent)

    Element.stitch!(previous_child, member)
    previous_child = member
  end
  Element.stitch!(@group[-1], nil)

  # Push graft group onto parent children
  graft_parent.children.push(*@group)
end
graft_onto(graft_parent, index=-1) click to toggle source

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 1384
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
    previous_child = graft_parent.children[index-1]
    next_child = graft_parent.children[index]
    @group.each do |member|
      member.set_parent!(graft_parent)

      Element.stitch!(previous_child, member)
      previous_child = member
    end
    Element.stitch!(@group[-1], next_child)

    # Graft group at index
    graft_parent.children.insert(index, *@group)
  end
end
listing() click to toggle source

Provides a listing/array containing the elements of the group

# File lib/arboretum/doctree.rb, line 1333
def listing
  @group
end
only() click to toggle source
# File lib/arboretum/doctree.rb, line 1337
def only
  raise IndexError.new("Element is not the only in its group") if not @group.length == 1
  @group.first
end
prune()
Alias for: detach
text_elements() click to toggle source
# File lib/arboretum/doctree.rb, line 1346
def text_elements
  Array.new.tap{|full_list| @group.each {|element| full_list << element.text_elements} }
end
text_string() click to toggle source
# File lib/arboretum/doctree.rb, line 1342
def text_string
  String.new.tap{|full_string| @group.each {|element| full_string << element.text_string} }
end
to_adjacent() click to toggle source
# File lib/arboretum/doctree.rb, line 1362
def to_adjacent
  adj_group = AdjacentElementGroup.new
  adj_group.base(@group[0])
  adj_group.fill
end