Class: OpenXml::Parts::Rels

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

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.



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

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



10
11
12
13
14
15
16
17
# File 'lib/openxml/parts/rels.rb', line 10

def self.parse(xml)
  document = Nokogiri::XML(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 = next_id, target_mode = nil) ⇒ Object



26
27
28
29
30
# File 'lib/openxml/parts/rels.rb', line 26

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

#each(&block) ⇒ Object



36
37
38
# File 'lib/openxml/parts/rels.rb', line 36

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

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/openxml/parts/rels.rb', line 40

def empty?
  relationships.empty?
end

#push(relationship) ⇒ Object



32
33
34
# File 'lib/openxml/parts/rels.rb', line 32

def push(relationship)
  relationships.push relationship
end

#to_xmlObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/openxml/parts/rels.rb', line 44

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