class ErpTechSvcs::FileManipulator

Public Class Methods

append_file(path, content) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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