Class: DocxGenerator::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_generator/element.rb

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}, content = []) ⇒ Element

Returns a new instance of Element.



3
4
5
6
7
# File 'lib/docx_generator/element.rb', line 3

def initialize(name, attributes = {}, content = [])
  @name = name
  @attributes = attributes
  @content = content
end

Instance Method Details

#add(element) ⇒ Object



9
10
11
# File 'lib/docx_generator/element.rb', line 9

def add(element)
  @content << element
end

#generateObject Also known as: to_s



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/docx_generator/element.rb', line 13

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