fspath 1.0.0-darwin → 1.1.0-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/VERSION +1 -1
  2. data/fspath.gemspec +3 -5
  3. data/lib/fspath.rb +36 -0
  4. data/spec/fspath_spec.rb +60 -0
  5. metadata +4 -6
  6. data/TODO +0 -40
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/fspath.gemspec CHANGED
@@ -5,23 +5,21 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "fspath"
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
9
9
  s.platform = "darwin"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.authors = ["Ivan Kuchin"]
13
- s.date = "2011-10-29"
13
+ s.date = "2011-10-31"
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE.txt",
16
- "README.markdown",
17
- "TODO"
16
+ "README.markdown"
18
17
  ]
19
18
  s.files = [
20
19
  ".tmignore",
21
20
  "LICENSE.txt",
22
21
  "README.markdown",
23
22
  "Rakefile",
24
- "TODO",
25
23
  "VERSION",
26
24
  "fspath.gemspec",
27
25
  "lib/fspath.rb",
data/lib/fspath.rb CHANGED
@@ -1,6 +1,14 @@
1
1
  require 'pathname'
2
+ require 'tempfile'
3
+ require 'tmpdir'
2
4
 
3
5
  class FSPath < Pathname
6
+ class Tempfile < ::Tempfile
7
+ def path
8
+ FSPath.new(super)
9
+ end
10
+ end
11
+
4
12
  class << self
5
13
  # Return current user home path if called without argument.
6
14
  # If called with argument return specified user home path.
@@ -14,6 +22,34 @@ class FSPath < Pathname
14
22
  new(path).dirname.ascend
15
23
  end.inject(:&).first
16
24
  end
25
+
26
+ # Returns or yields temp file created by Tempfile.new with path returning FSPath
27
+ def temp_file(*args, &block)
28
+ args = %w[f] if args.empty?
29
+ Tempfile.open(*args, &block)
30
+ end
31
+
32
+ # Returns or yields path as FSPath of temp file created by Tempfile.new
33
+ def temp_file_path(*args)
34
+ if block_given?
35
+ temp_file(*args) do |file|
36
+ yield file.path
37
+ end
38
+ else
39
+ temp_file(*args).path
40
+ end
41
+ end
42
+
43
+ # Returns or yields FSPath with temp directory created by Dir.mktmpdir
44
+ def temp_dir(*args)
45
+ if block_given?
46
+ Dir.mktmpdir(*args) do |dir|
47
+ yield new(dir)
48
+ end
49
+ else
50
+ new(Dir.mktmpdir(*args))
51
+ end
52
+ end
17
53
  end
18
54
 
19
55
  # Join paths using File.join
data/spec/fspath_spec.rb CHANGED
@@ -34,6 +34,66 @@ describe FSPath do
34
34
  end
35
35
  end
36
36
 
37
+ describe "temp_file" do
38
+ before do
39
+ @file = mock(:file)
40
+ end
41
+
42
+ it "should return instance of FSPath::Tempfile" do
43
+ FSPath::Tempfile.stub!(:open).and_return(@file)
44
+
45
+ FSPath.temp_file.should == @file
46
+ end
47
+
48
+ it "should yield instance of FSPath::Tempfile" do
49
+ FSPath::Tempfile.stub!(:open).and_yield(@file)
50
+
51
+ yielded = nil
52
+ FSPath.temp_file{ |y| yielded = y }
53
+ yielded.should == @file
54
+ end
55
+ end
56
+
57
+ describe "temp_file_path" do
58
+ before do
59
+ @path = mock(:path)
60
+ @file = mock(:file)
61
+ @file.stub!(:path).and_return(@path)
62
+ end
63
+
64
+ it "should return FSPath with temporary path" do
65
+ FSPath::Tempfile.stub!(:new).and_return(@file)
66
+
67
+ FSPath.temp_file_path.should == @path
68
+ end
69
+
70
+ it "should yield FSPath with temporary path" do
71
+ FSPath::Tempfile.stub!(:open).and_yield(@file)
72
+
73
+ yielded = nil
74
+ FSPath.temp_file_path{ |y| yielded = y }
75
+ yielded.should == @path
76
+ end
77
+ end
78
+
79
+ describe "temp_dir" do
80
+ it "should return result of running Dir.mktmpdir as FSPath instance" do
81
+ @path = '/tmp/a/b/1'
82
+ Dir.stub!(:mktmpdir).and_return(@path)
83
+
84
+ FSPath.temp_dir.should == FSPath('/tmp/a/b/1')
85
+ end
86
+
87
+ it "should yield path yielded by Dir.mktmpdir as FSPath instance" do
88
+ @path = '/tmp/a/b/2'
89
+ Dir.stub!(:mktmpdir).and_yield(@path)
90
+
91
+ yielded = nil
92
+ FSPath.temp_dir{ |y| yielded = y }
93
+ yielded.should == FSPath('/tmp/a/b/2')
94
+ end
95
+ end
96
+
37
97
  describe "/" do
38
98
  it "should join path with string" do
39
99
  (FSPath('a') / 'b').should == FSPath('a/b')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fspath
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: darwin
12
12
  authors:
13
13
  - Ivan Kuchin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-10-29 00:00:00 Z
18
+ date: 2011-10-31 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: xattr
@@ -98,13 +98,11 @@ extensions: []
98
98
  extra_rdoc_files:
99
99
  - LICENSE.txt
100
100
  - README.markdown
101
- - TODO
102
101
  files:
103
102
  - .tmignore
104
103
  - LICENSE.txt
105
104
  - README.markdown
106
105
  - Rakefile
107
- - TODO
108
106
  - VERSION
109
107
  - fspath.gemspec
110
108
  - lib/fspath.rb
data/TODO DELETED
@@ -1,40 +0,0 @@
1
- class Pathname
2
- def non_existing
3
- dir = dirname
4
- ext = extname
5
- name = basename(ext)
6
- postfix = nil
7
- while (path = dir + "#{name}#{postfix}#{ext}").exist?
8
- postfix = postfix ? postfix.succ : '-a'
9
- end
10
- path
11
- # begin
12
- # FSPath('abc').open(Fcntl::O_WRONLY | Fcntl::O_CREAT | Fcntl::O_EXCL) do |f|
13
- # end
14
- # rescue => e
15
- # p e
16
- # end
17
-
18
- end
19
-
20
- def lock(options = {}, &block)
21
- File.lock(@path, options, &block)
22
- # class File
23
- # def self.lock(path, options = {})
24
- # if lock = File.open(path, options[:mode] || 'r')
25
- # begin
26
- # if lock.flock(options[:no_block] ? (File::LOCK_EX | File::LOCK_NB) : (File::LOCK_EX))
27
- # yield
28
- # end
29
- # ensure
30
- # lock.flock(File::LOCK_UN)
31
- # lock.close
32
- # end
33
- # end
34
- # end
35
- # end
36
- # Fcntl::O_RDONLY | Fcntl::O_CREAT
37
- # Fcntl::O_SHLOCK
38
- # Fcntl::O_EXLOCK
39
- end
40
- end