temppath 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,3 +17,4 @@ test/version_tmp
17
17
  tmp
18
18
  html
19
19
  gems
20
+ vendor
data/.simplecov CHANGED
@@ -4,4 +4,5 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
4
  SimpleCov::Formatter::HTMLFormatter,
5
5
  Coveralls::SimpleCov::Formatter
6
6
  ]
7
+ SimpleCov.command_name 'bacon'
7
8
  SimpleCov.start {add_filter 'test'}
data/README.md CHANGED
@@ -13,10 +13,30 @@ deleted when Ruby exits.
13
13
 
14
14
  ## Usage
15
15
 
16
+ #### Create a path
17
+
16
18
  ```ruby
17
19
  path = Temppath.create
18
20
  #=> #<Pathname:/tmp/ruby-temppath-20130407-5775-w5k77l/f41bd6c5-fc99-4b7a-8f68-95b7ae4a6b22>
19
21
  path.exist? #=> false
22
+ path.open("w")
23
+ "%o" % path.stat.mode #=> "100600" (default permission 0600)
24
+ ```
25
+
26
+ #### Create a directory
27
+
28
+ ```ruby
29
+ path = Temppath.mkdir
30
+ path.directory? #=> true
31
+ "%o" % path.stat.mode #=> "40700"
32
+ ```
33
+
34
+ #### Create an empty file
35
+
36
+ ```ruby
37
+ path = Temppath.touch
38
+ path.file? #=> true
39
+ "%o" % path.stat.mode #=> "100600"
20
40
  ```
21
41
 
22
42
  ## Documentation
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require "bundler/gem_tasks"
2
2
 
3
3
  desc 'Test specs'
4
4
  task 'test' do
5
- sh "bundle exec bacon -a"
5
+ sh "bundle exec bacon -a -rsimplecov"
6
6
  end
7
7
 
8
8
  desc 'Generate API document'
@@ -8,15 +8,74 @@ require 'fileutils'
8
8
  # that this library generates Pathname objects with no files and filenames are
9
9
  # based on UUID. Files in paths generated by this are deleted when Ruby exits.
10
10
  #
11
- # @example
11
+ # @example Create a temporary path
12
12
  # path = Temppath.create
13
13
  # #=> #<Pathname:/tmp/ruby-temppath-20130407-5775-w5k77l/f41bd6c5-fc99-4b7a-8f68-95b7ae4a6b22>
14
14
  # path.exist? #=> false
15
+ # path.open("w")
16
+ # "%o" % path.stat.mode #=> "100600" (default permission 0600)
17
+ # @example Touch a temporary file
18
+ # path = Temppath.touch
19
+ # path.file? #=> true
20
+ # "%o" % path.stat.mode #=> "100600"
21
+ # @example Create a temporary directory
22
+ # path = Temppath.mkdir
23
+ # path.directory? #=> true
24
+ # "%o" % path.stat.mode #=> "40700"
15
25
  module Temppath
26
+ # OriginalMethodHolder keeps some original methods of Pathname.
27
+ module OriginalMethodHolder
28
+ # @api private
29
+ def self.extended(obj)
30
+ # keep original methods
31
+ obj.instance_exec do
32
+ class << self
33
+ alias :orig_open :open
34
+ alias :orig_mkdir :mkdir
35
+ alias :orig_mkpath :mkpath
36
+ alias :orig_sysopen :sysopen
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ # SecurePermissionMethods provides some Pathname methods with secure default
43
+ # permission.
44
+ module SecurePermissionMethods
45
+ # Open file with permission 0600 by default. Otherwise, this is same as
46
+ # Pathname#open.
47
+ def open(*args, &b)
48
+ args[1] = 0600 unless args[1]
49
+ orig_open(*args, &b)
50
+ end
51
+
52
+ # Make a directory with permission 0700 by defautl. Otherwise, this is same
53
+ # as Pathname#mkdir.
54
+ def mkdir(*args)
55
+ args[0] = 0700 unless args[0]
56
+ orig_mkdir(*args)
57
+ end
58
+
59
+ # Make paths with permission 0700 by defautl. Otherwise, this is same as
60
+ # Pathname#mkpath.
61
+ def mkpath(*args)
62
+ args[0] = {} unless args[0]
63
+ args[0] = args[0].merge(mode: 0700) unless args[0][:mode]
64
+ FileUtils.mkpath(self.to_s, *args)
65
+ end
66
+
67
+ # Open file with permission 0600 by default. Otherwise, this is same as
68
+ # Pathname#sysopen.
69
+ def sysopen(*args)
70
+ args[1] = 0600 unless args[1]
71
+ orig_sysopen(*args)
72
+ end
73
+ end
74
+
16
75
  class << self
