Class: OpenXmlPackage

Inherits:
Object
  • Object
show all
Defined in:
lib/open_xml_package.rb,
lib/open_xml_package/part.rb,
lib/open_xml_package/version.rb

Defined Under Namespace

Classes: Part

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(zipfile = nil) ⇒ OpenXmlPackage

Returns a new instance of OpenXmlPackage.



19
20
21
22
23
# File 'lib/open_xml_package.rb', line 19

def initialize(zipfile=nil)
  @zipfile = zipfile
  @parts = []
  read_zipfile! if zipfile
end

Instance Attribute Details

#partsObject (readonly)

Returns the value of attribute parts.



7
8
9
# File 'lib/open_xml_package.rb', line 7

def parts
  @parts
end

Class Method Details

.open(path) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/open_xml_package.rb', line 9

def self.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_or_part, content = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/open_xml_package.rb', line 27

def add_part(path_or_part, content=nil)
  path = path_or_part
  path = path_or_part.path if path_or_part.respond_to?(:path)
  content = path_or_part.read if path_or_part.respond_to?(:read)
  
  @parts << Part.new(path, content)
end

#closeObject



41
42
43
# File 'lib/open_xml_package.rb', line 41

def close
  zipfile.close if zipfile
end

#get_part(path) ⇒ Object



35
36
37
# File 'lib/open_xml_package.rb', line 35

def get_part(path)
  @parts.detect { |part| part.path == path }
end

#to_streamObject



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

def to_stream
  Zip::OutputStream.write_buffer do |io|
    parts.each do |part|
      io.put_next_entry part.path
      io.write part.content
    end
  end
end

#write_to(path) ⇒ Object



45
46
47
48
49
# File 'lib/open_xml_package.rb', line 45

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