Class: DocxTools::MailMerge
- Inherits:
-
Object
- Object
- DocxTools::MailMerge
- Defined in:
- lib/docx_tools/mail_merge.rb
Constant Summary collapse
- REGEXP =
/ MERGEFIELD "?([^ ]+?)"? (| \\\* MERGEFORMAT )/i.freeze
Instance Attribute Summary collapse
-
#document ⇒ Object
Returns the value of attribute document.
-
#part_list ⇒ Object
Returns the value of attribute part_list.
Instance Method Summary collapse
- #fields ⇒ Object
-
#initialize(file_object) ⇒ MailMerge
constructor
A new instance of MailMerge.
- #merge(replacements = {}) ⇒ Object
- #write(filename) ⇒ Object
Constructor Details
Instance Attribute Details
#document ⇒ Object
Returns the value of attribute document.
5 6 7 |
# File 'lib/docx_tools/mail_merge.rb', line 5 def document @document end |
#part_list ⇒ Object
Returns the value of attribute part_list.
5 6 7 |
# File 'lib/docx_tools/mail_merge.rb', line 5 def part_list @part_list end |
Instance Method Details
#fields ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/docx_tools/mail_merge.rb', line 13 def fields fields = Set.new part_list.each_part do |part| part.xpath('.//w:MergeField').each do |mf| fields.add(mf.content) end end fields.to_a end |
#merge(replacements = {}) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/docx_tools/mail_merge.rb', line 23 def merge(replacements = {}) part_list.each_part do |part| replacements.each do |field, text| merge_field(part, field, text) end end end |
#write(filename) ⇒ Object
31 32 33 34 35 |
# File 'lib/docx_tools/mail_merge.rb', line 31 def write(filename) File.open(filename, 'w') do |file| file.write(generate.string) end end |