Sha256: d91852f429263b901b491e51e3ea9c2cceea99918fb6ff1f944b37dd0685f999

Contents?: true

Size: 1.6 KB

Versions: 71

Compression:

Stored size: 1.6 KB

Contents

require "securerandom" # for uuid generation
require "fileutils"

module Stud
  module Temporary
    DEFAULT_PREFIX = "studtmp"

    # Returns a string for a randomly-generated temporary path.
    #
    # This does not create any files.
    def pathname(prefix=DEFAULT_PREFIX)

      root = ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMP"] || "/tmp"
      return File.join(root, "#{prefix}-#{SecureRandom.hex(30)}")
    end

    # Return a File handle to a randomly-generated path.
    #
    # Any arguments beyond the first (prefix) argument will be
    # given to File.new.
    #
    # If no file args are given, the default file mode is "w+"
    def file(prefix=DEFAULT_PREFIX, *args, &block)
      args << "w+" if args.empty?
      file = File.new(pathname(prefix), *args)
      if block_given?
        begin
          block.call(file)
        ensure
          file.close unless file.closed?
          File.unlink(file.path)
        end
      else
        return file
      end
    end

    # Make a temporary directory.
    #
    # If given a block, the directory path is given to the block.  WHen the
    # block finishes, the directory and all its contents will be deleted.
    #
    # If no block given, it will return the path to a newly created directory.
    # You are responsible for then cleaning up.
    def directory(prefix=DEFAULT_PREFIX, &block)
      path = pathname(prefix)
      Dir.mkdir(path)

      if block_given?
        begin
          block.call(path)
        ensure
          FileUtils.rm_r(path)
        end
      else
        return path
      end
    end
    extend self
  end # module Temporary

end # module Stud

Version data entries

71 entries across 67 versions & 18 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/stud-0.0.23/lib/stud/temporary.rb