Class: OpenXml::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/openxml/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
# 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_typesObject (readonly)

Returns the value of attribute content_types.



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

def content_types
  @content_types
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#relsObject (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_presetsObject



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

.open(path) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/openxml/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



63
64
65
# File 'lib/openxml/package.rb', line 63

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

#closeObject



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_streamObject



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