Class REXML::Parent
In: lib/rexml/parent.rb
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   length   new   parent?   push   replace_child   size   to_a   unshift  

Included Modules

Enumerable

Public Class methods

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

[Source]

    # File lib/rexml/parent.rb, line 12
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]

    # File lib/rexml/parent.rb, line 57
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]

    # File lib/rexml/parent.rb, line 70
70:     def []=( *args )
71:       args[-1].parent = self
72:       @children[*args[0..-2]] = args[-1]
73:     end

[Source]

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

Alias for to_a

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

[Source]

     # 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

[Source]

    # 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

[Source]

    # File lib/rexml/parent.rb, line 47
47:     def delete_at( index )
48:       @children.delete_at index
49:     end

[Source]

    # File lib/rexml/parent.rb, line 43
43:     def delete_if( &block )
44:       @children.delete_if(&block)
45:     end

[Source]

    # File lib/rexml/parent.rb, line 39
39:     def each(&block)
40:       @children.each(&block)
41:     end
each_child(&block)

Alias for each

[Source]

    # 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.

[Source]

     # 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)

[Source]

     # 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)

[Source]

    # 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
length()

Alias for size

[Source]

     # File lib/rexml/parent.rb, line 162
162:     def parent?
163:       true
164:     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]

     # 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

@return the number of children of this parent

[Source]

     # File lib/rexml/parent.rb, line 130
130:     def size
131:       @children.size
132:     end

[Source]

     # File lib/rexml/parent.rb, line 115
115:     def to_a
116:       @children.dup
117:     end

[Source]

    # File lib/rexml/parent.rb, line 28
28:     def unshift( object )
29:       object.parent = self
30:       @children.unshift object
31:     end

[Validate]