stud 0.0.16 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/stud/temporary.rb +11 -13
- metadata +1 -1
data/lib/stud/temporary.rb
CHANGED
@@ -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
|
-
|
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=
|
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
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
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=
|
44
|
+
def directory(prefix=DEFAULT_PREFIX, &block)
|
47
45
|
path = pathname(prefix)
|
48
46
|
Dir.mkdir(path)
|
49
47
|
|