temppath 0.2.0 → 0.3.0

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/README.md CHANGED
@@ -27,6 +27,7 @@ path.open("w")
27
27
 
28
28
  ```ruby
29
29
  path = Temppath.mkdir
30
+ path.exist? #=> true
30
31
  path.directory? #=> true
31
32
  "%o" % path.stat.mode #=> "40700"
32
33
  ```
@@ -35,7 +36,34 @@ path.directory? #=> true
35
36
 
36
37
  ```ruby
37
38
  path = Temppath.touch
38
- path.file? #=> true
39
+ path.exist? #=> true
40
+ path.file? #=> true
41
+ "%o" % path.stat.mode #=> "100600"
42
+ ```
43
+
44
+ #### Use temporary path generator
45
+
46
+ You can use Temppath::Generator when you want to use multiple base
47
+ directory. Generated paths have same natures as paths from Tempath.create,
48
+ mkdir, and touch.
49
+
50
+ ```ruby
51
+ # make a generator
52
+ temppath = Temppath::Generator.new("/tmp/other-dir")
53
+
54
+ path = temppath.create
55
+ path.exist? #=> false
56
+ path.open("w")
57
+ "%o" % path.stat.mode #=> "100600"
58
+
59
+ path = temppath.mkdir
60
+ path.exist? #=> true
61
+ path.directory? #=> true
62
+ "%o" % path.stat.mode #=> "40700"
63
+
64
+ path = temppath.touch
65
+ path.exist? #=> true
66
+ path.file? #=> true
39
67
  "%o" % path.stat.mode #=> "100600"
40
68
  ```
41
69
 
data/lib/temppath.rb CHANGED
@@ -16,12 +16,19 @@ require 'fileutils'
16
16
  # "%o" % path.stat.mode #=> "100600" (default permission 0600)
17
17
  # @example Touch a temporary file
18
18
  # path = Temppath.touch
19
- # path.file? #=> true
19
+ # path.exist? #=> true
20
+ # path.file? #=> true
20
21
  # "%o" % path.stat.mode #=> "100600"
21
22
  # @example Create a temporary directory
22
23
  # path = Temppath.mkdir
24
+ # path.exist? #=> true
23
25
  # path.directory? #=> true
24
26
  # "%o" % path.stat.mode #=> "40700"
27
+ # @example Use temporary path generator
28
+ # temppath = Temppath::Generator.new("/tmp/other-dir")
29
+ # temppath.create
30
+ # temppath.mkdir
31
+ # temppath.touch
25
32
  module Temppath
26
33
  # OriginalMethodHolder keeps some original methods of Pathname.
27
34
  module OriginalMethodHolder
@@ -72,15 +79,38 @@ module Temppath
72
79
  end
73
80
  end
74
81
 
75
- class << self
82
+ # Generator generates temporary path in the base directory.
83
+ class Generator
76
84
  # @return [Pathname]
77
85
  # defalut base directory for paths created by Temppath
78
86
  attr_reader :basedir
79
87
 
88
+ # @return [String]
89
+ # defalut base name
90
+ attr_accessor :basename
91
+
80
92
  # @return [Boolean]
81
93
  # true if unlink mode is enabled
82
94
  attr_accessor :unlink
83
95
 
96
+ # @param basedir [Pathname]
97
+ # generator's base directory
98
+ # @param option [Hash]
99
+ # @option option [String] :basename
100
+ # prefix of filename
101
+ def initialize(basedir, option={})
102
+ @basedir = Pathname.new(basedir)
103
+ @basename = (option[:basename] || "").to_s
104
+ @unlink = true
105
+
106
+ # register a cleaner for temporary directory
107
+ Kernel.at_exit do
108
+ if @unlink
109
+ remove_basedir rescue Errno::ENOENT
110
+ end
111
+ end
112
+ end
113
+
84
114
  # Create a temporary path. This method creates no files.
85
115
  #
86
116
  # @param option [Hash]
@@ -89,18 +119,42 @@ module Temppath
89
119
  # @option option [Pathname] :basedir
90
120
  # pathname of base directory
91
121
  def create(option={})
92
- basename = option[:basename] || ""
93
- _basedir = option[:basedir] || basedir
94
- path = Pathname.new(_basedir) + (basename.to_s + generate_uuid)
122
+ _basename = option[:basename] || @basename
123
+ _basedir = Pathname.new(option[:basedir] || @basedir)
124
+
125
+ # init basedir
126
+ unless _basedir.exist?
127
+ _basedir.mkdir(0700)
128
+ end
129
+
130
+ # make a path
131
+ path = Pathname.new(_basedir) + (_basename.to_s + generate_uuid)
132
+
133
+ # extend path object with secure methods
95
134
  path.extend OriginalMethodHolder
96
135
  path.extend SecurePermissionMethods
