rubysl-pathname 1.0.0 → 2.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8f18649e8f93aac59bb0550a713693ba6fbff5cc
4
- data.tar.gz: 3b1d2a5bd815c6c0b319fd8d23262aaaa7081043
3
+ metadata.gz: a50a6a72a8ab5c76d641f4c59963970a0351ee82
4
+ data.tar.gz: f73a1000db8a4010a902b2f0449a0d1ccd93a4b3
5
5
  SHA512:
6
- metadata.gz: 158aa06fe4756074097832ce6090a2156b60d8ed5431e14a722eff594c2527bbf8e4ae63224666ee2fbe8cb39eec4d3953c96e99b97f80c2c6ff64f0565a21f2
7
- data.tar.gz: 2823c67acff7ceefcb2e7e824d05cbc626470251026643db6008b514e688c7a8f6f1954fd80a297557359aba068111b076e377c32c2892daa0b2c9fd1cc60ae0
6
+ metadata.gz: 512d80c9c7dd0cb61a0b902081920c98a7f5002e18ffcf3194dd43cdc2481facc9ceabe80ee005d8cb7a2511296235f4b330dc57bf0c262f8abf50bfb5caed29
7
+ data.tar.gz: a4bb9aa419adebc7aac773fe7f0936f22349e93d97f91f9f050dbd7b96cdf99489ee7c9066091aed835541b0aeb01dc11c5244ffe4f0183fb24850761de3b181
@@ -1,8 +1,7 @@
1
1
  language: ruby
2
- before_install:
3
- - gem update --system
4
- - gem --version
5
- - gem install rubysl-bundler
6
- script: bundle exec mspec spec
2
+ env:
3
+ - RUBYLIB=lib
4
+ script: bundle exec mspec
7
5
  rvm:
8
- - rbx-nightly-18mode
6
+ - 1.9.3
7
+ - rbx-nightly-19mode
@@ -36,27 +36,27 @@
36
36
  # === Example 1: Using Pathname
37
37
  #
38
38
  # require 'pathname'
39
- # p = Pathname.new("/usr/bin/ruby")
40
- # size = p.size # 27662
41
- # isdir = p.directory? # false
42
- # dir = p.dirname # Pathname:/usr/bin
43
- # base = p.basename # Pathname:ruby
44
- # dir, base = p.split # [Pathname:/usr/bin, Pathname:ruby]
45
- # data = p.read
46
- # p.open { |f| _ }
47
- # p.each_line { |line| _ }
39
+ # pn = Pathname.new("/usr/bin/ruby")
40
+ # size = pn.size # 27662
41
+ # isdir = pn.directory? # false
42
+ # dir = pn.dirname # Pathname:/usr/bin
43
+ # base = pn.basename # Pathname:ruby
44
+ # dir, base = pn.split # [Pathname:/usr/bin, Pathname:ruby]
45
+ # data = pn.read
46
+ # pn.open { |f| _ }
47
+ # pn.each_line { |line| _ }
48
48
  #
49
49
  # === Example 2: Using standard Ruby
50
50
  #
51
- # p = "/usr/bin/ruby"
52
- # size = File.size(p) # 27662
53
- # isdir = File.directory?(p) # false
54
- # dir = File.dirname(p) # "/usr/bin"
55
- # base = File.basename(p) # "ruby"
56
- # dir, base = File.split(p) # ["/usr/bin", "ruby"]
57
- # data = File.read(p)
58
- # File.open(p) { |f| _ }
59
- # File.foreach(p) { |line| _ }
51
+ # pn = "/usr/bin/ruby"
52
+ # size = File.size(pn) # 27662
53
+ # isdir = File.directory?(pn) # false
54
+ # dir = File.dirname(pn) # "/usr/bin"
55
+ # base = File.basename(pn) # "ruby"
56
+ # dir, base = File.split(pn) # ["/usr/bin", "ruby"]
57
+ # data = File.read(pn)
58
+ # File.open(pn) { |f| _ }
59
+ # File.foreach(pn) { |line| _ }
60
60
  #
61
61
  # === Example 3: Special features
62
62
  #
@@ -71,14 +71,14 @@
71
71
  # p5.cleanpath # Pathname:articles
