Class: DocxReplace::Doc

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

Instance Method Summary collapse

Constructor Details

#initialize(path, temp_dir = nil) ⇒ Doc

Returns a new instance of Doc.



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

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

Instance Method Details

#commit(new_path = nil) ⇒ Object



35
36
37
# File 'lib/docx_replace.rb', line 35

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

#matches(pattern) ⇒ Object



24
25
26
# File 'lib/docx_replace.rb', line 24

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

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



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

def replace(pattern, replacement, multiple_occurrences=false)
  replace = replacement.to_s
  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



28
29
30
# File 'lib/docx_replace.rb', line 28

def unique_matches(pattern)
  matches(pattern)
end