rubysl-tempfile 2.0.0 → 2.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f82c47d38159bb53a71a207db4db87b6d0ace1d
4
- data.tar.gz: 026e066a5774dbfb057f861cc78a9ae7717be308
3
+ metadata.gz: af1920c240a1890026d324d8daca84bccce9066e
4
+ data.tar.gz: ba657b18a8c5b558dc403d789b3b9caebe6a3bd7
5
5
  SHA512:
6
- metadata.gz: daa1c9f4243df03b5f6266f0c451b6cba2fd1c499eab3e814575953a6c0124b3dbbe4d792c4c3e0ef11b852b41e88d24e8835ee7cb4c284027f573ee4f419b02
7
- data.tar.gz: c6485ac719d85e2fd2ace909a61a52cccc0883752de27ab15e1ba53022745ced1d5d3388465a99366669ff4d255435577493226a7b49e7d050fb20a55e33b52e
6
+ metadata.gz: 2b9cc5eebdc3b04ebe2af6f2adf2e5f1f330e6fa9fb7fcaccc854edb734555b468fa0456d28e9ac8ab0198ddd41f62d185d38aefea966702b1e9b81d76e7050f
7
+ data.tar.gz: b887c155ef7804e60dc7cc8912350ca9e80900c44208e70405c51c44e88c393c9daf757a02b1d20d82de0d1a24c5e7fcd3f1ca75e47c6e4b04d9f65088bee897
@@ -1,7 +1,14 @@
1
1
  language: ruby
2
2
  env:
3
3
  - RUBYLIB=lib
4
- script: bundle exec mspec
4
+ - RUBYLIB=
5
+ script: mspec spec
5
6
  rvm:
6
- - 1.9.3
7
- - rbx-nightly-19mode
7
+ - 2.0.0
8
+ - rbx-2.1.1
9
+ matrix:
10
+ exclude:
11
+ - rvm: 2.0.0
12
+ env: RUBYLIB=lib
13
+ - rvm: rbx-2.1.1
14
+ env: RUBYLIB=
@@ -1,7 +1,7 @@
1
1
  #
2
2
  # tempfile - manipulates temporary files
3
3
  #
4
- # $Id: tempfile.rb 31768 2011-05-28 23:31:24Z yugui $
4
+ # $Id$
5
5
  #
6
6
 
7
7
  require 'delegate'
@@ -39,7 +39,7 @@ require 'thread'
39
39
  # that's it's unnecessary to explicitly delete a Tempfile after use, though
40
40
  # it's good practice to do so: not explicitly deleting unused Tempfiles can
41
41
  # potentially leave behind large amounts of tempfiles on the filesystem
42
- # until they're garbage collected. The existance of these temp files can make
42
+ # until they're garbage collected. The existence of these temp files can make
43
43
  # it harder to determine a new Tempfile filename.
44
44
  #
45
45
  # Therefore, one should always call #unlink or close in an ensure block, like
@@ -79,7 +79,6 @@ require 'thread'
79
79
  # same Tempfile object from multiple threads then you should protect it with a
80
80
  # mutex.
81
81
  class Tempfile < DelegateClass(File)
82
- MAX_TRY = 10 # :nodoc:
83
82
  include Dir::Tmpname
84
83
 
85
84
  # call-seq:
@@ -127,12 +126,14 @@ class Tempfile < DelegateClass(File)
127
126
  # If Tempfile.new cannot find a unique filename within a limited
128
127
  # number of tries, then it will raise an exception.
129
128
  def initialize(basename, *rest)
129
+ if block_given?
130
+ warn "Tempfile.new doesn't call the given block."
131
+ end
130
132
  @data = []
131
133
  @clean_proc = Remover.new(@data)
132
134
  ObjectSpace.define_finalizer(self, @clean_proc)
133
135
 
134
136
  create(basename, *rest) do |tmpname, n, opts|
135
- lock = tmpname + '.lock'
136
137
  mode = File::RDWR|File::CREAT|File::EXCL
137
138
  perm = 0600
138
139
  if opts
@@ -142,13 +143,8 @@ class Tempfile < DelegateClass(File)
142
143
  else
143
144
  opts = perm
144
145
  end
145
- self.class.mkdir(lock)
146
- begin
147
- @data[1] = @tmpfile = File.open(tmpname, mode, opts)
148
- @data[0] = @tmpname = tmpname
149
- ensure
150
- self.class.rmdir(lock)
151
- end
146
+ @data[1] = @tmpfile = File.open(tmpname, mode, opts)
147
+ @data[0] = @tmpname = tmpname
152
148
  @mode = mode & ~(File::CREAT|File::EXCL)
153
149
  perm or opts.freeze
154
150
  @opts = opts