17
76
  # @return [Pathname]
18
- # defalut temporary directory for paths created by Temppath
19
- attr_reader :dir
77
+ # defalut base directory for paths created by Temppath
78
+ attr_reader :basedir
20
79
 
21
80
  # @return [Boolean]
22
81
  # true if unlink mode is enabled
@@ -24,42 +83,63 @@ module Temppath
24
83
 
25
84
  # Create a temporary path. This method creates no files.
26
85
  #
27
- # @param basename [String]
86
+ # @param option [Hash]
87
+ # @option option [String] :basename
28
88
  # prefix of filename
29
- # @param tmpdir [Pathname]
30
- # pathname of temporary directory
31
- def create(basename="", tmpdir=dir)
32
- path = Pathname.new(tmpdir) + (basename.to_s + generate_uuid)
33
- if tmpdir != dir
89
+ # @option option [Pathname] :basedir
90
+ # pathname of base directory
91
+ def create(option={})
92
+ basename = option[:basename] || ""
93
+ _basedir = option[:basedir] || basedir
94
+ path = Pathname.new(_basedir) + (basename.to_s + generate_uuid)
95
+ path.extend OriginalMethodHolder
96
+ path.extend SecurePermissionMethods
97
+ if _basedir != basedir
34
98
  Kernel.at_exit {FileUtils.remove_entry_secure(path) rescue Errno::ENOENT}
35
99
  end
36
100
  return path
37
101
  end
38
102
 
39
- # Remove curren temporary directory and create a new temporary directory and
103
+ # Create a temporary directory.
104
+ def mkdir(option={})
105
+ mode = option[:mode] || 0700
106
+ path = create(option)
107
+ path.mkdir(mode)
108
+ return path
109
+ end
110
+
111
+ # Create a empty file.
112
+ def touch(option={})
113
+ mode = option[:mode] || 0600
114
+ path = create(option)
115
+ path.open("w", mode)
116
+ return path
117
+ end
118
+
119
+ # Remove current base directory, create a new base directory, and
40
120
  # use it.
41
121
  #
42
122
  # @return [Pathname]
43
- # new temporary directory
44
- def update_tempdir
45
- remove_tempdir
46
- @dir = create_tempdir
123
+ # new base directory
124
+ def update_basedir
125
+ remove_basedir
126
+ @basedir = create_basedir
47
127
  end
48
128
 
49
129
  # Remove current temporary directory.
50
130
  #
51
131
  # @return [void]
52
- def remove_tempdir
53
- FileUtils.remove_entry_secure(@dir) if @dir.exist?
132
+ def remove_basedir
133
+ FileUtils.remove_entry_secure(@basedir) if @basedir.exist?
54
134
  end
55
135
 
56
136
  private
57
137
 
58
- # Create a new temporary directory.
138
+ # Create a new base directory.
59
139
  #
60
140
  # @return [Pathname]
61
- # temporary directory
62
- def create_tempdir
141
+ # base directory
142
+ def create_basedir
63
143
  Pathname.new(Dir.mktmpdir("ruby-temppath-"))
64
144
  end
65
145
 
@@ -72,13 +152,13 @@ module Temppath
72
152
  end
73
153
  end
74
154
 
75
- @dir = create_tempdir
155
+ @basedir = create_basedir
76
156
  @unlink = true
77
157
  end
78
158
 
79
159
  # Remove Temppath's temporary directory.
80
160
  Kernel.at_exit do
81
161
  if Temppath.unlink
82
- Temppath.remove_tempdir rescue Errno::ENOENT
162
+ Temppath.remove_basedir rescue Errno::ENOENT
83
163
  end
84
164
  end
@@ -1,4 +1,4 @@
1
1
  module Temppath
2
2
  # version of temppath gem
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
4
4
  end
@@ -1,9 +1,8 @@
1
- require 'simplecov'
2
1
  require 'temppath'
3
2
 
4
3
  describe 'Temppath' do
5
4
  before do
6
- Temppath.update_tempdir
5
+ Temppath.update_basedir
7
6
  end
8
7
 
9
8
  it 'should get temporary path' do
@@ -12,28 +11,28 @@ describe 'Temppath' do
12
11
  end
13
12
 
14
13
  it 'should get temporary path with basename' do
15
- Temppath.create("A_").basename.to_s[0,2].should == "A_"
14
+ Temppath.create(basename: "A_").basename.to_s[0,2].should == "A_"
16
15
  end
