Sha256: 98f9240eb96b2381d2221e9ad26693f86ece4d9d0b9a33dde773dd275f13e8a2

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

module DocxGenerator

  # Represent an XML element. This class should not be used directly by the users of the library.
  class Element

    # Create a new XML element.
    # @param name [String] The name of the XML element.
    # @param attributes [Hash] The attributes of the XML element.
    # @param content [Array] An array of the children of the XML element (other XML elements).
    def initialize(name, attributes = {}, content = [])
      @name = name
      @attributes = attributes
      @content = content
    end
    
    # Add an XML element in the content of this XML element.
    # @param element[DocxGenerator::Element] The XML element to add.
    def add(element)
      @content << element
    end
    
    # Generate the XML for the element.
    # @return [String] The XML produced for the element.
    def generate
      output = ""
      if @content.length != 0
        output += "<#{@name}#{generate_attributes}>"
        @content.each do |element|
          if element.respond_to?(:generate)
            output += element.generate
          else
            output += element.to_s
          end
        end
        output += "</#{@name}>"
      else
        output += "<#{@name}#{generate_attributes} />"
      end
      output
    end
    alias :to_s :generate
    
    private
      def generate_attributes
        output = ""
        @attributes.each do |name, value|
          output += " #{name}=\"#{value}\""
        end
        output
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
docx_generator-0.1.0 lib/docx_generator/element.rb