@@ -165,10 +161,13 @@ class Tempfile < DelegateClass(File)
165
161
  __setobj__(@tmpfile)
166
162
  end
167
163
 
168
- def _close # :nodoc:
169
- @tmpfile.close if @tmpfile
170
- @tmpfile = nil
171
- @data[1] = nil if @data
164
+ def _close # :nodoc:
165
+ begin
166
+ @tmpfile.close if @tmpfile
167
+ ensure
168
+ @tmpfile = nil
169
+ @data[1] = nil if @data
170
+ end
172
171
  end
173
172
  protected :_close
174
173
 
@@ -191,7 +190,6 @@ class Tempfile < DelegateClass(File)
191
190
  def close!
192
191
  _close
193
192
  unlink
194
- ObjectSpace.undefine_finalizer(self)
195
193
  end
196
194
 
197
195
  # Unlinks (deletes) the file from the filesystem. One should always unlink
@@ -228,18 +226,18 @@ class Tempfile < DelegateClass(File)
228
226
  # # to do so again.
229
227
  # end
230
228
  def unlink
231
- # keep this order for thread safeness
232
229
  return unless @tmpname
233
230
  begin
234
- if File.exist?(@tmpname)
235
- File.unlink(@tmpname)
236
- end
237
- # remove tmpname from remover
238
- @data[0] = @data[2] = nil
239
- @tmpname = nil
231
+ File.unlink(@tmpname)
232
+ rescue Errno::ENOENT
240
233
  rescue Errno::EACCES
241
234
  # may not be able to unlink on Windows; just ignore
235
+ return
242
236
  end
237
+ # remove tmpname from remover
238
+ @data[0] = @data[1] = nil
239
+ @tmpname = nil
240
+ ObjectSpace.undefine_finalizer(self)
243
241
  end
244
242
  alias delete unlink
245
243
 
@@ -264,6 +262,10 @@ class Tempfile < DelegateClass(File)
264
262
  alias length size
265
263
 
266
264
  # :stopdoc:
265
+ def inspect
266
+ "#<#{self.class}:#{path}>"
267
+ end
268
+
267
269
  class Remover
268
270
  def initialize(data)
269
271
  @pid = $$
@@ -271,20 +273,22 @@ class Tempfile < DelegateClass(File)
271
273
  end
272
274
 
273
275
  def call(*args)
274
- if @pid == $$
275
- path, tmpfile = *@data
276
+ return if @pid != $$
276
277
 
277
- STDERR.print "removing ", path, "..." if $DEBUG
278
+ path, tmpfile = *@data
278
279
 
279
- tmpfile.close if tmpfile
280
+ STDERR.print "removing ", path, "..." if $DEBUG
280
281
 
281
- # keep this order for thread safeness
282
- if path
283
- File.unlink(path) if File.exist?(path)
284
- end
282
+ tmpfile.close if tmpfile
285
283
 
286
- STDERR.print "done\n" if $DEBUG
284
+ if path
285
+ begin
286
+ File.unlink(path)
287
+ rescue Errno::ENOENT
288
+ end
287
289
  end
290
+
291
+ STDERR.print "done\n" if $DEBUG
288
292
  end
289
293
  end
290
294
  # :startdoc:
@@ -296,7 +300,7 @@ class Tempfile < DelegateClass(File)
296
300
  #
297
301
  # If a block is given, then a Tempfile object will be constructed,
298
302
  # and the block is run with said object as argument. The Tempfile
299
- # oject will be automatically closed after the block terminates.
303
+ # object will be automatically closed after the block terminates.
300
304
  # The call returns the value of the block.
301
305
  #
302
306
  # In any case, all arguments (+*args+) will be passed to Tempfile.new.
@@ -316,22 +320,61 @@ class Tempfile < DelegateClass(File)
316
320
  tempfile = new(*args)
317
321
 
318
322
  if block_given?
319
- begin
320
- yield(tempfile)
321
- ensure
322
- tempfile.close
323
- end
323
+ begin
324
+ yield(tempfile)
325
+ ensure
326
+ tempfile.close
327
+ end
324
328
  else
325
- tempfile
329
+ tempfile
326
330
  end
327
331
  end
332
+ end
333
+ end
328
334
 