97
- if _basedir != basedir
98
- Kernel.at_exit {FileUtils.remove_entry_secure(path) rescue Errno::ENOENT}
136
+
137
+ # register a file cleaner if the path is not in basedir
138
+ if _basedir != @basedir
139
+ Kernel.at_exit do
140
+ if @unlink
141
+ FileUtils.remove_entry_secure(path) rescue Errno::ENOENT
142
+ end
143
+ end
99
144
  end
145
+
100
146
  return path
101
147
  end
102
148
 
103
149
  # Create a temporary directory.
150
+ #
151
+ # @param option [Hash]
152
+ # @option option [Integer] :mode
153
+ # mode for the directory permission
154
+ # @option option [String] :basename
155
+ # prefix of directory name
156
+ # @option option [Pathname] :basedir
157
+ # pathname of base directory
104
158
  def mkdir(option={})
105
159
  mode = option[:mode] || 0700
106
160
  path = create(option)
@@ -109,6 +163,14 @@ module Temppath
109
163
  end
110
164
 
111
165
  # Create a empty file.
166
+ #
167
+ # @param option [Hash]
168
+ # @option option [Integer] :mode
169
+ # mode for the file permission
170
+ # @option option [String] :basename
171
+ # prefix of filename
172
+ # @option option [Pathname] :basedir
173
+ # pathname of base directory
112
174
  def touch(option={})
113
175
  mode = option[:mode] || 0600
114
176
  path = create(option)
@@ -116,16 +178,6 @@ module Temppath
116
178
  return path
117
179
  end
118
180
 
119
- # Remove current base directory, create a new base directory, and
120
- # use it.
121
- #
122
- # @return [Pathname]
123
- # new base directory
124
- def update_basedir
125
- remove_basedir
126
- @basedir = create_basedir
127
- end
128
-
129
181
  # Remove current temporary directory.
130
182
  #
131
183
  # @return [void]
@@ -135,6 +187,16 @@ module Temppath
135
187
 
136
188
  private
137
189
 
190
+ # Generate random UUID for filename of temporary path.
191
+ #
192
+ # @return [String]
193
+ # UUID string
194
+ def generate_uuid
195
+ UUIDTools::UUID.random_create.to_s
196
+ end
197
+ end
198
+
199
+ class << self
138
200
  # Create a new base directory.
139
201
  #
140
202
  # @return [Pathname]
@@ -142,23 +204,84 @@ module Temppath
142
204
  def create_basedir
143
205
  Pathname.new(Dir.mktmpdir("ruby-temppath-"))
144
206
  end
207
+ private :create_basedir
208
+ end
145
209
 
146
- # Generate random UUID for filename of temporary path.
210
+ # default generator
211
+ @generator = Generator.new(create_basedir)
212
+
213
+ class << self
214
+ # Return base directory of paths created by Temppath.
147
215
  #
148
- # @return [String]
149
- # UUID string
150
- def generate_uuid
151
- UUIDTools::UUID.random_create.to_s
216
+ # @return [Pathname]
217
+ # base directory
218
+ def basedir
219
+ @generator.basedir
152
220
  end
153
- end
154
221
 
155
- @basedir = create_basedir
156
- @unlink = true
157
- end
222
+ # Return true if unlink mode is enabled.
223
+ #
224
+ # @return [Boolean]
225
+ # true if unlink mode is enabled
226
+ def unlink
227
+ @generator.unlink
228
+ end
229
+
230
+ # Set true or false unlink mode.
231
+ #
232
+ # @param b [Boolean]
233
+ # unlink mode
234
+ def unlink=(b)
235
+ @generator.unlink = b
236
+ end
237
+
238
+ # Create a temporary path. This method creates no files.
239
+ #
240
+ # @param option [Hash]
241
+ # @option option [String] :basename
242
+ # prefix of filename
243
+ # @option option [Pathname] :basedir
244
+ # pathname of base directory
245
+ def create(option={})
246
+ @generator.create(option)
247
+ end
248
+
249
+ # Create a temporary directory.
250
+ #
251
+ # @param option [Hash]
252
+ # @option option [Integer] :mode
253
+ # mode for the directory permission
254
+ # @option option [String] :basename
255
+ # prefix of directory name
256
+ # @option option [Pathname] :basedir
257
+ # pathname of base directory
258
+ def mkdir(option={})
259
+ @generator.mkdir(option)
260
+ end
261
+
262
+ # Create a empty file.
263
+ def touch(option={})
264
+ @generator.touch(option)
265
+ end
266
+
267
+ # Remove current base directory and change to use a new base directory.
268
+ #
269
+ # @param basedir [Pathname]
270
+ # new base directory, or nil
271
+ # @return [Pathname]
272
+ # new base directory
273
+ def update_basedir(basedir=nil)
274
+ @generator.remove_basedir
275
+ _basedir = basedir || create_basedir
276
+ @generator = Generator.new(_basedir)
277
+ return _basedir
278
+ end
158
279
 
