class RubyXL::OOXMLRelationshipsFile
def self.get_class_by_rel_type(rel_type)
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
def self.load_relationship_file(zipdir_path, base_file_path)
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
def self.rel_file_path(base_file_path)
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
def add_relationship(obj)
def add_relationship(obj) return if obj.nil? || !defined?(obj.class::REL_TYPE) relationships << RubyXL::Relationship.new(:id => "rId#{relationships.size + 1}", :type => obj.class::REL_TYPE, :target => obj.xlsx_path.relative_path_from(owner.xlsx_path.dirname)) end
def before_write_xml
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
def find_by_rid(r_id)
def find_by_rid(r_id) relationships.find { |r| r.id == r_id } end
def find_by_target(target)
def find_by_target(target) relationships.find { |r| (r.target == target) || (r.target == target.relative_path_from(owner.xlsx_path.dirname)) } end
def load_related_files(zipdir_path, base_file_name)
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) file_path = (base_file_name.dirname + file_path).cleanpath if file_path.relative? 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
def new_relationship(target, type)
def new_relationship(target, type) RubyXL::Relationship.new(:id => "rId#{relationships.size + 1}", :type => type, :target => target) end
def xlsx_path
def xlsx_path self.class.rel_file_path(owner.xlsx_path) end