Sha256: 8c9be46ec332f2cc72e7c844256373e16c119139a6e7b7c4385102c89d026cda

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require 'tempfile'
require 'ftools'
require 'digest/md5'

module Gjman

  class FileNotFoundError < Exception ; end

  private

    module FileSystem
      class << self

        def tmp_dir(&block)
          Dir.mktmpdir(&block)
        end

        def file_must_exist!(path, timeout=0)
          if timeout.zero?
            File.exists?(path) or raise_file_not_found_error(path)
          else
            0.upto(timeout.pred) {|i| File.exists?(path) ? (return true) : sleep(1) }
            raise_file_not_found_error(path)
          end
        end

        def trash_tmp_files
          (@trashable_tmp_files || []).each {|f| f.path && f.unlink }
          @trashable_tmp_files = nil
        end

        def tmp_file(basename = nil)
          basename ||= Digest::MD5.hexdigest(Time.now.to_s)
          ((@trashable_tmp_files ||= []) << Tempfile.new(basename))[-1]
        end

        protected

          def raise_file_not_found_error(path)
            raise FileNotFoundError.new("File '#{path}' not found.")
          end

      end
    end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gjman-0.1.0 lib/gjman/file_system.rb