Skip to content

Commit 589f69a

Browse files
committed
Add tempfile support
1 parent 45dd37f commit 589f69a

2 files changed

Lines changed: 57 additions & 57 deletions

File tree

lib/docx/document.rb

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
1-
require 'docx/parser'
2-
require 'zip/zip'
3-
4-
module Docx
5-
class Document
6-
delegate :paragraphs, :bookmarks, :to => :@parser
7-
delegate :doc, :xml, :zip, :to => :@parser
8-
def initialize(path, &block)
9-
@replace = {}
10-
if block_given?
11-
@parser = Parser.new(File.expand_path(path), &block)
12-
else
13-
@parser = Parser.new(File.expand_path(path))
14-
end
15-
end
16-
17-
def self.open(path, &block)
18-
self.new(path, &block)
19-
end
20-
21-
def each_paragraph
22-
paragraphs.each { |p| yield(p) }
23-
end
24-
25-
def to_s
26-
paragraphs.map(&:to_s).join("\n")
27-
end
28-
29-
# TODO: Flesh this out to be compatible with other files
30-
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
31-
# end of methods that make edits?
32-
def update
33-
@replace["word/document.xml"] = doc.serialize :save_with => 0
34-
end
35-
36-
def save(path)
37-
update
38-
Zip::ZipFile.open(path, Zip::ZipFile::CREATE) do |out|
39-
zip.each do |entry|
40-
out.get_output_stream(entry.name) do |o|
41-
if @replace[entry.name]
42-
o.write(@replace[entry.name])
43-
else
44-
o.write(zip.read(entry.name))
45-
end
46-
end
47-
end
48-
end
49-
zip.close
50-
end
51-
52-
alias_method :text, :to_s
53-
end
54-
end
1+
require 'docx/parser'
2+
require 'zip/zip'
3+
4+
module Docx
5+
class Document
6+
delegate :paragraphs, :bookmarks, :to => :@parser
7+
delegate :doc, :xml, :zip, :to => :@parser
8+
def initialize(path, &block)
9+
@replace = {}
10+
if block_given?
11+
@parser = Parser.new(File.expand_path(path), &block)
12+
else
13+
@parser = Parser.new(File.expand_path(path))
14+
end
15+
end
16+
17+
def self.open(path, &block)
18+
self.new(path, &block)
19+
end
20+
21+
def each_paragraph
22+
paragraphs.each { |p| yield(p) }
23+
end
24+
25+
def to_s
26+
paragraphs.map(&:to_s).join("\n")
27+
end
28+
29+
# TODO: Flesh this out to be compatible with other files
30+
# TODO: Method to set flag on files that have been edited, probably by inserting something at the
31+
# end of methods that make edits?
32+
def update
33+
@replace["word/document.xml"] = doc.serialize :save_with => 0
34+
end
35+
36+
def save(path)
37+
update
38+
Zip::ZipOutputStream.open(path) do |out|
39+
zip.each do |entry|
40+
out.put_next_entry(entry.name)
41+
42+
if @replace[entry.name]
43+
out.write(@replace[entry.name])
44+
else
45+
out.write(zip.read(entry.name))
46+
end
47+
end
48+
end
49+
zip.close
50+
end
51+
52+
alias_method :text, :to_s
53+
end
54+
end

test/docx/test_saving.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ def test_saving_to_tempfile
2323

2424
temp_file.close
2525
temp_file.unlink
26-
# ensure temp file is gone
27-
assert equal(nil, File.exists?(@new_doc_path))
26+
# ensure temp file has been removed
27+
assert_equal(false, File.exists?(@new_doc_path))
2828
end
2929

3030
def teardown
31-
if @new_doc and @new_doc_path
31+
if File.exists?(@new_doc_path)
3232
File.delete(@new_doc_path)
3333
end
3434
end

0 commit comments

Comments
 (0)