Class: Trivet::Document

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Querier
Defined in:
lib/trivet.rb

Overview

A Trivet::Document object holds the root of a tree. It is not necessary to have a Trivet::Document object to create a tree, but it can be handy to have an object that holds the tree but is not part of the tree itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Querier

#query_first

Constructor Details

#initialize(new_root = nil) ⇒ Document

Accepts an optional root node.



1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
# File 'lib/trivet.rb', line 1411

def initialize(new_root=nil)
	# init
	@root = nil
	@misc = {}
	
	# set new root
	if new_root
		if new_root.is_a?(Class)
			new_root = new_root.new()
		end
		
		new_root.parent = self
	end
end

Instance Attribute Details

#miscObject (readonly)

A hash of any miscellaneous information you want to attach to the node.



1438
1439
1440
# File 'lib/trivet.rb', line 1438

def misc
  @misc
end

#rootObject

Returns the root node. Returns nil if the document does not have a root.



1435
1436
1437
# File 'lib/trivet.rb', line 1435

def root
  @root
end

Instance Method Details

#[](id) ⇒ Object

Shortcut for #node_by_id().



1552
1553
1554
# File 'lib/trivet.rb', line 1552

def [](id)
	return node_by_id(id)
end

#childrenObject

If there is a root node, returns an array consisting of just the root node. Else returns an empty array. This method if is a convenience to that a node can always call its parent’s children.



1498
1499
1500
1501
1502
1503
1504
# File 'lib/trivet.rb', line 1498

def children
	if @root
		return [@root]
	else
		return []
	end
end

#query(qobj, opts = {}, &block) ⇒ Object

Runs a query starting with, and including, the root node. See Trivet::Node#query for details.



1537
1538
1539
1540
# File 'lib/trivet.rb', line 1537

def query(qobj, opts={}, &block)
	opts = {'self'=>true}.merge(opts)
	return @root.query(qobj, opts, &block)
end

#set_root(new_root, opts = {}) ⇒ Object

Sets the root node. The given object must be a Trivet::Node object or nil.



1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
# File 'lib/trivet.rb', line 1464

def set_root(new_root, opts={})
	# $tm.hrm
	opts = {'recurse'=>true}.merge(opts)
	
	# must be node
	unless new_root.is_a?(Trivet::Node) or new_root.nil?
		raise 'root-not-trivet-node-or-nil'
	end
	
	# unlink old root
	if @root and opts['recurse']
		@root.unlink('recurse'=>false)
	end
	
	# set root
	@root = new_root
	
	# set parent
	if new_root and opts['recurse']
		new_root.parent = self
	end
end

#traverse(&block) ⇒ Object

Traverses the tree starting with the root node. See Trivet::Node#traverse for details.



1517
1518
1519
1520
1521
1522
1523
1524
1525
# File 'lib/trivet.rb', line 1517

def traverse(&block)
	# if no root
	if not @root
		raise 'cannot-traverse-without-root'
	end
	
	# traverse
	@root.traverse 'self'=>true, &block
end