Class: DocxGenerator::DSL::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/docx_generator/dsl/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) {|_self| ... } ⇒ Document

Returns a new instance of Document.

Yields:

  • (_self)

Yield Parameters:



6
7
8
9
10
# File 'lib/docx_generator/dsl/document.rb', line 6

def initialize(filename, &block)
  @filename = filename + ".docx"
  @objects = [] # It contains all the DSL elements
  yield self if block
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/docx_generator/dsl/document.rb', line 4

def filename
  @filename
end

Instance Method Details

#add(*objects) ⇒ Object



34
35
36
37
38
39
# File 'lib/docx_generator/dsl/document.rb', line 34

def add(*objects)
  objects.each do |object|
    @objects << object
  end
  self
end

#paragraph(options = {}) {|par| ... } ⇒ Object

Yields:

  • (par)


28
29
30
31
32
# File 'lib/docx_generator/dsl/document.rb', line 28

def paragraph(options = {}, &block)
  par = DocxGenerator::DSL::Paragraph.new(options)
  yield par if block
  @objects << par
end

#saveObject

Could be better



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/docx_generator/dsl/document.rb', line 13

def save
  content_types = generate_content_types
  rels = generate_rels
  document = generate_document
  
  File.delete(@filename) if File.exists?(@filename)
  Zip::ZipFile.open(@filename, Zip::ZipFile::CREATE) do |docx|
    docx.mkdir('_rels')
    docx.mkdir('word')
    docx.get_output_stream('[Content_Types].xml') { |f| f.puts content_types }
    docx.get_output_stream('_rels/.rels') { |f| f.puts rels }
    docx.get_output_stream('word/document.xml') { |f| f.puts document }
  end
end