File: rexml/child.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: REXML#3
  class: Child#8
includes
  Node ( REXML )
inherits from
  Object ( Builtin-Module )
has properties
attribute: parent [R] #10
method: initialize / 1 #17
method: replace_with / 1 #28
method: remove #36
method: parent= / 1 #51
alias: next_sibling next_sibling_node #57
alias: previous_sibling previous_sibling_node #58
method: next_sibling= / 1 #67
method: previous_sibling= / 1 #78
method: document #84
method: bytes #90

Class Hierarchy

Object ( Builtin-Module )
  Child ( REXML ) #8

Code

   1  require "rexml/node"
   2 
   3  module 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
  96  end