Class: PureDocx::Document

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path, arguments = {}) ⇒ Document

Returns a new instance of Document.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Attribute Details

#body_contentObject

Returns the value of attribute body_content.



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

def body_content
  @body_content
end

#brakeObject (readonly)

Returns the value of attribute brake.



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

def brake
  @brake
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



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

def file_name
  @file_name
end

#file_pathObject (readonly)

Returns the value of attribute file_path.



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

def file_path
  @file_path
end

#header_contentObject

Returns the value of attribute header_content.



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

def header_content
  @header_content
end

#new_pageObject (readonly)

Returns the value of attribute new_page.



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

def new_page
  @new_page
end

#pagination_positionObject (readonly)

Returns the value of attribute pagination_position.



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

def pagination_position
  @pagination_position
end

#rels_constructorObject (readonly)

Returns the value of attribute rels_constructor.



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

def rels_constructor
  @rels_constructor
end

Instance Method Details

#content(items) ⇒ Object



31
32
33
# File 'lib/puredocx/document.rb', line 31

def content(items)
  self.body_content += items.join
end

#ensure_file!Object

Raises:



35
36
37
38
# 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) ⇒ Object



27
28
29
# File 'lib/puredocx/document.rb', line 27

def header(items)
  self.header_content += items.join
end

#save!Object



40
41
42
43
44
45
46
47
# 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