Class: OpenxmlDocxTemplater::Template
- Inherits:
-
Object
- Object
- OpenxmlDocxTemplater::Template
- Defined in:
- lib/openxml_docx_templater/template.rb
Instance Attribute Summary collapse
-
#template ⇒ Object
Returns the value of attribute template.
Instance Method Summary collapse
-
#initialize(template, output) ⇒ Template
constructor
A new instance of Template.
- #process(context) ⇒ Object
Constructor Details
#initialize(template, output) ⇒ Template
Returns a new instance of Template.
10 11 12 13 |
# File 'lib/openxml_docx_templater/template.rb', line 10 def initialize(template, output) FileUtils.cp(template, output) @template = output end |
Instance Attribute Details
#template ⇒ Object
Returns the value of attribute template.
8 9 10 |
# File 'lib/openxml_docx_templater/template.rb', line 8 def template @template end |
Instance Method Details
#process(context) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/openxml_docx_templater/template.rb', line 15 def process(context) tmpfiles = [] Zip::File.open(@template) do |zipfile| zipfile.entries.select { |entry| entry.name[/\.xml$/] }.each do |xml_file| content = zipfile.read(xml_file).refact.force_encoding('utf-8') docxeruby = DocxEruby.new(XmlReader.new(content)) out = docxeruby.evaluate(context) tmpfiles << (file = Tempfile.new('openxml_template')) file << out file.close zipfile.replace(xml_file, file.path) end end end |