module InsertFromFile

Constants

VERSION

Public Class Methods

add_after(file_source, file_dest, str_dest) click to toggle source
# File lib/insert_from_file.rb, line 25
def self.add_after(file_source, file_dest, str_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  File.readlines(path_old).each do |line|
    file_w.write(line)
    # rubocop:disable Style/Next
    if line.include? str_dest
      File.readlines(file_source).each do |line_s|
        file_w.write(line_s)
      end
    end
    # rubocop:enable Style/Next
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end
add_before(file_source, file_dest, str_dest) click to toggle source
# File lib/insert_from_file.rb, line 8
def self.add_before(file_source, file_dest, str_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  File.readlines(path_old).each do |line|
    if line.include? str_dest
      File.readlines(file_source).each do |line_s|
        file_w.write(line_s)
      end
    end
    file_w.write(line)
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end
add_beginning(file_source, file_dest) click to toggle source
# File lib/insert_from_file.rb, line 89
def self.add_beginning(file_source, file_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  File.readlines(file_source).each do |line|
    file_w.write(line)
  end
  File.readlines(path_old).each do |line|
    file_w.write(line)
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end
add_end(file_source, file_dest) click to toggle source
# File lib/insert_from_file.rb, line 104
def self.add_end(file_source, file_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  File.readlines(path_old).each do |line|
    file_w.write(line)
  end
  File.readlines(file_source).each do |line|
    file_w.write(line)
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end
replace(file_source, file_dest, str_dest) click to toggle source
# File lib/insert_from_file.rb, line 44
def self.replace(file_source, file_dest, str_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  File.readlines(path_old).each do |line|
    if line.include? str_dest
      File.readlines(file_source).each do |line_s|
        file_w.write(line_s)
      end
    else
      file_w.write(line)
    end
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end
replace_between(file_source, file_dest, str1, str2) click to toggle source
# File lib/insert_from_file.rb, line 62
def self.replace_between(file_source, file_dest, str1, str2)
  LineContaining.delete_between(str1, str2, file_dest)
  path_old = file_dest
  path_new = "#{path_old}.new"
  file_w = File.open(path_new, 'w')
  is_before = true
  is_after = false
  File.readlines(path_old).each do |line|
    if is_before == true # Beginning
      file_w.write(line)
      if line.include? str1
        File.readlines(file_source).each do |line_s|
          file_w.write(line_s)
        end
      end
    elsif is_after == true # End
      file_w.write(line)
    elsif line.include? str2 # Middle
      file_w.write(line)
      is_after = true
    end
  end
  file_w.close
  system("rm #{path_old}")
  system("mv #{path_new} #{path_old}")
end