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

#==, 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



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



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

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



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

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



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

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

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

#xlsx_pathObject



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

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