Sha256: d35116954c337f08fe544c734c44e6a5ed8376c250d0751d79cf74b0b95bd55f

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

require "line_containing/version"

module LineContaining
  def self.add_before(str_orig, str_add, path)
    system("pwd")
    path_old = path
    path_new = "#{path_old}.new"
    file_w = open(path_new, 'w')
    File.readlines(path_old).each do |line|
      if line.include? str_orig
        file_w.write("#{str_add}\n")
        file_w.write(line)
      else
        file_w.write(line)
      end
    end
    file_w.close
    system("rm #{path_old}")
    system("mv #{path_new} #{path_old}")
  end
  
  def self.add_after(str_orig, str_add, path)
    path_old = path
    path_new = "#{path_old}.new"
    file_w = open(path_new, 'w')
    File.readlines(path_old).each do |line|
      if line.include? str_orig
        file_w.write(line)
        file_w.write("\n") if line[-1] != "\n"
        file_w.write("#{str_add}\n")
      else
        file_w.write(line)
      end
    end
    file_w.close
    system("rm #{path_old}")
    system("mv #{path_new} #{path_old}")
  end
  
  def self.replace(str_orig, str_new, path)
    path_old = path
    path_new = "#{path_old}.new"
    file_w = open(path_new, 'w')
    File.readlines(path_old).each do |line|
      if line.include? str_orig
        file_w.write("#{str_new}\n")
      else
        file_w.write(line)
      end
    end
    file_w.close
    system("rm #{path_old}")
    system("mv #{path_new} #{path_old}")
  end
  
  def self.delete(str_orig, path)
    path_old = path
    path_new = "#{path_old}.new"
    file_w = open(path_new, 'w')
    File.readlines(path_old).each do |line|
      if line.include? str_orig
        # Print NOTHING
      else
        file_w.write(line)
      end
    end
    file_w.close
    system("rm #{path_old}")
    system("mv #{path_new} #{path_old}")
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
line_containing-0.0.0 lib/line_containing.rb