pathname2 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,3 +1,7 @@
1
+ == 1.2.0 - 29-Aug-2005
2
+ * Added the 'ascend' and 'descend' methods.
3
+ * Added corresponding test suite additions.
4
+
1
5
  == 1.1.0 - 13-Jul-2005
2
6
  * Added the 'find' facade.
3
7
 
@@ -69,7 +69,7 @@ class Pathname < String
69
69
  Win32API.new("shlwapi", "PathRemoveBackslash", "P", "P")
70
70
  end
71
71
 
72
- VERSION = "1.1.0"
72
+ VERSION = "1.2.0"
73
73
  MAX_PATH = 260
74
74
 
75
75
  # Creates and returns a new Pathname object.
@@ -143,6 +143,54 @@ class Pathname < String
143
143
  self.to_a.each{ |element| yield element }
144
144
  end
145
145
 
146
+ # Yields each component of the path, concatenating the next component on
147
+ # each iteration, as a new Pathname object, starting with the root path.
148
+ #
149
+ # For example, if the path is "/foo/bar/baz", then "/" would be yielded
150
+ # on the first iteration, "/foo" on the second, "/foo/bar on the third and
151
+ # finally "/foo/bar/baz".
152
+ def descend
153
+ if PLATFORM.match("mswin")
154
+ path = ""
155
+ else
156
+ path = absolute? ? root : ""
157
+ end
158
+
159
+ # Yield the root directory if an absolute path (and not Windows)
160
+ unless PLATFORM.match("mswin")
161
+ yield root if absolute?
162
+ end
163
+
164
+ self.each{ |element|
165
+ path << element << @sep
166
+ yield Pathname.new(path.chop)
167
+ }
168
+ end
169
+
170
+ # Yields the path, minus one component on each iteration, as a new
171
+ # Pathname object, ending with the root path.
172
+ #
173
+ # For example, if the path is "/foo/bar/baz", then "/foo/bar/baz" would
174
+ # be yielded on the first iteration, "/foo/bar" on the second, "/foo" on
175
+ # the third, and finall "/".
176
+ def ascend
177
+ n = self.to_a.length
178
+
179
+ while n > 0
180
+ path = self.to_a[0..n-1].join(@sep)
181
+ if self.absolute?
182
+ path = root << path unless PLATFORM.match("mswin")
183
+ end
184
+ yield Pathname.new(path)
185
+ n -= 1
186
+ end
187
+
188
+ # Yield the root directory if an absolute path (and not Windows)
189
+ unless PLATFORM.match("mswin")
190
+ yield root if absolute?
191
+ end
192
+ end
193
+
146
194
  # Returns the root directory of the path, or '.' if there is no root
147
195
  # directory.
148
196
  #
@@ -12,7 +12,8 @@ require "test/unit"
12
12
 
13
13
  class TC_Pathname < Test::Unit::TestCase
14
14
  def setup
15
- @path = Pathname.new("/usr/local/bin")
15
+ @abs_path = Pathname.new("/usr/local/bin")
16
+ @rel_path = Pathname.new("usr/local/bin")
16
17
  end
17
18
 
18
19
  # Convenience method for test_plus
@@ -32,21 +33,59 @@ class TC_Pathname < Test::Unit::TestCase
32
33
  end
33
34
 
34
35
  def test_version
35
- assert_equal("1.1.0", Pathname::VERSION)
36
+ assert_equal("1.2.0", Pathname::VERSION)
37
+ end
38
+
39
+ def test_ascend
40
+ assert_respond_to(@abs_path, :ascend)
41
+ assert_nothing_raised{ @abs_path.ascend{} }
42
+
43
+ abs_array = []
44
+ rel_array = []
45
+ @abs_path.ascend{ |path| abs_array.push(path) }
46
+ @rel_path.ascend{ |path| rel_array.push(path) }
47
+
48
+ assert_equal("/usr/local/bin", abs_array[0])
49
+ assert_equal("/usr/local", abs_array[1])
50
+ assert_equal("/usr", abs_array[2])
51
+ assert_equal("/", abs_array[3])
52
+
53
+ assert_equal("usr/local/bin", rel_array[0])
54
+ assert_equal("usr/local", rel_array[1])
55
+ assert_equal("usr", rel_array[2])
56
+ end
57
+
58
+ def test_descend
59
+ assert_respond_to(@abs_path, :descend)
60
+ assert_nothing_raised{ @abs_path.descend{} }
61
+
62
+ abs_array = []
63
+ rel_array = []
64
+ @abs_path.descend{ |path| abs_array.push(path) }
65
+ @rel_path.descend{ |path| rel_array.push(path) }
66
+
67
+ assert_equal("/", abs_array[0])
68
+ assert_equal("/usr", abs_array[1])
69
+ assert_equal("/usr/local", abs_array[2])
70
+ assert_equal("/usr/local/bin", abs_array[3])
71
+
72
+ assert_equal("usr", rel_array[0])
73
+ assert_equal("usr/local", rel_array[1])
74
+ assert_equal("usr/local/bin", rel_array[2])
36
75
  end
