Method: Dom::Node#add_node
- Defined in:
- lib/dom/node.rb
#add_node(path, node) ⇒ Object #add_node(node) ⇒ Object
There are three different cases
-
Last Childnode i.e. “.foo” -> Append node as child
-
absolute path i.e. “Foo” -> delegate path resolution to Dom.root
-
relative path i.e. “.bar.foo”
-
if there is a matching child for first element, delegate adding to this node
-
create NoDoc node and delegate rest to this NoDoc
-
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/dom/node.rb', line 258 def add_node(*args) # Grabbing right overload if args.count == 2 node = args[1] path = args[0] elsif args.count == 1 node = args[0] path = '.'+node.name raise NoPathGiven.new("#{node} has no path.") if path.nil? else raise ArgumentError.new "Wrong number of arguments #{args.count} for 1 or 2" end leaf = LEAF.match path if leaf # node found, lets insert it add_child(leaf[:name], node) else matches = RELATIVE.match path if matches name = matches[:first].to_s # node not found, what to do? add_child(name, NoDoc.new(name)) if self[name].nil? # everything fixed, continue with rest self[name].add_node(matches[:rest], node) else # has to be an absolute path or something totally strange raise WrongPath.new(path) unless ABSOLUTE.match path # begin at top, if absolute Dom.add_node '.' + path, node end end end |