stud 0.0.14 → 0.0.15

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 +20 -3
  2. metadata +1 -1
@@ -1,8 +1,11 @@
1
1
  require "securerandom" # for uuid generation
2
+ require "stud/with"
2
3
  require "fileutils"
3
4
 
4
5
  module Stud
5
6
  module Temporary
7
+ include Stud::With
8
+
6
9
  # Returns a string for a randomly-generated temporary path.
7
10
  #
8
11
  # This does not create any files.
@@ -19,7 +22,18 @@ module Stud
19
22
  # If no file args are given, the default file mode is "w+"
20
23
  def file(prefix="", *args, &block)
21
24
  args << "w+" if args.empty?
22
- return File.new(pathname(prefix), *args)
25
+ 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
33
+ end
34
+ else
35
+ return File.new(pathname(prefix), *args)
36
+ end
23
37
  end
24
38
 
25
39
  # Make a temporary directory.
@@ -34,8 +48,11 @@ module Stud
34
48
  Dir.mkdir(path)
35
49
 
36
50
  if block_given?
37
- block.call(path)
38
- FileUtils.rm_r(path)
51
+ begin
52
+ block.call(path)
53
+ ensure
54
+ FileUtils.rm_r(path)
55
+ end
39
56
  else
40
57
  return path
41
58
  end
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.14
4
+ version: 0.0.15
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: