pathname2 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES CHANGED
@@ -1,13 +1,19 @@
1
- == 1.2.1 - 1-Sep-2005
2
- * Bug fix for the ascend and descend methods wrt Windows and UNC paths.
3
- * More tests added for ascend and descend methods.
4
-
5
- == 1.2.0 - 29-Aug-2005
6
- * Added the 'ascend' and 'descend' methods.
7
- * Added corresponding test suite additions.
8
-
9
- == 1.1.0 - 13-Jul-2005
10
- * Added the 'find' facade.
11
-
12
- == 1.0.0 - 11-Jun-2005
13
- * Initial release
1
+ == 1.3.0 - 28-Oct-2005
2
+ * Added the short_path and long_path methods for MS Windows.
3
+ * Optimization for the '+' method on Unix.
4
+ * Added some examples under the 'examples' directory.
5
+ * More tests added and some minor changes to the test suite in general.
6
+
7
+ == 1.2.1 - 1-Sep-2005
8
+ * Bug fix for the ascend and descend methods wrt Windows and UNC paths.
9
+ * More tests added for ascend and descend methods.
10
+
11
+ == 1.2.0 - 29-Aug-2005
12
+ * Added the 'ascend' and 'descend' methods.
13
+ * Added corresponding test suite additions.
14
+
15
+ == 1.1.0 - 13-Jul-2005
16
+ * Added the 'find' facade.
17
+
18
+ == 1.0.0 - 11-Jun-2005
19
+ * Initial release
data/lib/pathname2.rb CHANGED
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # == Usage
13
13
  #
14
- # require "pathname"
14
+ # require "pathname2"
15
15
  #
16
16
  # # Unix
17
17
  # path1 = Pathname.new("/foo/bar/baz")
@@ -58,6 +58,12 @@ class Pathname < String
58
58
  @@PathIsRelative = Win32API.new("shlwapi","PathIsRelative","P","L")
59
59
  @@PathFileExists = Win32API.new("shlwapi","PathFileExists","P","L")
60
60
  @@PathUndecorate = Win32API.new("shlwapi","PathUndecorate","P","L")
61
+
62
+ @@GetShortPathName =
63
+ Win32API.new("kernel32","GetShortPathName","PPL","L")
64
+
65
+ @@GetLongPathName =
66
+ Win32API.new("kernel32","GetLongPathName","PPL","L")
61
67
 
62
68
  @@PathGetDriveNumber =
63
69
  Win32API.new("shlwapi","PathGetDriveNumber","P","L")
@@ -69,7 +75,7 @@ class Pathname < String
69
75
  Win32API.new("shlwapi", "PathRemoveBackslash", "P", "P")
70
76
  end
71
77
 
72
- VERSION = "1.2.1"
78
+ VERSION = "1.3.0"
73
79
  MAX_PATH = 260
74
80
 
75
81
  # Creates and returns a new Pathname object.
@@ -115,16 +121,44 @@ class Pathname < String
115
121
  @@PathUndecorate.call(buf)
116
122
  Pathname.new(buf.split(0.chr).first)
117
123
  end
118
-
124
+
125
+ # Win32 only
126
+ #
127
+ # Returns the short path for a long path name. For example,
128
+ # C:\Program Files\Java would return C:\Progra~1\Java.
129
+ def short_path
130
+ unless @win
131
+ raise NotImplementedError, "not supported on this platform"
132
+ end
133
+ buf = 0.chr * MAX_PATH
134
+ buf[0..self.length-1] = self
135
+ @@GetShortPathName.call(self, buf, buf.length)
136
+ Pathname.new(buf.split(0.chr).first)
137
+ end
138
+
139
+ # Win32 only
140
+ #
141
+ # Returns the long path for a long path name. For example,
142
+ # C:\Progra~1\Java would return C:\Program Files\Java.
143
+ def long_path
144
+ unless @win
145
+ raise NotImplementedError, "not supported on this platform"
146
+ end
147
+ buf = 0.chr * MAX_PATH
148
+ buf[0..self.length-1] = self
149
+ @@GetLongPathName.call(self, buf, buf.length)
150
+ Pathname.new(buf.split(0.chr).first)
151
+ end
152
+
119
153
  # Removes trailing slash, if present.
120
154
  def pstrip
121
155
  if @win
122
156
  @@PathRemoveBackslash.call(self)
123
- self.strip!
157
+ strip!
124
158
  else
125
159
  if self[-1] == @sep
126
- self.strip!
127
- self.chop!
160
+ strip!
161
+ chop!
128
162
  end
129
163
  end
130
164
  self
@@ -140,7 +174,7 @@ class Pathname < String
140
174
 
141
175
  # Yields each component of the path name to a block.
142
176
  def each
143
- self.to_a.each{ |element| yield element }
177
+ to_a.each{ |element| yield element }
144
178
  end
145
179
 
146
180
  # Yields each component of the path, concatenating the next component on
@@ -166,7 +200,7 @@ class Pathname < String
166
200
  yield root if absolute?
167
201
  end
168
202
 
