PageRenderTime 86ms CodeModel.GetById 76ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rexml/child.rb

http://github.com/agross/netopenspace
Ruby | 96 lines | 42 code | 11 blank | 43 comment | 4 complexity | 2e3d90a5d29918e49cf3ea680c60315a MD5 | raw file
 1require "rexml/node"
 2
 3module REXML
 4	##
 5	# A Child object is something contained by a parent, and this class
 6	# contains methods to support that.  Most user code will not use this
 7	# class directly.
 8	class Child
 9		include Node
10		attr_reader :parent		# The Parent of this object
11
12		# Constructor.  Any inheritors of this class should call super to make
13		# sure this method is called.
14		# parent::
15		#   if supplied, the parent of this child will be set to the
16		#   supplied value, and self will be added to the parent
17		def initialize( parent = nil )
18			@parent = nil  
19			# Declare @parent, but don't define it.  The next line sets the 
20			# parent.
21			parent.add( self ) if parent
22		end
23
24		# Replaces this object with another object.  Basically, calls
25		# Parent.replace_child
26		#
27		# Returns:: self
28		def replace_with( child )
29			@parent.replace_child( self, child )
30			self
31		end
32
33		# Removes this child from the parent.
34		#
35		# Returns:: self
36		def remove
37			unless @parent.nil?
38				@parent.delete self
39			end
40			self
41		end
42
43		# Sets the parent of this child to the supplied argument.
44		#
45		# other::
46		#   Must be a Parent object.  If this object is the same object as the
47		#   existing parent of this child, no action is taken. Otherwise, this
48		#   child is removed from the current parent (if one exists), and is added
49		#   to the new parent.
50		# Returns:: The parent added
51		def parent=( other )
52			return @parent if @parent == other
53			@parent.delete self if defined? @parent and @parent
54			@parent = other
55		end
56
57		alias :next_sibling :next_sibling_node
58		alias :previous_sibling :previous_sibling_node
59
60		# Sets the next sibling of this child.  This can be used to insert a child
61		# after some other child.
62		#  a = Element.new("a")
63		#  b = a.add_element("b")
64		#  c = Element.new("c")
65		#  b.next_sibling = c
66		#  # => <a><b/><c/></a>
67		def next_sibling=( other )
68		  parent.insert_after self, other
69		end
70
71		# Sets the previous sibling of this child.  This can be used to insert a 
72		# child before some other child.
73		#  a = Element.new("a")
74		#  b = a.add_element("b")
75		#  c = Element.new("c")
76		#  b.previous_sibling = c
77		#  # => <a><b/><c/></a>
78		def previous_sibling=(other)
79		  parent.insert_before self, other
80		end
81
82		# Returns:: the document this child belongs to, or nil if this child
83		# belongs to no document
84		def document
85			return parent.document unless parent.nil?
86			nil
87		end
88
89		# This doesn't yet handle encodings
90		def bytes
91			encoding = document.encoding
92
93			to_s
94		end
95	end
96end