Module: RubyXL::RelationshipSupport

Overview

RelationshipSupport module enables automatic loading and saving of _rels for the respective file

Defined Under Namespace

Modules: ClassMehods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#generic_storageObject

Returns the value of attribute generic_storage.



152
153
154
# File 'lib/rubyXL/objects/relationships.rb', line 152

def generic_storage
  @generic_storage
end

#relationship_containerObject

Returns the value of attribute relationship_container.



152
153
154
# File 'lib/rubyXL/objects/relationships.rb', line 152

def relationship_container
  @relationship_container
end

Class Method Details

.included(klass) ⇒ Object



147
148
149
150
# File 'lib/rubyXL/objects/relationships.rb', line 147

def self.included(klass)
  klass.class_variable_set(:@@ooxml_relationships, {})
  klass.extend RubyXL::RelationshipSupport::ClassMehods
end

Instance Method Details

#attach_relationship(rid, rf) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rubyXL/objects/relationships.rb', line 187

def attach_relationship(rid, rf)
  relationships = self.class.class_variable_get(:@@ooxml_relationships)
  klass = rf.class
  if relationships.has_key?(klass) then
    accessor = relationships[klass]
    case accessor
    when NilClass then
      # Relationship is known, but we don't have a special accessor for it, store as generic
      store_relationship(rf)
    when false then
      # Do nothing, the code will perform attaching on its own
    else
      container = self.send(accessor)
      if container.is_a?(Array) then container << rf
      else self.send("#{accessor}=", rf)
      end
    end
  else store_relationship(rf, :unknown)
  end
end


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/rubyXL/objects/relationships.rb', line 158

def collect_related_objects
  res = related_objects.compact # Avoid tainting +related_objects+ array
  res.concat(generic_storage) if generic_storage

  if relationship_container then
    relationship_container.owner = self
    res << relationship_container
  end

  related = []

  res.each { |obj|
    next if obj.respond_to?(:empty?) && obj.empty?
    related << obj
    related.concat(obj.collect_related_objects) if obj.respond_to?(:collect_related_objects)
  }

  related
end

#load_relationships(dir_path, base_file_name) ⇒ Object



178
179
180
181
182
183
184
185
# File 'lib/rubyXL/objects/relationships.rb', line 178

def load_relationships(dir_path, base_file_name)
  self.relationship_container = RubyXL::OOXMLRelationshipsFile.load_relationship_file(dir_path, base_file_name)
  return if relationship_container.nil?

  relationship_container.load_related_files(dir_path, base_file_name).each_pair { |rid, related_file|
    attach_relationship(rid, related_file) if related_file
  }
end

Override this method



154
155
156
# File 'lib/rubyXL/objects/relationships.rb', line 154

def related_objects # Override this method
  []
end

#store_relationship(related_file, unknown = false) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/rubyXL/objects/relationships.rb', line 208

def store_relationship(related_file, unknown = false)
  self.generic_storage ||= []
  if unknown && !RubyXL.class_variable_get(:@@suppress_warnings) then
    puts "WARNING: #{self.class} is not aware how to process #{related_file.class}"
  end
  self.generic_storage << related_file
end