Class: Arboretum::DocTree::Elements::ElementGroup

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/arboretum/doctree.rb

Overview

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

Direct Known Subclasses

AdjacentElementGroup, GroupListener

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements = []) ⇒ ElementGroup

Returns a new instance of ElementGroup.



1328
1329
1330
# File 'lib/arboretum/doctree.rb', line 1328

def initialize(elements=[])
  @group = (elements.nil? or elements.empty?) ? [] : elements
end

Instance Attribute Details

#groupObject

Returns the value of attribute group.



1326
1327
1328
# File 'lib/arboretum/doctree.rb', line 1326

def group
  @group
end

Instance Method Details

#+(other) ⇒ Object



1350
1351
1352
# File 'lib/arboretum/doctree.rb', line 1350

def +(other)
  ElementGroup.new(self.group + other.group)
end

#adjacent?Boolean

Returns:

  • (Boolean)


1354
1355
1356
1357
1358
1359
1360
# 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

#copyObject

Deep copy for ElementGroup



1369
1370
1371
# File 'lib/arboretum/doctree.rb', line 1369

def copy
  ElementGroup.new(@group.map {|member| member.copy})
end

#detachObject Also known as: prune, delete

Detach from current parent/siblings



1374
1375
1376
1377
1378
# File 'lib/arboretum/doctree.rb', line 1374

def detach
  @group.each do |member|
    member.detach
  end
end

#graft_first_onto(graft_parent) ⇒ Object

Graft onto another element of the tree as the first child



1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
# 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) ⇒ Object

Graft onto another element of the tree as the last child



1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
# 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)) ⇒ 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



1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
# 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

#listingObject

Provides a listing/array containing the elements of the group



1333
1334
1335
# File 'lib/arboretum/doctree.rb', line 1333

def listing
  @group
end

#onlyObject

Raises:

  • (IndexError)


1337
1338
1339
1340
# 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

#text_elementsObject



1346
1347
1348
# 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_stringObject



1342
1343
1344
# 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_adjacentObject



1362
1363
1364
1365
1366
# File 'lib/arboretum/doctree.rb', line 1362

def to_adjacent
  adj_group = AdjacentElementGroup.new
  adj_group.base(@group[0])
  adj_group.fill
end