Class: RubyXL::OOXMLTopLevelObject

Inherits:
OOXMLObject show all
Defined in:
lib/rubyXL/objects/ooxml_object.rb

Overview

Extension class providing functionality for top-level OOXML objects that are represented by their own .xml files in .xslx zip container.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from OOXMLObjectClassMethods

#define_attribute, #define_child_node, #define_element_name, #obtain_class_variable, #parse, #set_countable

Methods included from OOXMLObjectInstanceMethods

#before_write_xml, #dup, #index_in_collection, #initialize, #write_xml

Class Method Details

.parse_file(dirpath, file_path = nil) ⇒ Object

Generates the top-level OOXML object by parsing its XML file from the temporary directory containing the unzipped contents of .xslx

Parameters

  • dirpath - path to the directory with the unzipped .xslx contents.



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rubyXL/objects/ooxml_object.rb', line 427

def self.parse_file(dirpath, file_path = nil)
  file_path = ::Pathname.new(file_path || self.xlsx_path)

  case dirpath
  when String then
    full_path = File.join(dirpath, file_path)
    return nil unless File.exist?(full_path)
    # Making sure that the file will be automatically closed immediately after it has been read
    File.open(full_path, 'r') { |f| parse(f) }
  when Zip::File then
    file_path = file_path.relative_path_from(::Pathname.new("/")) if file_path.absolute? # Zip doesn't like absolute paths.
    entry = dirpath.find_entry(file_path)
    entry && (entry.get_input_stream { |f| parse(f) })
  end
end

.save_orderObject



409
410
411
# File 'lib/rubyXL/objects/ooxml_object.rb', line 409

def self.save_order
  500
end

.set_namespaces(namespace_hash) ⇒ Object

Sets the list of namespaces on this object to be added when writing out XML. Valid only on top-level objects.

Parameters

  • namespace_hash - Hash of namespaces in the form of "prefix" => "url"

Examples

set_namespaces('http://schemas.openxmlformats.org/spreadsheetml/2006/main' => '',
               'http://schemas.openxmlformats.org/officeDocument/2006/relationships' => 'r')


419
420
421
# File 'lib/rubyXL/objects/ooxml_object.rb', line 419

def self.set_namespaces(namespace_hash)
  self.class_variable_set(:@@ooxml_namespaces, namespace_hash)
end

Instance Method Details

#add_to_zip(zipfile) ⇒ Object

Saves the contents of the object as XML to respective location in .xslx zip container.

Parameters

  • zipfile - ::Zip::File to which the resulting XNMML should be added.



446
447
448
449
450
# File 'lib/rubyXL/objects/ooxml_object.rb', line 446

def add_to_zip(zipfile)
  xml_string = write_xml
  return if xml_string.empty?
  zipfile.get_output_stream(self.xlsx_path) { |f| f << xml_string }
end

#file_indexObject



452
453
454
# File 'lib/rubyXL/objects/ooxml_object.rb', line 452

def file_index
  @workbook.root.rels_hash[self.class].index{ |f| f.equal?(self) }.to_i + 1
end

#xlsx_pathObject

Prototype method. For top-level OOXML object, returns the path at which the current object’s XML file is located within the .xslx zip container.



405
406
407
# File 'lib/rubyXL/objects/ooxml_object.rb', line 405

def xlsx_path
  raise 'Subclass responsebility'
end