Class: OpenXmlPackage
- Inherits:
-
Object
- Object
- OpenXmlPackage
- Defined in:
- lib/open_xml_package.rb,
lib/open_xml_package/part.rb,
lib/open_xml_package/version.rb
Defined Under Namespace
Classes: Part
Constant Summary collapse
- VERSION =
"0.0.3"
Instance Attribute Summary collapse
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
Class Method Summary collapse
Instance Method Summary collapse
- #add_part(path_or_part, content = nil) ⇒ Object
- #close ⇒ Object
- #get_part(path) ⇒ Object
-
#initialize(zipfile = nil) ⇒ OpenXmlPackage
constructor
A new instance of OpenXmlPackage.
- #to_stream ⇒ Object
- #write_to(path) ⇒ Object
Constructor Details
#initialize(zipfile = nil) ⇒ OpenXmlPackage
Returns a new instance of OpenXmlPackage.
36 37 38 39 40 |
# File 'lib/open_xml_package.rb', line 36 def initialize(zipfile=nil) @zipfile = zipfile @parts = [] read_zipfile! if zipfile end |
Instance Attribute Details
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
8 9 10 |
# File 'lib/open_xml_package.rb', line 8 def parts @parts end |
Class Method Details
.from_stream(stream) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/open_xml_package.rb', line 20 def self.from_stream(stream) stream = StringIO.new(stream) if stream.is_a?(String) # Hack: Zip::Entry.read_c_dir_entry initializes # a new Zip::Entry by calling `io.path`. Zip::Entry # uses this to open the original zipfile; but in # this case, the StringIO _is_ the original. def stream.path self end zipfile = ::Zip::File.new("", true, true) zipfile.read_from_stream(stream) new(zipfile) end |
.open(path) ⇒ Object
10 11 12 13 14 15 16 17 18 |
# File 'lib/open_xml_package.rb', line 10 def self.open(path) if block_given? Zip::File.open(path) do |zipfile| yield new(zipfile) end else new Zip::File.open(path) end end |
Instance Method Details
#add_part(path_or_part, content = nil) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/open_xml_package.rb', line 44 def add_part(path_or_part, content=nil) path = path_or_part path = path_or_part.path if path_or_part.respond_to?(:path) content = path_or_part.read if path_or_part.respond_to?(:read) @parts << Part.new(path, content) end |
#close ⇒ Object
58 59 60 |
# File 'lib/open_xml_package.rb', line 58 def close zipfile.close if zipfile end |
#get_part(path) ⇒ Object
52 53 54 |
# File 'lib/open_xml_package.rb', line 52 def get_part(path) @parts.detect { |part| part.path == path } end |
#to_stream ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/open_xml_package.rb', line 68 def to_stream Zip::OutputStream.write_buffer do |io| parts.each do |part| io.put_next_entry part.path io.write part.content end end end |
#write_to(path) ⇒ Object
62 63 64 65 66 |
# File 'lib/open_xml_package.rb', line 62 def write_to(path) File.open(path, "w") do |file| file.write to_stream.string end end |