Class: DocxReplace::Doc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, temp_dir = nil) ⇒ Doc

Returns a new instance of Doc.



11
12
13
14
15
# File 'lib/docx_replace.rb', line 11

def initialize(path, temp_dir=nil)
  @zip_file = Zip::File.new(path)
  @temp_dir = temp_dir
  read_docx_file
end

Instance Attribute Details

#document_contentObject (readonly)

Returns the value of attribute document_content.



9
10
11
# File 'lib/docx_replace.rb', line 9

def document_content
  @document_content
end

Instance Method Details

#commit(new_path = nil) ⇒ Object



37
38
39
# File 'lib/docx_replace.rb', line 37

def commit(new_path=nil)
  write_back_to_file(new_path)
end

#matches(pattern) ⇒ Object



26
27
28
# File 'lib/docx_replace.rb', line 26

def matches(pattern)
  @document_content.scan(pattern).map{|match| match.first}
end

#replace(pattern, replacement, multiple_occurrences = false) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/docx_replace.rb', line 17

def replace(pattern, replacement, multiple_occurrences=false)
  replace = replacement.to_s.encode(xml: :text)
  if multiple_occurrences
    @document_content.force_encoding("UTF-8").gsub!(pattern, replace)
  else
    @document_content.force_encoding("UTF-8").sub!(pattern, replace)
  end
end

#unique_matches(pattern) ⇒ Object Also known as: uniq_matches



30
31
32
# File 'lib/docx_replace.rb', line 30

def unique_matches(pattern)
  matches(pattern)
end