Class: OpenXml::TemplateDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/open_xml/template_document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TemplateDocument

Returns a new instance of TemplateDocument.



10
11
12
13
14
15
# File 'lib/open_xml/template_document.rb', line 10

def initialize(options)
  @template_path = options[:path]
  @parts = {}
  @data = options[:data]
  split_parts
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/open_xml/template_document.rb', line 8

def data
  @data
end

#partsObject (readonly)

Returns the value of attribute parts.



8
9
10
# File 'lib/open_xml/template_document.rb', line 8

def parts
  @parts
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



8
9
10
# File 'lib/open_xml/template_document.rb', line 8

def template_path
  @template_path
end

Instance Method Details

#processObject



26
27
28
29
30
31
32
33
34
35
# File 'lib/open_xml/template_document.rb', line 26

def process
  doc = Nokogiri::XML(parts["word/document.xml"])
  doc.xpath("//w:t").each do |node|
    data.each do |k, v|
      node.content = node.content.gsub(k, Array(v).join("\n")) if node.content[/#{k}/]
    end
  end

  parts["word/document.xml"] = doc.to_xml
end

#to_zip_bufferObject



17
18
19
20
21
22
23
24
# File 'lib/open_xml/template_document.rb', line 17

def to_zip_buffer
  Zip::OutputStream.write_buffer do |w|
    parts.each do |k, v|
      w.put_next_entry k
      w.write v
    end
  end
end