Class: DocxTemplate::Docx
- Inherits:
-
Object
- Object
- DocxTemplate::Docx
- Defined in:
- lib/docx_template/docx.rb
Defined Under Namespace
Classes: EntityReplacer
Instance Attribute Summary collapse
-
#dest_path ⇒ Object
readonly
Returns the value of attribute dest_path.
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#header_replacer_list ⇒ Object
readonly
Returns the value of attribute header_replacer_list.
-
#image_replacer_list ⇒ Object
readonly
Returns the value of attribute image_replacer_list.
-
#text_replacer_list ⇒ Object
readonly
Returns the value of attribute text_replacer_list.
Instance Method Summary collapse
-
#initialize(file_path) ⇒ Docx
constructor
A new instance of Docx.
- #replace_header(src_text, dest_text, multiple_occurances = false) ⇒ Object
- #replace_image(src_image_file_name, dest_image_file) ⇒ Object
- #replace_text(src_text, dest_text, multiple_occurances = false) ⇒ Object
- #save(dest_path = Dir.mktmpdir) ⇒ Object
Constructor Details
#initialize(file_path) ⇒ Docx
Returns a new instance of Docx.
12 13 14 15 16 17 |
# File 'lib/docx_template/docx.rb', line 12 def initialize(file_path) @file_path = file_path @text_replacer_list = [] @image_replacer_list = [] @header_replacer_list = [] end |
Instance Attribute Details
#dest_path ⇒ Object (readonly)
Returns the value of attribute dest_path.
6 7 8 |
# File 'lib/docx_template/docx.rb', line 6 def dest_path @dest_path end |
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
6 7 8 |
# File 'lib/docx_template/docx.rb', line 6 def file_path @file_path end |
#header_replacer_list ⇒ Object (readonly)
Returns the value of attribute header_replacer_list.
6 7 8 |
# File 'lib/docx_template/docx.rb', line 6 def header_replacer_list @header_replacer_list end |
#image_replacer_list ⇒ Object (readonly)
Returns the value of attribute image_replacer_list.
6 7 8 |
# File 'lib/docx_template/docx.rb', line 6 def image_replacer_list @image_replacer_list end |
#text_replacer_list ⇒ Object (readonly)
Returns the value of attribute text_replacer_list.
6 7 8 |
# File 'lib/docx_template/docx.rb', line 6 def text_replacer_list @text_replacer_list end |
Instance Method Details
#replace_header(src_text, dest_text, multiple_occurances = false) ⇒ Object
28 29 30 |
# File 'lib/docx_template/docx.rb', line 28 def replace_header(src_text, dest_text, multiple_occurances=false) @header_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances) end |
#replace_image(src_image_file_name, dest_image_file) ⇒ Object
23 24 25 26 |
# File 'lib/docx_template/docx.rb', line 23 def replace_image(src_image_file_name, dest_image_file) replacer = EntityReplacer.new(src_image_file_name, dest_image_file, false) @image_replacer_list << replacer end |
#replace_text(src_text, dest_text, multiple_occurances = false) ⇒ Object
19 20 21 |
# File 'lib/docx_template/docx.rb', line 19 def replace_text(src_text, dest_text, multiple_occurances=false) @text_replacer_list << EntityReplacer.new(src_text, dest_text, multiple_occurances) end |
#save(dest_path = Dir.mktmpdir) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/docx_template/docx.rb', line 32 def save(dest_path=Dir.mktmpdir) @dest_path = dest_path buffer = nil Zip::File.open(@file_path) do |zip_file| buffer = Zip::OutputStream.write_buffer do |out| exclusion_files_list = derive_exclusion_file_list prepare_rest_of_archive(zip_file, out, exclusion_files_list) # text part unless @text_replacer_list.empty? replace_content(out, DOCUMENT_FILE_PATH, zip_file, @text_replacer_list) end # image part @image_replacer_list.each do |replacer| next unless File.exist?(replacer.dest_entity) # silently skip non-existent files out.put_next_entry("#{IMAGES_DIR_PATH}/#{replacer.src_entity}") out.write File.read(replacer.dest_entity) end # header part unless @header_replacer_list.empty? replace_content(out, HEADER_FILE_PATH, zip_file, @header_replacer_list) end end end File.open(dest_path, "w") {|f| f.write(buffer.string) } end |