Class: ErpTechSvcs::FileManipulator

Inherits:
Object
  • Object
show all
Defined in:
lib/erp_tech_svcs/file_support/file_manipulator.rb

Class Method Summary collapse

Class Method Details

.append_file(path, content) ⇒ Object



18
19
20
# File 'lib/erp_tech_svcs/file_support/file_manipulator.rb', line 18

def append_file(path, content)
  File.open(path, 'a') { |f| f.puts(content) }
end

.patch_file(path, current, insert, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/erp_tech_svcs/file_support/file_manipulator.rb', line 5

def patch_file(path, current, insert, options = {})
  options = {
    :patch_mode => :insert_after
  }.merge(options)

  old_text = current
  new_text = patch_string(current, insert, options[:patch_mode])

  content = File.open(path) { |f| f.read }
  content.gsub!(old_text, new_text) unless content =~ /#{Regexp.escape(insert)}/mi
  File.open(path, 'w') { |f| f.puts(content) }
end

.patch_string(current, insert, mode = :insert_after) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/erp_tech_svcs/file_support/file_manipulator.rb', line 22

def patch_string(current, insert, mode = :insert_after)
  case mode
  when :change
    "#{insert}"
  when :insert_after
    "#{current}\n#{insert}"
  when :insert_before
    "#{insert}\n#{current}"
  else
    patch_string(current, insert, :insert_after)
  end
end