169
- self.each{ |element|
203
+ each{ |element|
170
204
  if @win && unc?
171
205
  next if root.to_a.include?(element)
172
206
  end
@@ -187,11 +221,11 @@ class Pathname < String
187
221
  return
188
222
  end
189
223
 
190
- n = self.to_a.length
224
+ n = to_a.length
191
225
 
192
226
  while n > 0
193
- path = self.to_a[0..n-1].join(@sep)
194
- if self.absolute?
227
+ path = to_a[0..n-1].join(@sep)
228
+ if absolute?
195
229
  if @win && unc?
196
230
  path = "\\\\" << path
197
231
  end
@@ -232,12 +266,8 @@ class Pathname < String
232
266
  dir = Pathname.new(buf.split(0.chr).first)
233
267
  end
234
268
  else
235
- dir = File.dirname(self)
236
- while dir != "/" && dir != "."
237
- dir = File.dirname(dir)
238
- end
269
+ dir = "/" if self =~ /^\//
239
270
  end
240
- dir = "." if dir.empty?
241
271
  dir
242
272
  end
243
273
 
@@ -313,11 +343,11 @@ class Pathname < String
313
343
  # If the string is an absolute directory, return it
314
344
  return string if string.absolute?
315
345
 
316
- array = self.to_a + string.to_a
346
+ array = to_a + string.to_a
317
347
  new_string = array.join(@sep)
318
348
 
319
- unless self.relative? || @win
320
- new_string = @sep + new_string # Add root path back if needed
349
+ unless relative? || @win
350
+ new_string = @sep + new_string # Add root path back if needed
321
351
  end
322
352
 
323
353
  Pathname.new(new_string).clean
@@ -351,7 +381,7 @@ class Pathname < String
351
381
  end
352
382
 
353
383
  final = []
354
- self.to_a.each{ |element|
384
+ to_a.each{ |element|
355
385
  next if element == "."
356
386
  final.push(element)
357
387
  if element == ".." && self != ".."
data/test/tc_pathname.rb CHANGED
@@ -7,6 +7,11 @@ Dir.chdir ".." if File.basename(Dir.pwd) == "test"
7
7
  $LOAD_PATH.unshift Dir.pwd
8
8
  $LOAD_PATH.unshift Dir.pwd + "/lib"
9
9
 
10
+ if PLATFORM.match("mswin")
11
+ STDERR.puts("Please run 'tc_pathname_win.rb' instead.")
12
+ exit
13
+ end
14
+
10
15
  require "pathname2"
11
16
  require "test/unit"
12
17
 
@@ -36,7 +41,7 @@ class TC_Pathname < Test::Unit::TestCase
36
41
  end
37
42
 
38
43
  def test_version
39
- assert_equal("1.2.1", Pathname::VERSION)
44
+ assert_equal("1.3.0", Pathname::VERSION)
40
45
  end
41
46
 
42
47
  def test_ascend
@@ -1,19 +1,25 @@
1
- ############################################
2
- # tc_pathname.rb
1
+ ######################################################
2
+ # tc_pathname_win.rb
3
3
  #
4
- # Test suite for the pathname package.
5
- ############################################
4
+ # MS Windows test suite for the Pathname class.
5
+ ######################################################
6
6
  Dir.chdir ".." if File.basename(Dir.pwd) == "test"
7
7
  $LOAD_PATH.unshift Dir.pwd
8
8
  $LOAD_PATH.unshift Dir.pwd + "/lib"
9
9
 
10
+ unless PLATFORM.match("mswin")
11
+ STDERR.puts("Please run 'tc_pathname.rb' instead.")
12
+ exit
13
+ end
14
+
10
15
  require "pathname2"
11
16
  require "test/unit"
12
17
 
13
- class TC_Pathname < Test::Unit::TestCase
18
+ class TC_Pathname_MSWin < Test::Unit::TestCase
14
19
  def setup
15
20
  @fpath = Pathname.new("C:/Program Files/Windows NT/Accessories")
16
21
  @bpath = Pathname.new("C:\\Program Files\\Windows NT\\Accessories")
22
+ @spath = Pathname.new("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1")
17
23
  @upath = Pathname.new("\\\\foo\\bar\\baz")
18
24
  @npath = Pathname.new("foo\\bar\\baz")
19
25
  @rpath = Pathname.new("Z:\\")
@@ -28,7 +34,7 @@ class TC_Pathname < Test::Unit::TestCase
28
34
  end
29
35
 
30
36
  def test_version
31
- assert_equal("1.2.1", Pathname::VERSION)
37
+ assert_equal("1.3.0", Pathname::VERSION)
32
38
  end
33
39
 
34
40
  # Convenience method for test_plus
@@ -47,6 +53,24 @@ class TC_Pathname < Test::Unit::TestCase
47
53
  assert_equal(int, result)
48
54
  end
49
55
 
56
+ def test_short_path
57
+ assert_respond_to(@bpath, :short_path)
58
+ assert_nothing_raised{ @bpath.short_path }
59
+ assert_kind_of(Pathname, @bpath.short_path)
60
+ assert_equal("C:\\PROGRA~1\\WINDOW~1\\ACCESS~1", @bpath.short_path)
61
+ end
62
+
63
+ def test_long_path
64
+ assert_respond_to(@spath, :long_path)
65
+ assert_nothing_raised{ @spath.long_path }
66
+ assert_kind_of(Pathname, @spath.long_path)
67
+
68
+ assert_equal(
69
+ "C:\\Program Files\\Windows NT\\Accessories",
70
+ @spath.long_path
71
+ )
72
+ end
73
+
50
74
  def test_each
51
75
  assert_respond_to(@fpath, :each)
52
76
  assert_nothing_raised{ @fpath.each{ |e| } }
@@ -275,6 +299,7 @@ class TC_Pathname < Test::Unit::TestCase
275
299
  def teardown
276
300
  @fpath = nil
277
301
  @bpath = nil
302
+ @spath = nil
278
303
  @upath = nil
279
304
  @npath = nil
280
305
  @rpath = nil
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: pathname2
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.2.1
7
- date: 2005-09-01 00:00:00 -06:00
6
+ version: 1.3.0
7
+ date: 2005-10-26 00:00:00 -06:00
8
8
  summary: An alternate implementation of the Pathname class
9
9
  require_paths:
10
10
  - lib