Sha256: 0117ef1fbaf6f4d461c4c12b65ca3b8fb1b4a4a4adf0876b86a64700157f311a

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

# Name::      Sysadmin::FileString
# Author::    774 <http://id774.net>
# Created::   Mar 23, 2012
# Updated::   Mar 23, 2012
# Copyright:: 774 Copyright (c) 2012
# License::   Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0.

module Sysadmin
  module FileString
    def self.append(file, str)
      if File.exist?(file)
        f = open(file, 'a')
      else
        f = open(file, 'w')
      end
      f << str
      f << "\n"
      f.close
    end

    def self.replaceFile(f, src, out)
      f.rewind
      body = f.read
      body = body.gsub(src) { |tmp|
        out
      }
      f.rewind
      f.puts body
    end

    def self.replace(file, src, out)
      open(file,"r+") { |f|
        f.flock(File::LOCK_EX)
        replaceFile(f,src,out)
        f.truncate(f.tell)
      }
    end

    def self.delete(file, str)
      out = ""
      IO.foreach(file) { |line|
        out << line unless line.include?(str)
      }
      open(file,"w") { |f|
        f.write out
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sysadmin-0.0.1 lib/sysadmin/file_string.rb