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'.freeze
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.



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



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

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



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

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



129
130
131
132
# File 'lib/rubyXL/objects/relationships.rb', line 129

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



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

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



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

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

#find_by_target(target) ⇒ Object



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

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


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

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
      if !RubyXL.class_variable_get(:@@suppress_warnings) then
        puts "*** WARNING: storage class not found for #{rel.target} (#{rel.type})"
      end

      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



111
112
113
# File 'lib/rubyXL/objects/relationships.rb', line 111

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