Class: OpenXml::Parts::Rels

Inherits:
OpenXml::Part show all
Includes:
Enumerable
Defined in:
lib/open_xml/parts/rels.rb

Defined Under Namespace

Classes: Relationship

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OpenXml::Part

#build_standalone_xml, #build_xml, #read

Constructor Details

#initialize(defaults = []) ⇒ Rels

Returns a new instance of Rels.



17
18
19
20
21
22
# File 'lib/open_xml/parts/rels.rb', line 17

def initialize(defaults=[])
  @relationships = []
  Array(defaults).each do |default|
    add_relationship(*default.values_at("Type", "Target", "Id"))
  end
end

Class Method Details

.parse(xml) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/open_xml/parts/rels.rb', line 8

def self.parse(xml)
  document = Nokogiri(xml)
  self.new.tap do |part|
    document.css("Relationship").each do |rel|
      part.add_relationship rel["Type"], rel["Target"], rel["Id"]
    end
  end
end

Instance Method Details

#add_relationship(type, target, id = nil) ⇒ Object



24
25
26
27
28
# File 'lib/open_xml/parts/rels.rb', line 24

def add_relationship(type, target, id=nil)
  Relationship.new(type, target, id).tap do |relationship|
    relationships.push relationship
  end
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/open_xml/parts/rels.rb', line 30

def each(&block)
  relationships.each(&block)
end

#to_xmlObject



34
35
36
37
38
39
40
41
42
# File 'lib/open_xml/parts/rels.rb', line 34

def to_xml
  build_standalone_xml do |xml|
    xml.Relationships(xmlns: "http://schemas.openxmlformats.org/package/2006/relationships") do
      relationships.each do |rel|
        xml.Relationship("Id" => rel.id, "Type" => rel.type, "Target" => rel.target)
      end
    end
  end
end