17
16
 
18
- it 'should get temporary path with tmpdir' do
19
- dir = Dir.tmpdir
20
- Temppath.create(nil, dir).dirname.should == Pathname.new(dir)
17
+ it 'should get base directory path with basedir' do
18
+ dir = Dir.mktmpdir
19
+ Temppath.create(basedir: dir).dirname.should == Pathname.new(dir)
21
20
  end
22
21
 
23
- it 'should be in the temporary directory' do
24
- Temppath.create.dirname.should == Temppath.dir
22
+ it 'should be in the base directory' do
23
+ Temppath.create.dirname.should == Temppath.basedir
25
24
  end
26
25
 
27
- it 'should update current temporary directory' do
28
- old_dir = Temppath.dir
29
- new_dir = Temppath.update_tempdir
26
+ it 'should update current base directory' do
27
+ old_dir = Temppath.basedir
28
+ new_dir = Temppath.update_basedir
30
29
  old_dir.should != new_dir
31
30
  old_dir.should.not.exist
32
31
  end
33
32
 
34
- it 'should remove current temporary directory' do
35
- dir = Temppath.dir
36
- Temppath.remove_tempdir
33
+ it 'should remove current base directory' do
34
+ dir = Temppath.basedir
35
+ Temppath.remove_basedir
37
36
  dir.should.not.exist
38
37
  end
39
38
 
@@ -46,4 +45,64 @@ describe 'Temppath' do
46
45
  Temppath.unlink.should == false
47
46
  Temppath.unlink = true
48
47
  end
48
+
49
+ it 'should create a file with permission 0600 by default' do
50
+ path = Temppath.create
51
+ path.open("w")
52
+ ("%o" % path.stat.mode).should == "100600"
53
+ end
54
+
55
+ it 'should create a file with permission 0644 explicitly' do
56
+ path = Temppath.create
57
+ path.open("w", 0644)
58
+ ("%o" % path.stat.mode).should == "100644"
59
+ end
60
+
61
+ it 'should create a directory with permission 0700 by default' do
62
+ path = Temppath.create
63
+ path.mkdir
64
+ ("%o" % path.stat.mode).should == "40700"
65
+ end
66
+
67
+ it 'should create a directory with permission 0755 explicitly' do
68
+ path = Temppath.create
69
+ path.mkdir(0755)
70
+ ("%o" % path.stat.mode).should == "40755"
71
+ end
72
+
73
+ it 'should do mkpath with permission 0700 by default' do
74
+ path = Temppath.create
75
+ path.mkpath
76
+ ("%o" % path.stat.mode).should == "40700"
77
+ end
78
+
79
+ it 'should do mkpath with permission 0755 explicitly' do
80
+ path = Temppath.create
81
+ path.mkpath(mode: 0755)
82
+ ("%o" % path.stat.mode).should == "40755"
83
+ end
84
+
85
+ it 'should do sysopen with permission 0600 by default' do
86
+ path = Temppath.create
87
+ path.sysopen("w")
88
+ ("%o" % path.stat.mode).should == "100600"
89
+ end
90
+
91
+ it 'should do sysopen with permission 0644 explicitly' do
92
+ path = Temppath.create
93
+ path.sysopen("w", 0644)
94
+ ("%o" % path.stat.mode).should == "100644"
95
+ end
96
+
97
+ it 'should make a directory' do
98
+ path = Temppath.mkdir
99
+ path.should.directory
100
+ ("%o" % path.stat.mode).should == "40700"
101
+ end
102
+
103
+ it 'should touch a file' do
104
+ path = Temppath.touch
105
+ path.should.file
106
+ ("%o" % path.stat.mode).should == "100600"
107
+ end
49
108
  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.1.1
4
+ version: 0.2.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-05-03 00:00:00.000000000 Z
12
+ date: 2013-06-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: uuidtools
@@ -121,12 +121,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
121
121
  - - ! '>='
122
122
  - !ruby/object:Gem::Version
123
123
  version: '0'
124
+ segments:
125
+ - 0
126
+ hash: -3285866796712180964
124
127
  required_rubygems_version: !ruby/object:Gem::Requirement
125
128
  none: false
126
129
  requirements:
127
130
  - - ! '>='
128
131
  - !ruby/object:Gem::Version
129
132
  version: '0'
133
+ segments:
134
+ - 0
135
+ hash: -3285866796712180964
130
136
  requirements: []
131
137
  rubyforge_project:
132
138
  rubygems_version: 1.8.25