Class: DocxGenerator::DSL::Document

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

Overview

Represent the docx document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Create a new docx document.

Parameters:

  • filename (String)

    The filename of the docx file, without the docx extension.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
# File 'lib/docx_generator/dsl/document.rb', line 10

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

Instance Attribute Details

#filenameObject (readonly)

Filename of the document (without the docx extension).



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

def filename
  @filename
end

Instance Method Details

#add(*objects) ⇒ DocxGenerator::DSL::Document

Add other objects to the document.

Parameters:

  • objects (Object)

    Objects (like paragraphs).

Returns:



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

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

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

Add a new paragraph to the document.

Parameters:

  • options (Hash) (defaults to: {})

    Formatting options for the paragraph. See the full list in DocxGenerator::DSL::Paragraph.

Yields:

  • (par)


23
24
25
26
27
# File 'lib/docx_generator/dsl/document.rb', line 23

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

#saveObject

Save the docx document to the target location.



17
18
19
# File 'lib/docx_generator/dsl/document.rb', line 17

def save
  generate_archive(generate_content_types, generate_rels, generate_document)
end