Class: Trivet::Document
- Inherits:
-
Object
- Object
- Trivet::Document
- 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
-
#misc ⇒ Object
readonly
A hash of any miscellaneous information you want to attach to the node.
-
#root ⇒ Object
Returns the root node.
Instance Method Summary collapse
-
#[](id) ⇒ Object
Shortcut for #node_by_id().
-
#children ⇒ Object
If there is a root node, returns an array consisting of just the root node.
-
#initialize(new_root = nil) ⇒ Document
constructor
Accepts an optional root node.
-
#query(qobj, opts = {}, &block) ⇒ Object
Runs a query starting with, and including, the root node.
-
#set_root(new_root, opts = {}) ⇒ Object
Sets the root node.
-
#traverse(&block) ⇒ Object
Traverses the tree starting with the root node.
Methods included from Querier
Constructor Details
#initialize(new_root = nil) ⇒ Document
Accepts an optional root node.
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 |
# File 'lib/trivet.rb', line 1438 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
#misc ⇒ Object (readonly)
A hash of any miscellaneous information you want to attach to the node.
1465 1466 1467 |
# File 'lib/trivet.rb', line 1465 def misc @misc end |
#root ⇒ Object
Returns the root node. Returns nil if the document does not have a root.
1462 1463 1464 |
# File 'lib/trivet.rb', line 1462 def root @root end |
Instance Method Details
#[](id) ⇒ Object
Shortcut for #node_by_id().
1579 1580 1581 |
# File 'lib/trivet.rb', line 1579 def [](id) return node_by_id(id) end |
#children ⇒ Object
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.
1525 1526 1527 1528 1529 1530 1531 |
# File 'lib/trivet.rb', line 1525 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.
1564 1565 1566 1567 |
# File 'lib/trivet.rb', line 1564 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.
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 |
# File 'lib/trivet.rb', line 1491 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.
1544 1545 1546 1547 1548 1549 1550 1551 1552 |
# File 'lib/trivet.rb', line 1544 def traverse(&block) # if no root if not @root raise 'cannot-traverse-without-root' end # traverse @root.traverse 'self'=>true, &block end |