Class: OpenXml::Package
- Inherits:
-
Object
- Object
- OpenXml::Package
- Defined in:
- lib/openxml/package.rb
Instance Attribute Summary collapse
-
#content_types ⇒ Object
readonly
Returns the value of attribute content_types.
-
#parts ⇒ Object
readonly
Returns the value of attribute parts.
-
#rels ⇒ Object
readonly
Returns the value of attribute rels.
Class Method Summary collapse
- .content_types(&block) ⇒ Object
- .content_types_presets ⇒ Object
- .from_stream(stream) ⇒ Object
- .open(path) ⇒ Object
Instance Method Summary collapse
- #add_part(path, part) ⇒ Object
- #close ⇒ Object
- #get_part(path) ⇒ Object
-
#initialize(zipfile = nil) ⇒ Package
constructor
A new instance of Package.
- #to_stream ⇒ Object
- #type_of(path) ⇒ Object
- #write_to(path) ⇒ Object (also: #save)
Constructor Details
#initialize(zipfile = nil) ⇒ Package
Returns a new instance of Package.
53 54 55 56 57 58 59 |
# File 'lib/openxml/package.rb', line 53 def initialize(zipfile=nil) @zipfile = zipfile @parts = {} set_defaults read_zipfile! if zipfile end |
Instance Attribute Details
#content_types ⇒ Object (readonly)
Returns the value of attribute content_types.
11 12 13 |
# File 'lib/openxml/package.rb', line 11 def content_types @content_types end |
#parts ⇒ Object (readonly)
Returns the value of attribute parts.
11 12 13 |
# File 'lib/openxml/package.rb', line 11 def parts @parts end |
#rels ⇒ Object (readonly)
Returns the value of attribute rels.
11 12 13 |
# File 'lib/openxml/package.rb', line 11 def rels @rels end |
Class Method Details
.content_types(&block) ⇒ Object
20 21 22 |
# File 'lib/openxml/package.rb', line 20 def content_types(&block) content_types_presets.instance_eval(&block) end |
.content_types_presets ⇒ Object
16 17 18 |
# File 'lib/openxml/package.rb', line 16 def content_types_presets @content_types_presets ||= OpenXml::ContentTypesPresets.new end |
.from_stream(stream) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/openxml/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 |
Instance Method Details
#add_part(path, part) ⇒ Object
63 64 65 |
# File 'lib/openxml/package.rb', line 63 def add_part(path, part) @parts[path] = part end |
#close ⇒ Object
78 79 80 |
# File 'lib/openxml/package.rb', line 78 def close zipfile.close if zipfile end |
#get_part(path) ⇒ Object
67 68 69 |
# File 'lib/openxml/package.rb', line 67 def get_part(path) @parts.fetch(path) end |
#to_stream ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/openxml/package.rb', line 89 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) ⇒ Object
71 72 73 74 |
# File 'lib/openxml/package.rb', line 71 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) ⇒ Object Also known as: save
82 83 84 85 86 |
# File 'lib/openxml/package.rb', line 82 def write_to(path) File.open(path, "w") do |file| file.write to_stream.string end end |