329
- def mkdir(*args)
330
- Dir.mkdir(*args)
335
+ # Creates a temporally file as usual File object (not Tempfile).
336
+ # It don't use finalizer and delegation.
337
+ #
338
+ # If no block is given, this is similar to Tempfile.new except
339
+ # creating File instead of Tempfile.
340
+ # The created file is not removed automatically.
341
+ # You should use File.unlink to remove it.
342
+ #
343
+ # If a block is given, then a File object will be constructed,
344
+ # and the block is invoked with the object as the argument.
345
+ # The File object will be automatically closed and
346
+ # the temporally file is removed after the block terminates.
347
+ # The call returns the value of the block.
348
+ #
349
+ # In any case, all arguments (+*args+) will be treated as Tempfile.new.
350
+ #
351
+ # Tempfile.create('foo', '/home/temp') do |f|
352
+ # ... do something with f ...
353
+ # end
354
+ #
355
+ def Tempfile.create(basename, *rest)
356
+ tmpfile = nil
357
+ Dir::Tmpname.create(basename, *rest) do |tmpname, n, opts|
358
+ mode = File::RDWR|File::CREAT|File::EXCL
359
+ perm = 0600
360
+ if opts
361
+ mode |= opts.delete(:mode) || 0
362
+ opts[:perm] = perm
363
+ perm = nil
364
+ else
365
+ opts = perm
331
366
  end
332
- def rmdir(*args)
333
- Dir.rmdir(*args)
367
+ tmpfile = File.open(tmpname, mode, opts)
368
+ end
369
+ if block_given?
370
+ begin
371
+ yield tmpfile
372
+ ensure
373
+ tmpfile.close if !tmpfile.closed?
374
+ File.unlink tmpfile
334
375
  end
376
+ else
377
+ tmpfile
335
378
  end
336
379
  end
337
380
 
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Tempfile
3
- VERSION = "2.0.0"
3
+ VERSION = "2.0.1"
4
4
  end
5
5
  end
@@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "mspec", "~> 1.5"
24
+ spec.add_development_dependency "rubysl-prettyprint", "~> 2.0"
24
25
  end
@@ -0,0 +1,38 @@
1
+ describe "Tempfile#create" do
2
+ before do
3
+ @path = nil
4
+ end
5
+
6
+ describe "with block" do
7
+ it "should exist inside the block" do
8
+ Tempfile.create("tempfile-create") do |f|
9
+ @path = f.path
10
+ File.exist?(@path).should be_true
11
+ end
12
+ end
13
+
14
+ it "should be deleted after the block finishes" do
15
+ Tempfile.create("tempfile-create") do |f|
16
+ @path = f.path
17
+ end
18
+ File.exist?(@path).should_not be_true
19
+ end
20
+ end
21
+
22
+ describe "without block" do
23
+ before do
24
+ @tempfile = Tempfile.create("tempfile-create")
25
+ end
26
+
27
+ it "should not be deleted before closing" do
28
+ @path = @tempfile.path
29
+ File.exist?(@path).should be_true
30
+ end
31
+
32
+ it "should not be deleted after closing" do
33
+ @path = @tempfile.path
34
+ @tempfile.close
35
+ File.exist?(@path).should be_true
36
+ end
37
+ end
38
+ end
metadata CHANGED
@@ -1,57 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-tempfile
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-17 00:00:00.000000000 Z
11
+ date: 2013-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ~>
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
33
  version: '10.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ~>
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: mspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.5'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubysl-prettyprint
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
55
69
  description: Ruby standard library tempfile.
56
70
  email:
57
71
  - brixen@gmail.com
@@ -59,8 +73,8 @@ executables: []
59
73
  extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
- - .gitignore
63
- - .travis.yml
76
+ - ".gitignore"
77
+ - ".travis.yml"
64
78
  - Gemfile
65
79
  - LICENSE
66
80
  - README.md
@@ -73,6 +87,7 @@ files:
73
87
  - spec/_close_spec.rb
74
88
  - spec/callback_spec.rb
75
89
  - spec/close_spec.rb
90
+ - spec/create_spec.rb
76
91
  - spec/delete_spec.rb
77
92
  - spec/fixtures/common.rb
78
93
  - spec/initialize_spec.rb
@@ -93,12 +108,12 @@ require_paths:
93
108
  - lib
94
109
  required_ruby_version: !ruby/object:Gem::Requirement
95
110
  requirements:
96
- - - ~>
111
+ - - "~>"
97
112
  - !ruby/object:Gem::Version
98
113
  version: '2.0'
99
114
  required_rubygems_version: !ruby/object:Gem::Requirement
100
115
  requirements:
101
- - - '>='
116
+ - - ">="
102
117
  - !ruby/object:Gem::Version
103
118
  version: '0'
104
119
  requirements: []
@@ -111,6 +126,7 @@ test_files:
111
126
  - spec/_close_spec.rb
112
127
  - spec/callback_spec.rb
113
128
  - spec/close_spec.rb
129
+ - spec/create_spec.rb
114
130
  - spec/delete_spec.rb
115
131
  - spec/fixtures/common.rb
116
132
  - spec/initialize_spec.rb
@@ -121,3 +137,4 @@ test_files:
121
137
  - spec/shared/unlink.rb
122
138
  - spec/size_spec.rb
123
139
  - spec/unlink_spec.rb
140
+ has_rdoc: