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.

Methods

<<   []   []=   add   children   deep_clone   delete   delete_at   delete_if   each   each_child   each_index   index   insert_after   insert_before   new   parent?   push   replace_child   size   to_a   unshift  

Included Modules

Public Class methods

Constructor @param parent if supplied, will be set as the parent of this object

[Source]

12:                 def initialize parent=nil
13:                         super(parent)
14:                         @children = []
15:                 end

Public Instance methods

<<( object )

Alias for push

Fetches a child at a given index @param index the Integer index of the child to fetch

[Source]

57:                 def []( index )
58:                         @children[index]
59:                 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)

[Source]

70:                 def []=( *args )
71:                         args[-1].parent = self
72:                         @children[*args[0..-2]] = args[-1]
73:                 end

[Source]

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
children()

Alias for to_a

Deeply clones this object. This creates a complete duplicate of this Parent, including all descendants.

[Source]

147:                 def deep_clone
148:                         cl = clone()
149:                         each do |child|
150:                                 if child.kind_of? Parent
151:                                         cl << child.deep_clone
152:                                 else
153:                                         cl << child.clone
154:                                 end
155:                         end
156:                         cl
157:                 end

[Source]

33:                 def delete( object )
34:                         return unless @children.include? object
35:                         @children.delete object
36:                         object.parent = nil
37:                 end

[Source]

47:                 def delete_at( index )
48:                         @children.delete_at index
49:                 end

[Source]

43:                 def delete_if( &block )
44:                         @children.delete_if(&block)
45:                 end

[Source]

39:                 def each(&block)
40:                         @children.each(&block)
41:                 end
each_child(&block)

Alias for each

[Source]

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.

[Source]

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)

[Source]

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)

[Source]

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

[Source]

161:                 def parent?
162:                         true
163:                 end
push( object )

Alias for add

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)

[Source]

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

@return the number of children of this parent

[Source]

130:                 def size
131:                         @children.size
132:                 end

[Source]

115:                 def to_a
116:                         @children.dup
117:                 end

[Source]

28:                 def unshift( object )
29:                         object.parent = self
30:                         @children.unshift object
31:                 end

Search

Google

Ruby API Docs

Links