Sha256: a845949503f6f3a389db62758ccea7072cad45eecd93c142f25c39f3c4372a84

Contents?: true

Size: 1.53 KB

Versions: 4

Compression:

Stored size: 1.53 KB

Contents

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

module Sysadmin
  module FileExtension
    require 'zlib'
    Zlib::GZIP_MAGIC = "\x1F\x8B"
    Zlib::GZIP_MAGIC.force_encoding("ASCII-8BIT") if RUBY_VERSION >= "1.9"

    def File.zread(file)
      Object.module_eval do
        open(file) {|f|
          magic = f.read(2)
          f.rewind
          if magic == Zlib::GZIP_MAGIC
            Zlib::GzipReader.wrap(f) {|gz|gz.read}
          else
            f.read
          end
        }
      end
    end

    def File.append_line(params)
      if File.exist?(params[:file])
        f = open(params[:file], 'a')
      else
        f = open(params[:file], 'w')
      end
      f << params[:str]
      f << "\n"
      f.close
    end

    def File.new_line(params)
      f = open(params[:file], 'w')
      f << params[:str]
      f << "\n"
      f.close
    end

    def File.replace_line(params)
      open(params[:file], "r+") { |f|
        f.rewind
        body = f.read
        body = body.gsub(params[:src]) { |tmp|
          params[:dst]
        }
        f.rewind
        f.puts body
        f.truncate(f.tell)
      }
    end

    def File.remove_line(params)
      out = ""
      IO.foreach(params[:file]) { |line|
        out << line unless line.include?(params[:str])
      }
      open(params[:file], "w") { |f|
        f.write out
      }
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sysadmin-0.2.0 lib/sysadmin/file_ext.rb
sysadmin-0.1.5 lib/sysadmin/file_ext.rb
sysadmin-0.1.4 lib/sysadmin/file_ext.rb
sysadmin-0.1.3 lib/sysadmin/file_ext.rb