| Class | Webgen::ProxyNode |
| In: |
lib/webgen/node.rb
|
| Parent: | Object |
Encapsulates a node. This class is needed when a hierarchy of nodes should be created but the original hierarchy should not be destroyed.
| children | [RW] | Array of child proxy nodes. |
| node | [R] | The encapsulated node. |
| parent | [RW] | The parent proxy node. |
Turn the hierarchy of proxy nodes into a flat list.
# File lib/webgen/node.rb, line 478
478: def flatten!
479: result = []
480: while !self.children.empty?
481: result << self.children.shift
482: result.last.parent = self
483: self.children.unshift(*result.last.children)
484: result.last.children = []
485: end
486: self.children = result
487: end
Sort recursively all children of the node using the wrapped nodes. If value is false, no sorting is done at all. If it is true, then the default sort mechanism is used (see Node#<=>). Otherwise value has to be a meta information key on which should be sorted.
# File lib/webgen/node.rb, line 458
458: def sort!(value = true)
459: return self unless value
460:
461: if value.kind_of?(String)
462: self.children.sort! do |a,b|
463: aval, bval = a.node[value].to_s, b.node[value].to_s
464: if aval !~ /\D/ && aval !~ /\D/
465: aval = aval.to_i
466: bval = bval.to_i
467: end
468: aval <=> bval
469: end
470: else
471: self.children.sort! {|a,b| a.node <=> b.node}
472: end
473: self.children.each {|child| child.sort!(value)}
474: self
475: end