37
76
 
38
77
  def test_unc
39
- assert_raises(NotImplementedError){ @path.unc? }
78
+ assert_raises(NotImplementedError){ @abs_path.unc? }
40
79
  end
41
80
 
42
81
  def test_enumerable
43
- assert_respond_to(@path, :each)
82
+ assert_respond_to(@abs_path, :each)
44
83
  end
45
84
 
46
85
  def test_root
47
- assert_respond_to(@path, :root)
48
- assert_nothing_raised{ @path.root }
49
- assert_equal("/", @path.root)
86
+ assert_respond_to(@abs_path, :root)
87
+ assert_nothing_raised{ @abs_path.root }
88
+ assert_equal("/", @abs_path.root)
50
89
 
51
90
  path1 = Pathname.new("foo")
52
91
  path2 = Pathname.new("foo/bar/baz")
@@ -55,8 +94,8 @@ class TC_Pathname < Test::Unit::TestCase
55
94
  end
56
95
 
57
96
  def test_root?
58
- assert_respond_to(@path, :root?)
59
- assert_nothing_raised{ @path.root? }
97
+ assert_respond_to(@abs_path, :root?)
98
+ assert_nothing_raised{ @abs_path.root? }
60
99
 
61
100
  path1 = Pathname.new("/")
62
101
  path2 = Pathname.new("a")
@@ -65,8 +104,8 @@ class TC_Pathname < Test::Unit::TestCase
65
104
  end
66
105
 
67
106
  def test_absolute
68
- assert_respond_to(@path, :absolute?)
69
- assert_nothing_raised{ @path.absolute? }
107
+ assert_respond_to(@abs_path, :absolute?)
108
+ assert_nothing_raised{ @abs_path.absolute? }
70
109
  assert_equal(true, Pathname.new("/usr/bin/ruby").absolute?)
71
110
  assert_equal(false, Pathname.new("foo").absolute?)
72
111
  assert_equal(false, Pathname.new("foo/bar").absolute?)
@@ -74,8 +113,8 @@ class TC_Pathname < Test::Unit::TestCase
74
113
  end
75
114
 
76
115
  def test_relative
77
- assert_respond_to(@path, :relative?)
78
- assert_nothing_raised{ @path.relative? }
116
+ assert_respond_to(@abs_path, :relative?)
117
+ assert_nothing_raised{ @abs_path.relative? }
79
118
  assert_equal(false, Pathname.new("/usr/bin/ruby").relative?)
80
119
  assert_equal(true, Pathname.new("foo").relative?)
81
120
  assert_equal(true, Pathname.new("foo/bar").relative?)
@@ -83,14 +122,14 @@ class TC_Pathname < Test::Unit::TestCase
83
122
  end
84
123
 
85
124
  def test_to_a
86
- assert_respond_to(@path, :to_a)
87
- assert_nothing_raised{ @path.to_a }
88
- assert_kind_of(Array, @path.to_a)
89
- assert_equal(%w/usr local bin/, @path.to_a)
125
+ assert_respond_to(@abs_path, :to_a)
126
+ assert_nothing_raised{ @abs_path.to_a }
127
+ assert_kind_of(Array, @abs_path.to_a)
128
+ assert_equal(%w/usr local bin/, @abs_path.to_a)
90
129
  end
91
130
 
92
131
  def test_spaceship_operator
93
- assert_respond_to(@path, :<=>)
132
+ assert_respond_to(@abs_path, :<=>)
94
133
 
95
134
  assert_pathname_cmp( 0, "/foo/bar", "/foo/bar")
96
135
  assert_pathname_cmp(-1, "/foo/bar", "/foo/zap")
@@ -100,7 +139,7 @@ class TC_Pathname < Test::Unit::TestCase
100
139
  end
101
140
 
102
141
  def test_plus_operator
103
- assert_respond_to(@path, :+)
142
+ assert_respond_to(@abs_path, :+)
104
143
 
105
144
  # Standard stuff
106
145
  assert_pathname_plus("/foo/bar", "/foo", "bar")
@@ -146,21 +185,21 @@ class TC_Pathname < Test::Unit::TestCase
146
185
  end
147
186
 
148
187
  def test_facade_io
149
- assert_respond_to(@path, :foreach)
150
- assert_respond_to(@path, :read)
151
- assert_respond_to(@path, :readlines)
152
- assert_respond_to(@path, :sysopen)
188
+ assert_respond_to(@abs_path, :foreach)
189
+ assert_respond_to(@abs_path, :read)
190
+ assert_respond_to(@abs_path, :readlines)
191
+ assert_respond_to(@abs_path, :sysopen)
153
192
  end
154
193
 
155
194
  def test_facade_file
