class DocxGenerator::Element

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

Public Class Methods

new(name, attributes = {}, content = []) click to toggle source

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

# File lib/docx_generator/element.rb, line 10
def initialize(name, attributes = {}, content = [])
  @name = name
  @attributes = attributes
  @content = content
end

Public Instance Methods

add(element) click to toggle source

Add an XML element in the content of this XML element. @param element The XML element to add.

# File lib/docx_generator/element.rb, line 18
def add(element)
  @content << element
end
generate() click to toggle source

Generate the XML for the element. @return [String] The XML produced for the element.

# File lib/docx_generator/element.rb, line 24
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
Also aliased as: to_s
to_s()
Alias for: generate

Private Instance Methods

generate_attributes() click to toggle source
# File lib/docx_generator/element.rb, line 44
def generate_attributes
  output = ""
  @attributes.each do |name, value|
    output += " #{name}=\"#{value}\""
  end
  output
end