Class: OpenXml::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/open_xml/package.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zipfile = nil) ⇒ Package

Returns a new instance of Package.



53
54
55
56
57
58
59
60
61
62
# File 'lib/open_xml/package.rb', line 53

def initialize(zipfile=nil)
  @zipfile = zipfile
  @parts = {}
  
  if zipfile
    read_zipfile!
  else
    set_defaults
  end
end

Instance Attribute Details

#content_typesObject (readonly)

Returns the value of attribute content_types.



11
12
13
# File 'lib/open_xml/package.rb', line 11

def content_types
  @content_types
end

#partsObject (readonly)

Returns the value of attribute parts.



11
12
13
# File 'lib/open_xml/package.rb', line 11

def parts
  @parts
end

#relsObject (readonly)

Returns the value of attribute rels.



11
12
13
# File 'lib/open_xml/package.rb', line 11

def rels
  @rels
end

Class Method Details

.content_types(&block) ⇒ Object



20
21
22
# File 'lib/open_xml/package.rb', line 20

def content_types(&block)
  content_types_presets.instance_eval &block
end

.content_types_presetsObject



16
17
18
# File 'lib/open_xml/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/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

.open(path) ⇒ Object



24
25
26
27
28
29
30
31
32
# 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

Instance Method Details

#add_part(path, part) ⇒ Object



66
67
68
# File 'lib/open_xml/package.rb', line 66

def add_part(path, part)
  @parts[path] = part
end

#closeObject



81
82
83
# File 'lib/open_xml/package.rb', line 81

def close
  zipfile.close if zipfile
end

#get_part(path) ⇒ Object



70
71
72
# File 'lib/open_xml/package.rb', line 70

def get_part(path)
  @parts.fetch(path)
end

#to_streamObject



92
93
94
95
96
97
98
99
# 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) ⇒ Object



74
75
76
77
# 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) ⇒ Object Also known as: save



85
86
87
88
89
# 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