72
72
  # p5.realpath # Pathname:/home/gavin/articles
73
73
  # p5.children # [Pathname:/home/gavin/articles/linux, ...]
74
- #
74
+ #
75
75
  # == Breakdown of functionality
76
76
  #
77
77
  # === Core methods
78
78
  #
79
- # These methods are effectively manipulating a String, because that's all a path
80
- # is. Except for #mountpoint?, #children, and #realpath, they don't access the
81
- # filesystem.
79
+ # These methods are effectively manipulating a String, because that's
80
+ # all a path is. Except for #mountpoint?, #children, #each_child,
81
+ # #realdirpath and #realpath, they don't access the filesystem.
82
82
  #
83
83
  # - +
84
84
  # - #join
@@ -90,7 +90,9 @@
90
90
  # - #each_filename
91
91
  # - #cleanpath
92
92
  # - #realpath
93
+ # - #realdirpath
93
94
  # - #children
95
+ # - #each_child
94
96
  # - #mountpoint?
95
97
  #
96
98
  # === File status predicate methods
@@ -165,6 +167,7 @@
165
167
  # These methods are a facade for IO:
166
168
  # - #each_line(*args, &block)
167
169
  # - #read(*args)
170
+ # - #binread(*args)
168
171
  # - #readlines(*args)
169
172
  # - #sysopen(*args)
170
173
  #
@@ -194,6 +197,13 @@ class Pathname
194
197
  # to_path is implemented so Pathname objects are usable with File.open, etc.
195
198
  TO_PATH = :to_path
196
199
  end
200
+
201
+ SAME_PATHS = if File::FNM_SYSCASE.nonzero?
202
+ proc {|a, b| a.casecmp(b).zero?}
203
+ else
204
+ proc {|a, b| a == b}
205
+ end
206
+
197
207
  # :startdoc:
198
208
 
199
209
  #
@@ -251,15 +261,40 @@ class Pathname
251
261
 
252
262
  # Return a pathname which is substituted by String#sub.
253
263
  def sub(pattern, *rest, &block)
254
- self.class.new(@path.sub(pattern, *rest, &block))
264
+ if block
265
+ path = @path.sub(pattern, *rest) {|*args|
266
+ begin
267
+ old = Thread.current[:pathname_sub_matchdata]
268
+ Thread.current[:pathname_sub_matchdata] = $~
269
+ eval("$~ = Thread.current[:pathname_sub_matchdata]", block.binding)
270
+ ensure
271
+ Thread.current[:pathname_sub_matchdata] = old
272
+ end
273
+ yield(*args)
274
+ }
275
+ else
276
+ path = @path.sub(pattern, *rest)
277
+ end
278
+ self.class.new(path)
255
279
  end
256
280
 
257
281
  if File::ALT_SEPARATOR
