class PureDocx::Document

Attributes

body_content[RW]
brake[R]
file_name[R]
file_path[R]
header_content[RW]
new_page[R]
pagination_position[R]
rels_constructor[R]

Public Class Methods

new(file_path, arguments = {}) click to toggle source
# File lib/puredocx/document.rb, line 6
def initialize(file_path, arguments = {})
  @file_path = file_path
  ensure_file!
  @file_name           = File.basename(file_path)
  @pagination_position = arguments[:pagination_position]
  @rels_constructor    = PureDocx::Constructors::Rels.new
  @brake               = File.read(DocArchive.template_path('brake.xml'))
  @new_page            = File.read(DocArchive.template_path('new_page.xml'))
  @header_content      = ''
  @body_content        = ''
  (class << self; self; end).class_eval do
    %i[text table image].each do |method_name|
      define_method method_name do |content, options = {}|
        Object.const_get(
          "PureDocx::XmlGenerators::#{method_name.to_s.capitalize}"
        ).new(content, rels_constructor, options).xml
      end
    end
  end
end

Public Instance Methods

content(items) click to toggle source
# File lib/puredocx/document.rb, line 31
def content(items)
  self.body_content += items.join
end
ensure_file!() click to toggle source
# File lib/puredocx/document.rb, line 35
def ensure_file!
  return unless File.exist?(file_path)
  raise FileCreatingError, 'File already exists in this directory. Please change the file name!'
end
header(items) click to toggle source
# File lib/puredocx/document.rb, line 27
def header(items)
  self.header_content += items.join
end
save!() click to toggle source
# File lib/puredocx/document.rb, line 40
def save!
  rels_constructor.prepare_basic_rels!
  DocArchive.open(file_path, rels_constructor.rels) do |file|
    file.add('[Content_Types].xml', DocArchive.template_path('[Content_Types].xml'))
    file.save_rels
    file.save_document_content(body_content, header_content, pagination_position)
  end
end