class OpenXml::Package
Attributes
content_types[R]
parts[R]
rels[R]
zipfile[R]
Public Class Methods
content_types(&block)
click to toggle source
# File lib/open_xml/package.rb, line 20 def content_types(&block) content_types_presets.instance_eval &block end
content_types_presets()
click to toggle source
# File lib/open_xml/package.rb, line 16 def content_types_presets @content_types_presets ||= OpenXml::ContentTypesPresets.new end
from_stream(stream)
click to toggle source
# File lib/open_xml/package.rb, line 34 def 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
new(zipfile=nil)
click to toggle source
# File lib/open_xml/package.rb, line 53 def initialize(zipfile=nil) @zipfile = zipfile @parts = {} if zipfile read_zipfile! else set_defaults end end
open(path) { |new(zipfile)| ... }
click to toggle source
# File lib/open_xml/package.rb, line 24 def open(path) if block_given? Zip::File.open(path) do |zipfile| yield new(zipfile) end else new Zip::File.open(path) end end
Public Instance Methods
add_part(path, part)
click to toggle source
# File lib/open_xml/package.rb, line 66 def add_part(path, part) @parts[path] = part end
close()
click to toggle source
# File lib/open_xml/package.rb, line 81 def close zipfile.close if zipfile end
get_part(path)
click to toggle source
# File lib/open_xml/package.rb, line 70 def get_part(path) @parts.fetch(path) end
to_stream()
click to toggle source
# File lib/open_xml/package.rb, line 92 def to_stream Zip::OutputStream.write_buffer do |io| parts.each do |path, part| io.put_next_entry path io.write part.content end end end
type_of(path)
click to toggle source
# File lib/open_xml/package.rb, line 74 def type_of(path) raise Errors::MissingContentTypesPart, "We haven't yet read [ContentTypes].xml; but are reading #{path.inspect}" unless content_types content_types.of(path) end
write_to(path)
click to toggle source
# File lib/open_xml/package.rb, line 85 def write_to(path) File.open(path, "w") do |file| file.write to_stream.string end end
Also aliased as: save
Protected Instance Methods
part_for(path, content_type, part)
click to toggle source
# File lib/open_xml/package.rb, line 127 def part_for(path, content_type, part) case content_type when Types::RELATIONSHIPS then Parts::Rels.parse(part.content) else part end end
set_defaults()
click to toggle source
# File lib/open_xml/package.rb, line 118 def set_defaults presets = self.class.content_types_presets @content_types = Parts::ContentTypes.new(presets.defaults, presets.overrides) add_part "[Content_Types].xml", content_types @rels = Parts::Rels.new add_part "_rels/.rels", rels end
Private Instance Methods
read_zipfile!()
click to toggle source
# File lib/open_xml/package.rb, line 105 def read_zipfile! zipfile.entries.each do |entry| path, part = entry.name, Parts::UnparsedPart.new(entry) add_part path, case path when "[Content_Types].xml" then @content_types = Parts::ContentTypes.parse(part.content) when "_rels/.rels" then @rels = Parts::Rels.parse(part.content) else part_for(path, type_of(path), part) end end end