Sha256: 00138b3652d1e7e6b4bbd4c9600570dfb17b9924ec8195dce405dbed0590cad8

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true
require 'fileutils'

module IOStreams
  module File
    class Path < IOStreams::BasePath
      # Yields the path to a temporary file_name.
      #
      # File is deleted upon completion if present.
      def self.temp_file_name(basename, extension = '')
        result = nil
        ::Dir::Tmpname.create([basename, extension]) do |tmpname|
          begin
            result = yield(tmpname)
          ensure
            ::File.unlink(tmpname) if ::File.exist?(tmpname)
          end
        end
        result
      end

      # Used by writers that can write directly to file to create the target path
      def self.mkpath(path)
        dir = ::File.dirname(path)
        FileUtils.mkdir_p(dir) unless ::File.exist?(dir)
      end

      def mkpath
        self.class.mkpath(path)
        self
      end

      def mkdir
        FileUtils.mkdir_p(path) unless ::File.exist?(path)
        self
      end

      def exist?
        ::File.exist?(path)
      end

      def size
        ::File.size(path)
      end

      def delete(recursively: false)
        return self unless ::File.exist?(path)

        if ::File.directory?(path)
          recursively ? FileUtils.remove_dir(path) : Dir.delete(path)
        else
          ::File.unlink(path)
        end
        self
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
iostreams-0.20.3 lib/io_streams/file/path.rb
iostreams-0.20.2 lib/io_streams/file/path.rb
iostreams-0.20.1 lib/io_streams/file/path.rb
iostreams-0.20.0 lib/io_streams/file/path.rb