Class: RubyDocx::Document

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

Constant Summary collapse

TEMPLATE =
'document.xml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(children = []) ⇒ Document

Returns a new instance of Document.



7
8
9
# File 'lib/ruby_docx/elements/document.rb', line 7

def initialize(children = [])
  @children = children
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



5
6
7
# File 'lib/ruby_docx/elements/document.rb', line 5

def children
  @children
end

Instance Method Details

#append(block) ⇒ Object



11
12
13
# File 'lib/ruby_docx/elements/document.rb', line 11

def append(block)
  @children << block
end

#buildObject



19
20
21
22
23
24
# File 'lib/ruby_docx/elements/document.rb', line 19

def build
  doc = Nokogiri::XML(File.read( File.expand_path("../../templates/#{TEMPLATE}", __FILE__) ))
  body = doc.at '/w:document/w:body'
  @children.each { |child| body.add_child child.build }
  doc
end

#fileObject



30
31
32
33
# File 'lib/ruby_docx/elements/document.rb', line 30

def file
  file = Tempfile.new('order_preview')
  write_file(file)
end

#prepend(block) ⇒ Object



15
16
17
# File 'lib/ruby_docx/elements/document.rb', line 15

def prepend(block)
  @children.unshift block
end

#renderObject



26
27
28
# File 'lib/ruby_docx/elements/document.rb', line 26

def render
  build.to_s
end

#save(path) ⇒ Object



35
36
37
38
# File 'lib/ruby_docx/elements/document.rb', line 35

def save(path)
  file = File.open(path, 'w')
  write_file(file).close
end