159
- # Remove Temppath's temporary directory.
160
- Kernel.at_exit do
161
- if Temppath.unlink
162
- Temppath.remove_basedir rescue Errno::ENOENT
280
+ # Remove current temporary directory.
281
+ #
282
+ # @return [void]
283
+ def remove_basedir
284
+ @generator.remove_basedir
285
+ end
163
286
  end
164
287
  end
@@ -1,4 +1,4 @@
1
1
  module Temppath
2
2
  # version of temppath gem
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -1,6 +1,6 @@
1
1
  require 'temppath'
2
2
 
3
- describe 'Temppath' do
3
+ describe Temppath do
4
4
  before do
5
5
  Temppath.update_basedir
6
6
  end
@@ -28,6 +28,17 @@ describe 'Temppath' do
28
28
  new_dir = Temppath.update_basedir
29
29
  old_dir.should != new_dir
30
30
  old_dir.should.not.exist
31
+ new_dir.should.exist
32
+ end
33
+
34
+ it 'should update current base directory to specific directory' do
35
+ old_dir = Temppath.basedir
36
+ new_dir = Pathname.new(Dir.mktmpdir("ruby-temppath-test"))
37
+ _new_dir = Temppath.update_basedir(new_dir)
38
+ old_dir.should != _new_dir
39
+ old_dir.should.not.exist
40
+ _new_dir.should.exist
41
+ _new_dir.should == new_dir
31
42
  end
32
43
 
33
44
  it 'should remove current base directory' do
@@ -106,3 +117,66 @@ describe 'Temppath' do
106
117
  ("%o" % path.stat.mode).should == "100600"
107
118
  end
108
119
  end
120
+
121
+ describe Temppath::Generator do
122
+ before do
123
+ Temppath.update_basedir
124
+ @dir = Dir.mktmpdir("ruby-temppath-generator-test")
125
+ @generator = Temppath::Generator.new(@dir)
126
+ end
127
+
128
+ it "should generate a path" do
129
+ path = @generator.create
130
+ path.should.kind_of Pathname
131
+ path.dirname.to_s.should == @dir
132
+ path.should.not.exist
133
+ end
134
+
135
+ it "should make a directory" do
136
+ path = @generator.mkdir
137
+ path.should.kind_of Pathname
138
+ path.dirname.to_s.should == @dir
139
+ path.should.exist
140
+ path.should.directory
141
+ end
142
+
143
+ it "should make a file" do
144
+ path = @generator.touch
145
+ path.should.kind_of Pathname
146
+ path.dirname.to_s.should == @dir
147
+ path.should.exist
148
+ path.should.file
149
+ end
150
+
151
+ it "should have own base directory" do
152
+ Temppath.basedir.should != @generator.basedir
153
+
154
+ dir = Dir.mktmpdir("ruby-temppath-generator-test")
155
+ generator = Temppath::Generator.new(dir)
156
+ generator.basedir.should != @generator.basedir
157
+ end
158
+
159
+ it "should make base directory with #create if it doesn't exist" do
160
+ dir = Dir.mktmpdir("ruby-temppath-generator-test")
161
+ generator = Temppath::Generator.new(dir + "create")
162
+ generator.basedir.should.not.exist
163
+ generator.create
164
+ generator.basedir.should.exist
165
+ end
166
+
167
+ it "should make base directory with #mkdir if it doesn't exist" do
168
+ dir = Dir.mktmpdir("ruby-temppath-generator-test")
169
+ generator = Temppath::Generator.new(dir + "mkdir")
170
+ generator.basedir.should.not.exist
171
+ generator.create
172
+ generator.basedir.should.exist
173
+ end
174
+
175
+ it "should make base directory with #touch if it doesn't exist" do
176
+ dir = Dir.mktmpdir("ruby-temppath-generator-test")
177
+ generator = Temppath::Generator.new(dir + "touch")
178
+ generator.basedir.should.not.exist
179
+ generator.create
180
+ generator.basedir.should.exist
181
+ end
182
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: temppath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-22 00:00:00.000000000 Z
12
+ date: 2013-10-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: uuidtools
@@ -123,7 +123,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
123
123
  version: '0'
124
124
  segments:
125
125
  - 0
126
- hash: -3285866796712180964
126
+ hash: 1725793384501661700
127
127
  required_rubygems_version: !ruby/object:Gem::Requirement
128
128
  none: false
129
129
  requirements:
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  segments:
134
134
  - 0
135
- hash: -3285866796712180964
135
+ hash: 1725793384501661700
136
136
  requirements: []
137
137
  rubyforge_project:
138
138
  rubygems_version: 1.8.25