258
- SEPARATOR_PAT = /[#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}]/
282
+ SEPARATOR_LIST = "#{Regexp.quote File::ALT_SEPARATOR}#{Regexp.quote File::SEPARATOR}"
283
+ SEPARATOR_PAT = /[#{SEPARATOR_LIST}]/
259
284
  else
285
+ SEPARATOR_LIST = "#{Regexp.quote File::SEPARATOR}"
260
286
  SEPARATOR_PAT = /#{Regexp.quote File::SEPARATOR}/
261
287
  end
262
288
 
289
+ # Return a pathname which the extension of the basename is substituted by
290
+ # <i>repl</i>.
291
+ #
292
+ # If self has no extension part, <i>repl</i> is appended.
293
+ def sub_ext(repl)
294
+ ext = File.extname(@path)
295
+ self.class.new(@path.chomp(ext) + repl)
296
+ end
297
+
263
298
  # chop_basename(path) -> [pre-basename, basename] or nil
264
299
  def chop_basename(path)
265
300
  base = File.basename(path)
@@ -400,58 +435,25 @@ class Pathname
400
435
  end
401
436
  private :cleanpath_conservative
402
437
 
403
- def realpath_rec(prefix, unresolved, h)
404
- resolved = []
405
- until unresolved.empty?
406
- n = unresolved.shift
407
- if n == '.'
408
- next
409
- elsif n == '..'
410
- resolved.pop
411
- else
412
- path = prepend_prefix(prefix, File.join(*(resolved + [n])))
413
- if h.include? path
414
- if h[path] == :resolving
415
- raise Errno::ELOOP.new(path)
416
- else
417
- prefix, *resolved = h[path]
418
- end
419
- else
420
- s = File.lstat(path)
421
- if s.symlink?
422
- h[path] = :resolving
423
- link_prefix, link_names = split_names(File.readlink(path))
424
- if link_prefix == ''
425
- prefix, *resolved = h[path] = realpath_rec(prefix, resolved + link_names, h)
426
- else
427
- prefix, *resolved = h[path] = realpath_rec(link_prefix, link_names, h)
428
- end
429
- else
430
- resolved << n
431
- h[path] = [prefix, *resolved]
432
- end
433
- end
434
- end
435
- end
436
- return prefix, *resolved
438
+ #
439
+ # Returns the real (absolute) pathname of +self+ in the actual
440
+ # filesystem not containing symlinks or useless dots.
441
+ #
442
+ # All components of the pathname must exist when this method is
443
+ # called.
444
+ #
445
+ def realpath(basedir=nil)
446
+ self.class.new(File.realpath(@path, basedir))
437
447
  end
438
- private :realpath_rec
439
448
 
440
449
  #
441
- # Returns a real (absolute) pathname of +self+ in the actual filesystem.
450
+ # Returns the real (absolute) pathname of +self+ in the actual filesystem.
442
451
  # The real pathname doesn't contain symlinks or useless dots.
443
452
  #
444
- # No arguments should be given; the old behaviour is *obsoleted*.
453
+ # The last component of the real pathname can be nonexistent.
445
454
  #
446
- def realpath
447
- path = @path
448
- prefix, names = split_names(path)
449
- if prefix == ''
450
- prefix, names2 = split_names(Dir.pwd)
451
- names = names2 + names
452
- end
453
- prefix, *names = realpath_rec(prefix, names, {})
454
- self.class.new(prepend_prefix(prefix, File.join(*names)))
455
+ def realdirpath(basedir=nil)
456
+ self.class.new(File.realdirpath(@path, basedir))
455
457
  end
456
458
 
457
459
  # #parent returns the parent directory.
@@ -506,6 +508,7 @@ class Pathname
506
508
  # # yields "usr", "bin", and "ruby".
507
509
  #
508
510
  def each_filename # :yield: filename
511
+ return to_enum(__method__) unless block_given?
509
512
  prefix, names = split_names(@path)
510
513
  names.each {|filename| yield filename }
511
514
  nil
@@ -576,7 +579,7 @@ class Pathname
576
579
  # p2 = p1 + "bin/ruby" # Pathname:/usr/bin/ruby
577
580
  # p3 = p1 + "/etc/passwd" # Pathname:/etc/passwd
578
581
  #
579
- # This method doesn't access the file system; it is pure string manipulation.
582
+ # This method doesn't access the file system; it is pure string manipulation.
580
583
  #
581
584
  def +(other)
582
585
  other = Pathname.new(other) unless Pathname === other
@@ -652,12 +655,12 @@ class Pathname
652
655
  # filename only.
653
656
  #
654
657
  # For example:
655
- # p = Pathname("/usr/lib/ruby/1.8")
656
- # p.children
658
+ # pn = Pathname("/usr/lib/ruby/1.8")
659
+ # pn.children
657
660
  # # -> [ Pathname:/usr/lib/ruby/1.8/English.rb,
658
661
  # Pathname:/usr/lib/ruby/1.8/Env.rb,
659
662
  # Pathname:/usr/lib/ruby/1.8/abbrev.rb, ... ]
660
- # p.children(false)
663
+ # pn.children(false)
661
664
  # # -> [ Pathname:English.rb, Pathname:Env.rb, Pathname:abbrev.rb, ... ]
662
665
  #
663
666
  # Note that the result never contain the entries <tt>.</tt> and <tt>..</tt> in
@@ -679,6 +682,36 @@ class Pathname
679
682
  result
680
683
  end
681
684
 
685
+ # Iterates over the children of the directory
686
+ # (files and subdirectories, not recursive).
687
+ # It yields Pathname object for each child.
688
+ # By default, the yielded pathnames will have enough information to access the files.
689
+ # If you set +with_directory+ to +false+, then the returned pathnames will contain the filename only.
690
+ #
691
+ # Pathname("/usr/local").each_child {|f| p f }
692
+ # #=> #<Pathname:/usr/local/share>
693
+ # # #<Pathname:/usr/local/bin>
694
+ # # #<Pathname:/usr/local/games>
695
+ # # #<Pathname:/usr/local/lib>
696
+ # # #<Pathname:/usr/local/include>
697
+ # # #<Pathname:/usr/local/sbin>
698
+ # # #<Pathname:/usr/local/src>
699
+ # # #<Pathname:/usr/local/man>
700
+ #
701
+ # Pathname("/usr/local").each_child(false) {|f| p f }
702
+ # #=> #<Pathname:share>
703
+ # # #<Pathname:bin>
704
+ # # #<Pathname:games>
705
+ # # #<Pathname:lib>
706
+ # # #<Pathname:include>
707
+ # # #<Pathname:sbin>
708
+ # # #<Pathname:src>
709
+ # # #<Pathname:man>
710
+ #
711
+ def each_child(with_directory=true, &b)
712
+ children(with_directory).each(&b)
713
+ end
714
+
682
715
  #
683
716
  # #relative_path_from returns a relative path from the argument to the
684
717
  # receiver. If +self+ is absolute, the argument must be absolute too. If
@@ -705,12 +738,12 @@ class Pathname
705
738
  base_prefix, basename = r
706
739
  base_names.unshift basename if basename != '.'
707
740
  end
708
- if dest_prefix != base_prefix
741
+ unless SAME_PATHS[dest_prefix, base_prefix]
709
742
  raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
710
743
  end
711
744
  while !dest_names.empty? &&
712
745
  !base_names.empty? &&
713
- dest_names.first == base_names.first
746
+ SAME_PATHS[dest_names.first, base_names.first]
714
747
  dest_names.shift
715
748
  base_names.shift
716
749
  end
@@ -738,16 +771,14 @@ class Pathname # * IO *
738
771
  IO.foreach(@path, *args, &block)
739
772
  end
740
773
 
741
- # Pathname#foreachline is *obsoleted* at 1.8.1. Use #each_line.
742
- def foreachline(*args, &block)
743
- warn "Pathname#foreachline is obsoleted. Use Pathname#each_line."
744
- each_line(*args, &block)
745
- end
746
-
747
- # See <tt>IO.read</tt>. Returns all the bytes from the file, or the first +N+
774
+ # See <tt>IO.read</tt>. Returns all data from the file, or the first +N+ bytes
748
775
  # if specified.
749
776
  def read(*args) IO.read(@path, *args) end
750
777
 
778
+ # See <tt>IO.binread</tt>. Returns all the bytes from the file, or the first +N+
779
+ # if specified.
780
+ def binread(*args) IO.binread(@path, *args) end
781
+
751
782
  # See <tt>IO.readlines</tt>. Returns all the lines from the file.
752
783
  def readlines(*args) IO.readlines(@path, *args) end
753
784
 
@@ -834,20 +865,6 @@ class Pathname # * File *
834
865
  # See <tt>File.split</tt>. Returns the #dirname and the #basename in an
835
866
  # Array.
836
867
  def split() File.split(@path).map {|f| self.class.new(f) } end
837
-
838
- # Pathname#link is confusing and *obsoleted* because the receiver/argument
839
- # order is inverted to corresponding system call.
840
- def link(old)
841
- warn 'Pathname#link is obsoleted. Use Pathname#make_link.'
842
- File.link(old, @path)
843
- end
844
-
845
- # Pathname#symlink is confusing and *obsoleted* because the receiver/argument
846
- # order is inverted to corresponding system call.
847
- def symlink(old)
848
- warn 'Pathname#symlink is obsoleted. Use Pathname#make_symlink.'
849
- File.symlink(old, @path)
850
- end
851
868
  end
852
869
 
853
870
 
@@ -929,7 +946,7 @@ end
929
946
 
930
947
  class Pathname # * Dir *
931
948
  # See <tt>Dir.glob</tt>. Returns or yields Pathname objects.
932
- def Pathname.glob(*args) # :yield: p
949
+ def Pathname.glob(*args) # :yield: pathname
933
950
  if block_given?
934
951
  Dir.glob(*args) {|f| yield self.new(f) }
935
952
  else
@@ -941,18 +958,6 @@ class Pathname # * Dir *
941
958
  def Pathname.getwd() self.new(Dir.getwd) end
942
959
  class << self; alias pwd getwd end
943
960
 
944
- # Pathname#chdir is *obsoleted* at 1.8.1.
945
- def chdir(&block)
946
- warn "Pathname#chdir is obsoleted. Use Dir.chdir."
947
- Dir.chdir(@path, &block)
948
- end
949
-
950
- # Pathname#chroot is *obsoleted* at 1.8.1.
951
- def chroot
952
- warn "Pathname#chroot is obsoleted. Use Dir.chroot."
953
- Dir.chroot(@path)
954
- end
955
-
956
961
  # Return the entries (files and subdirectories) in the directory, each as a
957
962
  # Pathname object.
958
963
  def entries() Dir.entries(@path).map {|f| self.class.new(f) } end
@@ -961,16 +966,10 @@ class Pathname # * Dir *
961
966
  # yields a Pathname object for each entry.
962
967
  #
963
968
  # This method has existed since 1.8.1.
964
- def each_entry(&block) # :yield: p
969
+ def each_entry(&block) # :yield: pathname
965
970
  Dir.foreach(@path) {|f| yield self.class.new(f) }
966
971
  end
967
972
 
968
- # Pathname#dir_foreach is *obsoleted* at 1.8.1.
969
- def dir_foreach(*args, &block)
970
- warn "Pathname#dir_foreach is obsoleted. Use Pathname#each_entry."
971
- each_entry(*args, &block)
972
- end
973
-
974
973
  # See <tt>Dir.mkdir</tt>. Create the referenced directory.
975
974
  def mkdir(*args) Dir.mkdir(@path, *args) end
976
975
 
@@ -995,7 +994,7 @@ class Pathname # * Find *
995
994
  # If +self+ is <tt>.</tt>, yielded pathnames begin with a filename in the
996
995
  # current directory, not <tt>./</tt>.
997
996
  #
998
- def find(&block) # :yield: p
997
+ def find(&block) # :yield: pathname
999
998
  require 'find'
1000
999
  if @path == '.'
1001
1000
  Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
@@ -1037,18 +1036,10 @@ class Pathname # * mixed *
1037
1036
  end
1038
1037
  end
1039
1038
  alias delete unlink
1039
+ end
1040
1040
 
1041
- # This method is *obsoleted* at 1.8.1. Use #each_line or #each_entry.
1042
- def foreach(*args, &block)
1043
- warn "Pathname#foreach is obsoleted. Use each_line or each_entry."
1044
- if FileTest.directory? @path
1045
- # For polymorphism between Dir.foreach and IO.foreach,
1046
- # Pathname#foreach doesn't yield Pathname object.
1047
- Dir.foreach(@path, *args, &block)
1048
- else
1049
- IO.foreach(@path, *args, &block)
1050
- end
1051
- end
1041
+ class Pathname
1042
+ undef =~
1052
1043
  end
1053
1044
 
1054
1045
  module Kernel
@@ -1,5 +1,5 @@
1
1
  module RubySL
2
2
  module Pathname
3
- VERSION = "1.0.0"
3
+ VERSION = "2.0.0"
4
4
  end
5
5
  end
@@ -19,5 +19,4 @@ Gem::Specification.new do |spec|
19
19
  spec.add_development_dependency "bundler", "~> 1.3"
20
20
  spec.add_development_dependency "rake", "~> 10.0"
21
21
  spec.add_development_dependency "mspec", "~> 1.5"
22
- spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
23
- end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysl-pathname
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Shirai
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-25 00:00:00.000000000 Z
11
+ date: 2013-09-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - ~>
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.5'
55
- - !ruby/object:Gem::Dependency
56
- name: rubysl-prettyprint
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '1.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '1.0'
69
55
  description: Ruby standard library pathname.
70
56
  email:
71
57
  - brixen@gmail.com