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
-
#root ⇒ Object
Returns the root node.
Instance Method Summary collapse
-
#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.
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 |
# File 'lib/trivet.rb', line 1241 def initialize(new_root=nil) # init @root = nil # 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
#root ⇒ Object
Returns the root node. Returns nil if the document does not have a root.
1264 1265 1266 |
# File 'lib/trivet.rb', line 1264 def root @root end |
Instance Method Details
#query(qobj, opts = {}, &block) ⇒ Object
Runs a query starting with, and including, the root node. See Trivet::Node#query for details.
1342 1343 1344 1345 |
# File 'lib/trivet.rb', line 1342 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.
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 |
# File 'lib/trivet.rb', line 1289 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.
1322 1323 1324 1325 1326 1327 1328 1329 1330 |
# File 'lib/trivet.rb', line 1322 def traverse(&block) # if no root if not @root raise 'cannot-traverse-without-root' end # traverse @root.traverse 'self'=>true, &block end |