Class | REXML::Parent |
In: |
lib/rexml/parent.rb
|
Parent: | Child |
Constructor @param parent if supplied, will be set as the parent of this object
# File lib/rexml/parent.rb, line 12 12: def initialize parent=nil 13: super(parent) 14: @children = [] 15: end
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)
# File lib/rexml/parent.rb, line 70 70: def []=( *args ) 71: args[-1].parent = self 72: @children[*args[0..-2]] = args[-1] 73: end
# File lib/rexml/parent.rb, line 17 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
Deeply clones this object. This creates a complete duplicate of this Parent, including all descendants.
# File lib/rexml/parent.rb, line 148 148: def deep_clone 149: cl = clone() 150: each do |child| 151: if child.kind_of? Parent 152: cl << child.deep_clone 153: else 154: cl << child.clone 155: end 156: end 157: cl 158: end
# File lib/rexml/parent.rb, line 33 33: def delete( object ) 34: found = false 35: @children.delete_if {|c| c.equal?(object) and found = true } 36: object.parent = nil if found 37: end
# File lib/rexml/parent.rb, line 47 47: def delete_at( index ) 48: @children.delete_at index 49: end
# File lib/rexml/parent.rb, line 43 43: def delete_if( &block ) 44: @children.delete_if(&block) 45: end
# File lib/rexml/parent.rb, line 51 51: def each_index( &block ) 52: @children.each_index(&block) 53: 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.
# File lib/rexml/parent.rb, line 123 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)
# File lib/rexml/parent.rb, line 102 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)
# File lib/rexml/parent.rb, line 82 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)
# File lib/rexml/parent.rb, line 140 140: def replace_child( to_replace, replacement ) 141: @children.map! {|c| c.equal?( to_replace ) ? replacement : c } 142: to_replace.parent = nil 143: replacement.parent = self 144: end