Class: DocxReport::ContentFile

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_report/content_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, zip) ⇒ ContentFile

Returns a new instance of ContentFile.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/docx_report/content_file.rb', line 5

def initialize(name, zip)
  @name = name
  @xml = Nokogiri::XML(zip.read(name))
  @rels_name = "#{name.sub '/', '/_rels/'}.rels"
  @new_rels = false
  @rels_xml = Nokogiri::XML(if zip.entries.any? { |r| r.name == @rels_name }
                              zip.read(@rels_name)
                            else
                              new_rels_xml
                            end)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/docx_report/content_file.rb', line 3

def name
  @name
end

#new_relsObject (readonly)

Returns the value of attribute new_rels.



3
4
5
# File 'lib/docx_report/content_file.rb', line 3

def new_rels
  @new_rels
end

#rels_nameObject (readonly)

Returns the value of attribute rels_name.



3
4
5
# File 'lib/docx_report/content_file.rb', line 3

def rels_name
  @rels_name
end

#rels_xmlObject (readonly)

Returns the value of attribute rels_xml.



3
4
5
# File 'lib/docx_report/content_file.rb', line 3

def rels_xml
  @rels_xml
end

#xmlObject (readonly)

Returns the value of attribute xml.



3
4
5
# File 'lib/docx_report/content_file.rb', line 3

def xml
  @xml
end

Instance Method Details

#new_rels_xmlObject



23
24
25
26
27
28
# File 'lib/docx_report/content_file.rb', line 23

def new_rels_xml
  @new_rels = true
  '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Relationships'\
  'xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'\
  '</Relationships>'
end

#new_uniqe_idObject



17
18
19
20
21
# File 'lib/docx_report/content_file.rb', line 17

def new_uniqe_id
  (@rels_xml.xpath('//*[@Id]').map do |e|
    e[:Id][3..-1].to_i if e.name == 'Relationship'
  end.compact.max || 0) + 1
end

#rels_has_items?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/docx_report/content_file.rb', line 30

def rels_has_items?
  @rels_xml.xpath('//*[@Id]').any?
end