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.



52
53
54
55
56
57
58
# File 'lib/openxml/package.rb', line 52

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.



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

def content_types
  @content_types
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#relsObject (readonly)

Returns the value of attribute rels.



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

def rels
  @rels
end

Class Method Details

.content_types(&block) ⇒ Object



19
20
21
# File 'lib/openxml/package.rb', line 19

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

.content_types_presetsObject



15
16
17
# File 'lib/openxml/package.rb', line 15

def content_types_presets
  @content_types_presets ||= OpenXml::ContentTypesPresets.new
end

.from_stream(stream) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/openxml/package.rb', line 33

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



23
24
25
26
27
28
29
30
31
# File 'lib/openxml/package.rb', line 23

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



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

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

#closeObject



77
78
79
# File 'lib/openxml/package.rb', line 77

def close
  zipfile.close if zipfile
end

#get_part(path) ⇒ Object



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

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

#to_streamObject



88
89
90
91
92
93
94
95
# File 'lib/openxml/package.rb', line 88

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



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

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



81
82
83
84
85
# File 'lib/openxml/package.rb', line 81

def write_to(path)
  File.open(path, "wb") do |file|
    file.write to_stream.string
  end
end