chrislloyd-fancypath 0.5.10 → 0.5.11
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/Rakefile +3 -3
- data/lib/fancypath.rb +33 -25
- data/spec/fancypath_spec.rb +40 -40
- metadata +3 -3
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'date'
|
|
5
5
|
require 'spec/rake/spectask'
|
6
6
|
|
7
7
|
GEM = "fancypath"
|
8
|
-
GEM_VERSION = "0.5.
|
8
|
+
GEM_VERSION = "0.5.11"
|
9
9
|
AUTHORS = ["Myles Byrne", "Chris Lloyd"]
|
10
10
|
EMAIL = "myles@ducknewmedia.com"
|
11
11
|
HOMEPAGE = "http://ducknewmedia.com/fancypath"
|
@@ -22,10 +22,10 @@ spec = Gem::Specification.new do |s|
|
|
22
22
|
s.authors = AUTHORS
|
23
23
|
s.email = EMAIL
|
24
24
|
s.homepage = HOMEPAGE
|
25
|
-
|
25
|
+
|
26
26
|
# Uncomment this to add a dependency
|
27
27
|
# s.add_dependency "foo"
|
28
|
-
|
28
|
+
|
29
29
|
s.require_path = 'lib'
|
30
30
|
s.autorequire = GEM
|
31
31
|
s.files = %w(LICENSE README.rdoc Rakefile) + Dir.glob("{lib,spec}/**/*")
|
data/lib/fancypath.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'pathname'
|
2
2
|
|
3
3
|
class Pathname
|
4
|
-
|
4
|
+
|
5
5
|
def to_fancypath
|
6
6
|
Fancypath.new(self)
|
7
7
|
end
|
@@ -11,7 +11,7 @@ class Pathname
|
|
11
11
|
end
|
12
12
|
|
13
13
|
class String
|
14
|
-
|
14
|
+
|
15
15
|
def to_fancypath
|
16
16
|
Fancypath.new(self)
|
17
17
|
end
|
@@ -22,14 +22,21 @@ end
|
|
22
22
|
|
23
23
|
class Fancypath < Pathname
|
24
24
|
# methods are chainable and do what you think they do
|
25
|
-
|
25
|
+
|
26
|
+
alias_method :dir, :dirname
|
27
|
+
alias_method :directory, :dirname
|
28
|
+
|
29
|
+
alias_method :expand, :expand_path
|
30
|
+
alias_method :abs, :expand_path
|
31
|
+
alias_method :absolute, :expand_path
|
32
|
+
|
26
33
|
alias_method :exists?, :exist?
|
27
34
|
alias_method :rename_to, :rename
|
28
|
-
|
35
|
+
|
29
36
|
def join(path)
|
30
37
|
super(path).to_path
|
31
38
|
end
|
32
|
-
|
39
|
+
|
33
40
|
alias_method :/, :join
|
34
41
|
|
35
42
|
# make file
|
@@ -37,19 +44,19 @@ class Fancypath < Pathname
|
|
37
44
|
FileUtils.touch self.to_s
|
38
45
|
self
|
39
46
|
end
|
40
|
-
|
47
|
+
|
41
48
|
def create_dir
|
42
49
|
mkpath unless exist?
|
43
50
|
self
|
44
51
|
end
|
45
|
-
|
52
|
+
|
46
53
|
alias_method :create, :create_dir
|
47
|
-
|
54
|
+
|
48
55
|
def copy(dest)
|
49
56
|
FileUtils.cp(self, dest)
|
50
57
|
self
|
51
58
|
end
|
52
|
-
|
59
|
+
|
53
60
|
alias_method :cp, :copy
|
54
61
|
|
55
62
|
# file or dir
|
@@ -57,24 +64,24 @@ class Fancypath < Pathname
|
|
57
64
|
directory? ? rmtree : delete if exist?
|
58
65
|
self
|
59
66
|
end
|
60
|
-
|
67
|
+
|
61
68
|
alias_method :rm, :remove
|
62
69
|
def write(contents, mode='wb')
|
63
70
|
dirname.create
|
64
71
|
open(mode) { |f| f.write contents }
|
65
72
|
self
|
66
73
|
end
|
67
|
-
|
74
|
+
|
68
75
|
def append(contents)
|
69
76
|
write(contents,'a+')
|
70
77
|
self
|
71
78
|
end
|
72
|
-
|
79
|
+
|
73
80
|
def move(dest)
|
74
81
|
self.rename(dest)
|
75
82
|
dest.to_path
|
76
83
|
end
|
77
|
-
|
84
|
+
|
78
85
|
def tail(bytes)
|
79
86
|
return self.read if self.size < bytes
|
80
87
|
open('r') do |f|
|
@@ -84,52 +91,53 @@ class Fancypath < Pathname
|
|
84
91
|
end
|
85
92
|
|
86
93
|
alias_method :mv, :move
|
87
|
-
|
94
|
+
|
88
95
|
def set_extension(ext)
|
89
96
|
"#{without_extension}.#{ext}".to_path
|
90
97
|
end
|
98
|
+
|
91
99
|
alias_method :change_extension, :set_extension
|
92
|
-
|
100
|
+
|
93
101
|
def without_extension
|
94
102
|
to_s[/^ (.+?) (\. ([^\.]+))? $/x, 1].to_path
|
95
103
|
end
|
96
|
-
|
104
|
+
|
97
105
|
def has_extension?(ext)
|
98
106
|
!!(self.to_s =~ /\.#{ext}$/)
|
99
107
|
end
|
100
|
-
|
108
|
+
|
101
109
|
def parent
|
102
110
|
super.to_path
|
103
111
|
end
|
104
|
-
|
112
|
+
|
105
113
|
alias_method :all_children, :children
|
106
|
-
|
114
|
+
|
107
115
|
def children
|
108
116
|
super.reject { |c| c.basename.to_s =~ /^\./ }
|
109
117
|
end
|
110
|
-
|
118
|
+
|
111
119
|
# only takes sym atm
|
112
120
|
def select(*args)
|
113
121
|
return args.map { |arg| select(arg) }.flatten.uniq if args.size > 1
|
114
|
-
|
122
|
+
|
115
123
|
case arg = args.first
|
116
124
|
when Symbol
|
117
125
|
Dir["#{self}/*.#{arg}"].map { |p| self.class.new(p) }
|
118
|
-
when Regexp
|
126
|
+
when Regexp
|
119
127
|
children.select { |child| child.to_s =~ arg }
|
120
128
|
else
|
121
129
|
Dir["#{self}/#{arg}"].map { |p| self.class.new(p) }
|
122
130
|
end
|
123
131
|
end
|
124
|
-
|
132
|
+
|
125
133
|
def inspect
|
126
134
|
super.sub('Pathname','Fancypath')
|
127
135
|
end
|
128
|
-
|
136
|
+
|
129
137
|
def to_path
|
130
138
|
self
|
131
139
|
end
|
132
|
-
|
140
|
+
|
133
141
|
end
|
134
142
|
|
135
143
|
def Fancypath(path); Fancypath.new(path) end
|
data/spec/fancypath_spec.rb
CHANGED
@@ -11,80 +11,80 @@ end
|
|
11
11
|
after { TMP_DIR.rmtree }
|
12
12
|
|
13
13
|
describe '#join', 'aliased to #/' do
|
14
|
-
|
15
|
-
it('returns a Fancypath') { (@dir/'somefile').class.should == Fancypath }
|
14
|
+
|
15
|
+
it('returns a Fancypath') { (@dir/'somefile').class.should == Fancypath }
|
16
16
|
it('joins paths') { (@dir/'somefile').to_s.should =~ /\/somefile$/ }
|
17
|
-
|
17
|
+
|
18
18
|
end
|
19
19
|
|
20
20
|
describe '#parent' do
|
21
|
-
|
21
|
+
|
22
22
|
it('returns parent') { @file.parent.should == TMP_DIR.to_path }
|
23
23
|
it('returns Fancypath') { @file.parent.should be_instance_of(Fancypath) }
|
24
|
-
|
24
|
+
|
25
25
|
end
|
26
26
|
|
27
27
|
describe '#touch', 'file does not exist' do
|
28
|
-
|
29
|
-
it('returns self') { @file.touch.should == @file }
|
28
|
+
|
29
|
+
it('returns self') { @file.touch.should == @file }
|
30
30
|
it('returns a Fancypath') { @file.touch.should be_instance_of(Fancypath) }
|
31
31
|
it('creates file') { @file.touch.should be_file }
|
32
|
-
|
32
|
+
|
33
33
|
end
|
34
34
|
|
35
35
|
describe '#create', 'dir does not exist' do
|
36
|
-
|
36
|
+
|
37
37
|
it('returns self') { @dir.create.should == @dir }
|
38
38
|
it('returns a Fancypath') { @dir.create.should be_instance_of(Fancypath) }
|
39
39
|
it('creates directory') { @dir.create.should be_directory }
|
40
|
-
|
40
|
+
|
41
41
|
end
|
42
42
|
|
43
43
|
describe '#remove' do
|
44
|
-
|
44
|
+
|
45
45
|
it('returns self') { @file.remove.should == @file }
|
46
46
|
it('returns a Fancypath') { @file.remove.should be_instance_of(Fancypath) }
|
47
|
-
it('removes file') { @file.touch.remove.should_not exist }
|
47
|
+
it('removes file') { @file.touch.remove.should_not exist }
|
48
48
|
it('removes directory') { @dir.create.remove.should_not exist }
|
49
|
-
|
49
|
+
|
50
50
|
end
|
51
51
|
|
52
52
|
describe '#write' do
|
53
|
-
|
53
|
+
|
54
54
|
it('returns self') { @file.write('').should == @file }
|
55
55
|
it('returns a Fancypath') { @file.write('').should be_instance_of(Fancypath) }
|
56
56
|
it('writes contents to file') { @file.write('test').read.should == 'test' }
|
57
|
-
|
57
|
+
|
58
58
|
end
|
59
59
|
|
60
60
|
describe '#copy' do
|
61
|
-
|
61
|
+
|
62
62
|
before { @file.touch }
|
63
63
|
it('returns a Fancypath') { @file.copy(TMP_DIR/'foo').should be_instance_of(Fancypath) }
|
64
64
|
it('creates a new file') { @file.copy(TMP_DIR/'foo').should exist }
|
65
65
|
it('keeps the original') { @file.copy(TMP_DIR/'foo'); @file.should exist }
|
66
66
|
it('copies the contents') { @file.copy(TMP_DIR/'foo').read.should == @file.read }
|
67
|
-
|
67
|
+
|
68
68
|
end
|
69
69
|
|
70
70
|
describe '#set_extension' do
|
71
|
-
|
71
|
+
|
72
72
|
example "file without extension" do
|
73
73
|
Fancypath('/tmp/foo').set_extension('rb').should == Fancypath('/tmp/foo.rb')
|
74
74
|
end
|
75
|
-
|
75
|
+
|
76
76
|
example "single extension" do
|
77
77
|
Fancypath('/tmp/foo.py').set_extension('rb').should == Fancypath('/tmp/foo.rb')
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
example "multi extension" do
|
81
81
|
Fancypath('/tmp/foo.py.z').set_extension('rb').should == Fancypath('/tmp/foo.py.rb')
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
end
|
85
85
|
|
86
86
|
describe '#move' do
|
87
|
-
|
87
|
+
|
88
88
|
example "destination has the file contents, source does not exist" do
|
89
89
|
@file.write('foo')
|
90
90
|
dest = TMP_DIR/'newfile'
|
@@ -92,65 +92,65 @@ describe '#move' do
|
|
92
92
|
@file.should_not exist
|
93
93
|
dest.read.should == 'foo'
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
end
|
97
97
|
|
98
98
|
describe '#has_extension?' do
|
99
|
-
|
99
|
+
|
100
100
|
example do
|
101
101
|
Fancypath('/tmp/foo.bar').has_extension?('bar').should be_true
|
102
102
|
end
|
103
|
-
|
103
|
+
|
104
104
|
example do
|
105
105
|
Fancypath('/tmp/foo.bar').has_extension?('foo').should be_false
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
end
|
109
109
|
|
110
110
|
describe '#select' do
|
111
|
-
|
111
|
+
|
112
112
|
example 'with symbol' do
|
113
113
|
@dir.create_dir
|
114
114
|
%W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
|
115
|
-
|
115
|
+
|
116
116
|
@dir.select(:jpg).should == [@dir/'a.jpg', @dir/'b.jpg']
|
117
117
|
end
|
118
|
-
|
118
|
+
|
119
119
|
example 'with glob' do
|
120
120
|
@dir.create_dir
|
121
121
|
%W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
|
122
|
-
|
122
|
+
|
123
123
|
@dir.select("*.jpg").should == [@dir/'a.jpg', @dir/'b.jpg']
|
124
124
|
end
|
125
|
-
|
125
|
+
|
126
126
|
example 'with regex' do
|
127
127
|
@dir.create_dir
|
128
128
|
%W(a.jpg b.jpg c.gif 1.jpg).each { |f| (@dir/f).touch }
|
129
|
-
|
129
|
+
|
130
130
|
@dir.select(/[^\d]\.(jpg|gif)$/).should == [@dir/'a.jpg', @dir/'b.jpg', @dir/'c.gif']
|
131
131
|
end
|
132
|
-
|
132
|
+
|
133
133
|
example 'with multiple args' do
|
134
134
|
@dir.create_dir
|
135
135
|
%W(a.jpg b.jpg c.gif).each { |f| (@dir/f).touch }
|
136
|
-
|
136
|
+
|
137
137
|
@dir.select(:jpg, '*.gif').should == [@dir/'a.jpg', @dir/'b.jpg', @dir/'c.gif']
|
138
138
|
end
|
139
|
-
|
139
|
+
|
140
140
|
# todo: with block
|
141
|
-
|
141
|
+
|
142
142
|
end
|
143
143
|
|
144
144
|
end #/Fancypath
|
145
145
|
|
146
146
|
describe "String#to_path" do
|
147
|
-
|
147
|
+
|
148
148
|
it('returns a Fancypath') { 'test'.to_path.should be_instance_of(Fancypath) }
|
149
|
-
|
149
|
+
|
150
150
|
end
|
151
151
|
|
152
152
|
describe "Pathname#to_path" do
|
153
|
-
|
153
|
+
|
154
154
|
it('returns a Fancypath') { Fancypath.new('/').to_path.should be_instance_of(Fancypath) }
|
155
|
-
|
155
|
+
|
156
156
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chrislloyd-fancypath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Myles Byrne
|
@@ -10,7 +10,7 @@ autorequire: fancypath
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-05-01 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
@@ -54,7 +54,7 @@ requirements: []
|
|
54
54
|
rubyforge_project:
|
55
55
|
rubygems_version: 1.2.0
|
56
56
|
signing_key:
|
57
|
-
specification_version:
|
57
|
+
specification_version: 3
|
58
58
|
summary: Extensions for the Pathname library.
|
59
59
|
test_files: []
|
60
60
|
|