module #{element.class_name}AsChild
    require "xamplr/indexed-array"
  
    def #{element.attribute_name}_child
      accessed
      @#{element.attribute_name}_child
    end

    def #{element.attribute_name}_child=(v)
      accessed
      @#{element.attribute_name}_child = v
    end

    alias #{element.attribute_name} #{element.attribute_name}_child
    alias #{element.attribute_name}= #{element.attribute_name}_child=
  
    def init_#{element.attribute_name}_as_child
      @#{element.attribute_name}_child = IndexedArray.new
    end
  
    def add_#{element.attribute_name}(#{element.attribute_name})
      accessed
      index = #{element.attribute_name}.get_the_index
      if(nil == index) then
        raise Xampl::XamplException.new("no value for the index '#{element.indexed_by_attr}' of #{element.attribute_name} defined in : " << #{element.attribute_name}.pp_xml)
      end

      existing = @#{element.attribute_name}_child[index]

      return existing if existing == #{element.attribute_name}

			self.remove_#{element.attribute_name}(index) if existing

      @children << #{element.attribute_name}
      @#{element.attribute_name}_child[index] = #{element.attribute_name}

      #{element.attribute_name}.add_parent(self)

      changed
      return #{element.attribute_name}
    end
  
    def new_#{element.attribute_name}(index)
      accessed

      #{element.attribute_name} = nil
|
|if @element.persisted then
|
      #{element.attribute_name} = #{element.class_name}.lookup(index) if Xampl.persister and Xampl.persister.automatic
|
|end
|
      #{element.attribute_name} = #{element.class_name}.new(index) unless #{element.attribute_name}

      yield(#{element.attribute_name}) if block_given?
      return add_#{element.attribute_name}(#{element.attribute_name})
    end

    def ensure_#{element.attribute_name}(index)
      accessed

      #{element.attribute_name} = @#{element.attribute_name}_child[index]
		  return #{element.attribute_name} if #{element.attribute_name} 

|
|if @element.persisted then
|
      #{element.attribute_name} = #{element.class_name}.lookup(index) if Xampl.persister and Xampl.persister.automatic
|
|end
|
      #{element.attribute_name} = #{element.class_name}.new(index) unless #{element.attribute_name}

      yield(#{element.attribute_name}) if block_given?
      return add_#{element.attribute_name}(#{element.attribute_name})
		end

    def remove_#{element.attribute_name}(index)
      accessed
			changed
	    unless String === index or Symbol === index then
        index = index.get_the_index
			end
      #{element.attribute_name} = @#{element.attribute_name}_child.delete(index) if index
      @children.delete(#{element.attribute_name})
    end
  end