Class: RubyXL::OOXMLRelationshipsFile

Inherits:
OOXMLTopLevelObject show all
Defined in:
lib/rubyXL/objects/relationships.rb

Constant Summary collapse

CONTENT_TYPE =
'application/vnd.openxmlformats-package.relationships+xml'
SAVE_ORDER =
100
@@debug =

Change to 0 to enable debug output

nil

Constants inherited from OOXMLTopLevelObject

RubyXL::OOXMLTopLevelObject::ROOT

Instance Attribute Summary collapse

Attributes inherited from OOXMLTopLevelObject

#root

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from OOXMLTopLevelObject

#add_to_zip, #file_index, parse_file, set_namespaces

Methods included from OOXMLObjectInstanceMethods

#dup, included, #index_in_collection, #initialize, #write_xml

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



25
26
27
# File 'lib/rubyXL/objects/relationships.rb', line 25

def owner
  @owner
end

Returns the value of attribute related_files.



25
26
27
# File 'lib/rubyXL/objects/relationships.rb', line 25

def related_files
  @related_files
end

Class Method Details

.get_class_by_rel_type(rel_type) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rubyXL/objects/relationships.rb', line 58

def self.get_class_by_rel_type(rel_type)
  unless defined?(@@rel_hash)
    @@rel_hash = {}
    RubyXL.constants.each { |c|
      klass = RubyXL.const_get(c)

      if klass.is_a?(Class) && klass.const_defined?(:REL_TYPE) then
        @@rel_hash[klass::REL_TYPE] = klass
      end
    }
  end

  @@rel_hash[rel_type]
end

.load_relationship_file(zipdir_path, base_file_path) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/rubyXL/objects/relationships.rb', line 106

def self.load_relationship_file(zipdir_path, base_file_path)
  rel_file_path = rel_file_path(base_file_path)

  puts "--> DEBUG:  #{'  ' * @@debug}Loading .rel file: #{rel_file_path}" if @@debug

  parse_file(zipdir_path, rel_file_path)
end

.rel_file_path(base_file_path) ⇒ Object



132
133
134
135
# File 'lib/rubyXL/objects/relationships.rb', line 132

def self.rel_file_path(base_file_path)
  basename = base_file_path.root? ? '' : base_file_path.basename
  base_file_path.dirname.join('_rels', "#{basename}.rels").cleanpath
end

Instance Method Details

#before_write_xmlObject



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rubyXL/objects/relationships.rb', line 118

def before_write_xml
  case owner
  when RubyXL::WorkbookRoot, RubyXL::Workbook then
    # Fully implemented objects with no generic (unhandled) relationships -
    #   (re)generating relationships from scratch.
    related_objects = owner.related_objects
    related_objects += owner.generic_storage if owner.generic_storage

    self.relationships = []
    related_objects.compact.each { |f| add_relationship(f) }
  end
  super
end

#find_by_rid(r_id) ⇒ Object



48
49
50
# File 'lib/rubyXL/objects/relationships.rb', line 48

def find_by_rid(r_id)
  relationships.find { |r| r.id == r_id }
end

#find_by_target(target) ⇒ Object



52
53
54
55
56
# File 'lib/rubyXL/objects/relationships.rb', line 52

def find_by_target(target)
  relationships.find { |r|
    (r.target == target) || (r.target == target.relative_path_from(owner.xlsx_path.dirname))
  }
end


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubyXL/objects/relationships.rb', line 73

def load_related_files(zipdir_path, base_file_name)
  self.related_files = {}

  @@debug +=2 if @@debug

  self.relationships.each { |rel|
    next if rel.target_mode == 'External'

    file_path = Pathname.new(rel.target)

    if !file_path.absolute? then
      file_path = (base_file_name.dirname + file_path).cleanpath
    end

    klass = RubyXL::OOXMLRelationshipsFile.get_class_by_rel_type(rel.type)

    if klass.nil? then
      puts "*** WARNING: storage class not found for #{rel.target} (#{rel.type})"
      klass = GenericStorageObject
    end

    puts "--> DEBUG:#{'  ' * @@debug}Loading #{klass} (#{rel.id}): #{file_path}" if @@debug

    obj = klass.parse_file(zipdir_path, file_path)
    obj.load_relationships(zipdir_path, file_path) if obj.respond_to?(:load_relationships)
    self.related_files[rel.id] = obj
  }

  @@debug -=2 if @@debug

  related_files
end

#xlsx_pathObject



114
115
116
# File 'lib/rubyXL/objects/relationships.rb', line 114

def xlsx_path
  self.class.rel_file_path(owner.xlsx_path)
end