Class: RubyXL::Writer::CoreWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyXL/writer/core_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dirpath, wb) ⇒ CoreWriter

Returns a new instance of CoreWriter.



13
14
15
16
17
# File 'lib/rubyXL/writer/core_writer.rb', line 13

def initialize(dirpath, wb)
  @dirpath = dirpath
  @workbook = wb
  @filepath = @dirpath + '/docProps/core.xml'
end

Instance Attribute Details

#dirpathObject

Returns the value of attribute dirpath.



11
12
13
# File 'lib/rubyXL/writer/core_writer.rb', line 11

def dirpath
  @dirpath
end

#filepathObject

Returns the value of attribute filepath.



11
12
13
# File 'lib/rubyXL/writer/core_writer.rb', line 11

def filepath
  @filepath
end

#workbookObject

Returns the value of attribute workbook.



11
12
13
# File 'lib/rubyXL/writer/core_writer.rb', line 11

def workbook
  @workbook
end

Instance Method Details

#writeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubyXL/writer/core_writer.rb', line 19

def write()
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.coreProperties('xmlns:cp'=>"http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
    'xmlns:dc'=>"http://purl.org/dc/elements/1.1/", 'xmlns:dcterms'=>"http://purl.org/dc/terms/",
    'xmlns:dcmitype'=>"http://purl.org/dc/dcmitype/", 'xmlns:xsi'=>"http://www.w3.org/2001/XMLSchema-instance") {
      xml['dc'].creator @workbook.creator.to_s
      xml['cp'].lastModifiedBy @workbook.modifier.to_s
      xml['dcterms'].created('xsi:type' => 'dcterms:W3CDTF') do
        @workbook.created_at
      end

      xml['dcterms'].modified('xsi:type' => 'dcterms:W3CDTF')
    }
  end

  contents = builder.to_xml
  contents = contents.gsub(/coreProperties/,'cp:coreProperties')
  contents = contents.gsub(/\n/,'')
  contents = contents.gsub(/>(\s)+</,'><')

  #seems hack-y..
  contents = contents.gsub(/<dcterms:created xsi:type=\"dcterms:W3CDTF\"\/>/,
    '<dcterms:created xsi:type="dcterms:W3CDTF">'+@workbook.created_at+'</dcterms:created>')
  contents = contents.gsub(/<dcterms:modified xsi:type=\"dcterms:W3CDTF\"\/>/,
    '<dcterms:modified xsi:type="dcterms:W3CDTF">'+@workbook.modified_at+'</dcterms:modified>')

  contents = contents.sub(/<\?xml version=\"1.0\"\?>/,'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'+"\n")

  return contents
end