156
195
  File.methods(false).each{ |method|
157
- assert_respond_to(@path, method.to_sym)
196
+ assert_respond_to(@abs_path, method.to_sym)
158
197
  }
159
198
  end
160
199
 
161
200
  def test_facade_dir
162
201
  Dir.methods(false).each{ |method|
163
- assert_respond_to(@path, method.to_sym)
202
+ assert_respond_to(@abs_path, method.to_sym)
164
203
  }
165
204
  end
166
205
 
@@ -172,13 +211,13 @@ class TC_Pathname < Test::Unit::TestCase
172
211
  methods.delete("identical?")
173
212
 
174
213
  methods.each{ |method|
175
- assert_respond_to(@path, method.to_sym)
214
+ assert_respond_to(@abs_path, method.to_sym)
176
215
  }
177
216
  end
178
217
 
179
218
  def test_facade_find
180
- assert_respond_to(@path, :find)
181
- assert_nothing_raised{ @path.find{} }
219
+ assert_respond_to(@abs_path, :find)
220
+ assert_nothing_raised{ @abs_path.find{} }
182
221
 
183
222
  Pathname.new(Dir.pwd).find{ |f|
184
223
  Find.prune if f.match("CVS")
@@ -187,6 +226,6 @@ class TC_Pathname < Test::Unit::TestCase
187
226
  end
188
227
 
189
228
  def teardown
190
- @path = nil
229
+ @abs_path = nil
191
230
  end
192
231
  end
@@ -21,10 +21,13 @@ class TC_Pathname < Test::Unit::TestCase
21
21
  @ypath = Pathname.new("\\\\foo")
22
22
  @zpath = Pathname.new("\\\\")
23
23
  @epath = Pathname.new("")
24
+
25
+ @abs_array = []
26
+ @rel_array = []
24
27
  end
25
28
 
26
29
  def test_version
27
- assert_equal("1.1.0", Pathname::VERSION)
30
+ assert_equal("1.2.0", Pathname::VERSION)
28
31
  end
29
32
 
30
33
  # Convenience method for test_plus
@@ -48,6 +51,40 @@ class TC_Pathname < Test::Unit::TestCase
48
51
  assert_nothing_raised{ @fpath.each{ |e| } }
49
52
  end
50
53
 
54
+ def test_descend
55
+ assert_respond_to(@bpath, :descend)
56
+ assert_nothing_raised{ @bpath.descend{} }
57
+
58
+ @bpath.descend{ |path| @abs_array.push(path) }
59
+ @npath.descend{ |path| @rel_array.push(path) }
60
+
61
+ assert_equal("C:", @abs_array[0])
62
+ assert_equal("C:\\Program Files", @abs_array[1])
63
+ assert_equal("C:\\Program Files\\Windows NT", @abs_array[2])
64
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[3])
65
+
66
+ assert_equal("foo", @rel_array[0])
67
+ assert_equal("foo\\bar", @rel_array[1])
68
+ assert_equal("foo\\bar\\baz", @rel_array[2])
69
+ end
70
+
71
+ def test_ascend
72
+ assert_respond_to(@bpath, :ascend)
73
+ assert_nothing_raised{ @bpath.ascend{} }
74
+
75
+ @bpath.ascend{ |path| @abs_array.push(path) }
76
+ @npath.ascend{ |path| @rel_array.push(path) }
77
+
78
+ assert_equal("C:\\Program Files\\Windows NT\\Accessories", @abs_array[0])
79
+ assert_equal("C:\\Program Files\\Windows NT", @abs_array[1])
80
+ assert_equal("C:\\Program Files", @abs_array[2])
81
+ assert_equal("C:", @abs_array[3])
82
+
83
+ assert_equal("foo\\bar\\baz", @rel_array[0])
84
+ assert_equal("foo\\bar", @rel_array[1])
85
+ assert_equal("foo", @rel_array[2])
86
+ end
87
+
51
88
  # Fails due to PathIsURL for some reason
52
89
  #def test_immutability
53
90
  # path = "C:\\Program Files\\foo\\bar".freeze
@@ -233,5 +270,8 @@ class TC_Pathname < Test::Unit::TestCase
233
270
  @ypath = nil
234
271
  @zpath = nil
235
272
  @epath = nil
273
+
274
+ @abs_array.clear
275
+ @rel_array.clear
236
276
  end
237
277
  end
metadata CHANGED
@@ -1,10 +1,10 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.10
2
+ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: pathname2
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.1.0
7
- date: 2005-07-13
6
+ version: 1.2.0
7
+ date: 2005-08-30 00:00:00 -06:00
8
8
  summary: An alternate implementation of the Pathname class
9
9
  require_paths:
10
10
  - lib
@@ -24,6 +24,8 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
24
24
  version: 0.0.0
25
25
  version:
26
26
  platform: ruby
27
+ signing_key:
28
+ cert_chain:
27
29
  authors:
28
30
  - Daniel J. Berger
29
31
  files: