Class: DocxGenerator::Document

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Document

Returns a new instance of Document.



5
6
7
8
# File 'lib/docx_generator/document.rb', line 5

def initialize(filename)
  @filename = filename
  @content = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#add_paragraph(*text_fragments) ⇒ Object

text_fragments : Word::Text or String Later: Paragraph Object, with Text Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/docx_generator/document.rb', line 26

def add_paragraph(*text_fragments)
  content = []
  text_fragments.each do |text_fragment|
    content.push(text_fragment.respond_to?(:generate) ? text_fragment : text(text_fragment))
    content.push Word::Extensions.space
  end
  content.pop
  @content.push Word::Paragraph.new({}, content)
  self
end

#saveObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/docx_generator/document.rb', line 10

def save
  content_types = generate_content_types
  rels = generate_rels
  document = generate_document
  
  Zip::Archive.open(@filename + ".docx", Zip::CREATE | Zip::TRUNC) do |docx|
    docx.add_dir('_rels')
    docx.add_dir('word')
    docx.add_buffer('[Content_Types].xml', content_types)
    docx.add_buffer('_rels/.rels', rels)
    docx.add_buffer('word/document.xml', document)
  end
end

#text(text_fragment, arguments = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/docx_generator/document.rb', line 37

def text(text_fragment, arguments = {})
  content = []
  if arguments.length != 0
    options = []
    arguments.each do |option, value|
      options.push parse_option(option, value)
    end
    content.push Word::RunProperties.new({}, options)
  end
  content.push Word::Text.new({}, [text_fragment])
  Word::Run.new({}, content)
end