Class: OpenXml::Parts::Rels

Inherits:
OpenXml::Part show all
Includes:
Enumerable
Defined in:
lib/openxml/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


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

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

Class Method Details

.parse(xml) ⇒ Object


8
9
10
11
12
13
14
15
# File 'lib/openxml/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"], rel["TargetMode"]
    end
  end
end

Instance Method Details

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


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

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

#each(&block) ⇒ Object


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

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

#to_xmlObject


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/openxml/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|
        attributes = { "Id" => rel.id, "Type" => rel.type, "Target" => rel.target }
        attributes["TargetMode"] = rel.target_mode if rel.target_mode
        xml.Relationship(attributes)
      end
    end
  end
end