stud 0.0.16 → 0.0.17

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/stud/temporary.rb +11 -13
  2. metadata +1 -1
@@ -1,15 +1,15 @@
1
1
  require "securerandom" # for uuid generation
2
- require "stud/with"
3
2
  require "fileutils"
4
3
 
5
4
  module Stud
6
5
  module Temporary
7
- include Stud::With
6
+ DEFAULT_PREFIX = "studtmp"
8
7
 
9
8
  # Returns a string for a randomly-generated temporary path.
10
9
  #
11
10
  # This does not create any files.
12
- def pathname(prefix="")
11
+ def pathname(prefix=DEFAULT_PREFIX)
12
+
13
13
  root = ENV["TMP"] || ENV["TMPDIR"] || ENV["TEMP"] || "/tmp"
14
14
  return File.join(root, "#{prefix}-#{SecureRandom.uuid}")
15
15
  end
@@ -20,19 +20,17 @@ module Stud
20
20
  # given to File.new.
21
21
  #
22
22
  # If no file args are given, the default file mode is "w+"
23
- def file(prefix="", *args, &block)
23
+ def file(prefix=DEFAULT_PREFIX, *args, &block)
24
24
  args << "w+" if args.empty?
25
+ file = File.new(pathname(prefix), *args)
25
26
  if block_given?
26
- with(File.new(pathname(prefix), *args)) do |fd|
27
- begin
28
- block.call(fd)
29
- fd.close
30
- ensure
31
- File.unlink(fd.path)
32
- end
27
+ begin
28
+ block.call(file)
29
+ ensure
30
+ File.unlink(file.path)
33
31
  end
34
32
  else
35
- return File.new(pathname(prefix), *args)
33
+ return file
36
34
  end
37
35
  end
38
36
 
@@ -43,7 +41,7 @@ module Stud
43
41
  #
44
42
  # If no block given, it will return the path to a newly created directory.
45
43
  # You are responsible for then cleaning up.
46
- def directory(prefix="", &block)
44
+ def directory(prefix=DEFAULT_PREFIX, &block)
47
45
  path = pathname(prefix)
48
46
  Dir.mkdir(path)
49
47
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16
4
+ version: 0.0.17
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: