| Class | REXML::Parent |
| In: |
|
| Parent: | Child |
A parent has children, and has methods for accessing them. The Parent class is never encountered except as the superclass for some other object.
Included Modules
Public Class methods
Constructor @param parent if supplied, will be set as the parent of this object
12: def initialize parent=nil 13: super(parent) 14: @children = [] 15: end
Public Instance methods
Set an index entry. See Array.[]= @param index the index of the element to set @param opt either the object to set, or an Integer length @param child if opt is an Integer, this is the child to set @return the parent (self)
70: def []=( *args ) 71: args[-1].parent = self 72: @children[*args[0..-2]] = args[-1] 73: end
17: def add( object ) 18: #puts "PARENT GOTS #{size} CHILDREN" 19: object.parent = self 20: @children << object 21: #puts "PARENT NOW GOTS #{size} CHILDREN" 22: object 23: end
33: def delete( object ) 34: return unless @children.include? object 35: @children.delete object 36: object.parent = nil 37: end
Fetches the index of a given child @param child the child to get the index of @return the index of the child, or nil if the object is not a child of this parent.
123: def index( child ) 124: count = -1 125: @children.find { |i| count += 1 ; i.hash == child.hash } 126: count 127: end
Inserts an child after another child @param child1 this is either an xpath or an Element. If an Element, child2 will be inserted after child1 in the child list of the parent. If an xpath, child2 will be inserted after the first child to match the xpath. @param child2 the child to insert @return the parent (self)
102: def insert_after( child1, child2 ) 103: if child1.kind_of? String 104: child1 = XPath.first( self, child1 ) 105: child1.parent.insert_after child1, child2 106: else 107: ind = index(child1)+1 108: child2.parent.delete(child2) if child2.parent 109: @children[ind,0] = child2 110: child2.parent = self 111: end 112: self 113: end
Inserts an child before another child @param child1 this is either an xpath or an Element. If an Element, child2 will be inserted before child1 in the child list of the parent. If an xpath, child2 will be inserted before the first child to match the xpath. @param child2 the child to insert @return the parent (self)
82: def insert_before( child1, child2 ) 83: if child1.kind_of? String 84: child1 = XPath.first( self, child1 ) 85: child1.parent.insert_before child1, child2 86: else 87: ind = index(child1) 88: child2.parent.delete(child2) if child2.parent 89: @children[ind,0] = child2 90: child2.parent = self 91: end 92: self 93: end
Replaces one child with another, making sure the nodelist is correct @param to_replace the child to replace (must be a Child) @param replacement the child to insert into the nodelist (must be a Child)
138: def replace_child( to_replace, replacement ) 139: ind = @children.index( to_replace ) 140: to_replace.parent = nil 141: @children[ind,0] = replacement 142: replacement.parent = self 143: end