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_indent =
($DEBUG ? 0 : nil)

Constants inherited from OOXMLTopLevelObject

RubyXL::OOXMLTopLevelObject::ROOT

Instance Attribute Summary collapse

Attributes inherited from OOXMLTopLevelObject

#root

Attributes included from OOXMLObjectInstanceMethods

#local_namespaces

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

#==, included, #index_in_collection, #initialize, #write_xml

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



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

def owner
  @owner
end

Returns the value of attribute related_files.



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

def related_files
  @related_files
end

Class Method Details

.get_class_by_rel_type(rel_type) ⇒ Object



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

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



101
102
103
104
105
106
107
# File 'lib/rubyXL/objects/relationships.rb', line 101

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

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

  parse_file(zipdir_path, rel_file_path)
end

.rel_file_path(base_file_path) ⇒ Object



127
128
129
130
# File 'lib/rubyXL/objects/relationships.rb', line 127

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



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rubyXL/objects/relationships.rb', line 113

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



46
47
48
# File 'lib/rubyXL/objects/relationships.rb', line 46

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

#find_by_target(target) ⇒ Object



50
51
52
53
54
# File 'lib/rubyXL/objects/relationships.rb', line 50

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


71
72
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
# File 'lib/rubyXL/objects/relationships.rb', line 71

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

  @@debug_indent += 2 if @@debug_indent

  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_indent}Loading #{klass} (#{rel.id}): #{file_path}" if @@debug_indent

    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_indent -=2 if @@debug_indent

  related_files
end

#xlsx_pathObject



109
110
111
# File 'lib/rubyXL/objects/relationships.rb', line 109

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