epath 0.0.1 → 0.1.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.
@@ -0,0 +1,59 @@
1
+ class Path
2
+ # Opens the file for reading or writing. See +File.open+.
3
+ def open(*args, &block) # :yield: file
4
+ File.open(@path, *args, &block)
5
+ end
6
+
7
+ # Iterates over the lines in the file. See +IO.foreach+.
8
+ def each_line(*args, &block) # :yield: line
9
+ IO.foreach(@path, *args, &block)
10
+ end
11
+ alias :lines :each_line
12
+
13
+ # Returns all data from the file, or the first +N+ bytes if specified.
14
+ # See +IO.read+.
15
+ def read(*args)
16
+ IO.read(@path, *args)
17
+ end
18
+
19
+ # Returns all the bytes from the file, or the first +N+ if specified.
20
+ # See +IO.binread+.
21
+ if IO.respond_to? :binread
22
+ def binread(*args)
23
+ IO.binread(@path, *args)
24
+ end
25
+ else
26
+ alias :binread :read
27
+ end
28
+
29
+ # Returns all the lines from the file. See +IO.readlines+.
30
+ def readlines(*args)
31
+ IO.readlines(@path, *args)
32
+ end
33
+
34
+ # See +IO.sysopen+.
35
+ def sysopen(*args)
36
+ IO.sysopen(@path, *args)
37
+ end
38
+
39
+ if IO.respond_to? :write
40
+ def write(contents, *open_args)
41
+ IO.write(@path, contents, *open_args)
42
+ end
43
+ else
44
+ def write(contents, *open_args)
45
+ open('w', *open_args) { |f| f.write(contents) }
46
+ end
47
+ end
48
+
49
+ if IO.respond_to? :write and !RUBY_DESCRIPTION.start_with?('jruby')
50
+ def append(contents, open_args = {})
51
+ open_args[:mode] = 'a'
52
+ IO.write(@path, contents, open_args)
53
+ end
54
+ else
55
+ def append(contents, open_args = nil)
56
+ open('a') { |f| f.write(contents) }
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,26 @@
1
+ class Path
2
+
3
+ # The list of loaders. See {Path.register_loader}.
4
+ LOADERS = {}
5
+
6
+ # Registers a new loader (a block which will be called with the Path to load)
7
+ # for the given extensions (either with the leading dot or not)
8
+ #
9
+ # Path.register_loader('.marshal') { |file| Marshal.load file.read }
10
+ def self.register_loader(*extensions, &loader)
11
+ extensions.each { |ext|
12
+ LOADERS[pure_ext(ext)] = loader
13
+ }
14
+ end
15
+
16
+ # Path#load helps loading data from various files.
17
+ # JSON and YAML loaders are provided by default.
18
+ # See Path.register_loader.
19
+ def load
20
+ if LOADERS.key? ext
21
+ LOADERS[ext].call(self)
22
+ else
23
+ raise "Unable to load #{self} (unrecognized extension)"
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,115 @@
1
+ class Path
2
+ # Returns the last component of the path. See +File.basename+.
3
+ def basename(*args)
4
+ Path.new(File.basename(@path, *args))
5
+ end
6
+
7
+ # basename(extname)
8
+ def base
9
+ basename(extname)
10
+ end
11
+
12
+ # Returns all but the last component of the path. See +File.dirname+.
13
+ def dirname
14
+ Path.new(File.dirname(@path))
15
+ end
16
+ alias :dir :dirname
17
+
18
+ # Returns the file's extension. See +File.extname+.
19
+ def extname
20
+ File.extname(@path)
21
+ end
22
+
23
+ # extname without leading .
24
+ def ext
25
+ ext = extname
26
+ ext.empty? ? ext : ext[1..-1]
27
+ end
28
+
29
+ # Returns the #dirname and the #basename in an Array. See +File.split+.
30
+ def split
31
+ File.split(@path).map(&Path)
32
+ end
33
+
34
+ def add_extension(ext)
35
+ return self if ext.empty?
36
+ Path.new @path+dotted_ext(ext)
37
+ end
38
+ alias :add_ext :add_extension
39
+
40
+ def without_extension
41
+ Path.new @path[0..-extname.size-1]
42
+ end
43
+ alias :rm_ext :without_extension
44
+
45
+ def replace_extension(ext)
46
+ return without_extension if ext.empty?
47
+ Path.new(@path[0..-extname.size-1] << dotted_ext(ext))
48
+ end
49
+ alias :sub_ext :replace_extension
50
+
51
+ # Iterates over each component of the path.
52
+ #
53
+ # Path.new("/usr/bin/ruby").each_filename { |filename| ... }
54
+ # # yields "usr", "bin", and "ruby".
55
+ def each_filename # :yield: filename
56
+ return to_enum(__method__) unless block_given?
57
+ _, names = split_names(@path)
58
+ names.each { |filename| yield filename }
59
+ nil
60
+ end
61
+
62
+ # Iterates over and yields a new Path object
63
+ # for each element in the given path in descending order.
64
+ #
65
+ # Path.new('/path/to/some/file.rb').descend { |v| p v }
66
+ # #<Path />
67
+ # #<Path /path>
68
+ # #<Path /path/to>
69
+ # #<Path /path/to/some>
70
+ # #<Path /path/to/some/file.rb>
71
+ #
72
+ # Path.new('path/to/some/file.rb').descend { |v| p v }
73
+ # #<Path path>
74
+ # #<Path path/to>
75
+ # #<Path path/to/some>
76
+ # #<Path path/to/some/file.rb>
77
+ #
78
+ # It doesn't access actual filesystem.
79
+ def descend
80
+ return to_enum(:descend) unless block_given?
81
+ vs = []
82
+ ascend { |v| vs << v }
83
+ vs.reverse_each { |v| yield v }
84
+ nil
85
+ end
86
+
87
+ # Iterates over and yields a new Path object
88
+ # for each element in the given path in ascending order.
89
+ #
90
+ # Path.new('/path/to/some/file.rb').ascend { |v| p v }
91
+ # #<Path /path/to/some/file.rb>
92
+ # #<Path /path/to/some>
93
+ # #<Path /path/to>
94
+ # #<Path /path>
95
+ # #<Path />
96
+ #
97
+ # Path.new('path/to/some/file.rb').ascend { |v| p v }
98
+ # #<Path path/to/some/file.rb>
99
+ # #<Path path/to/some>
100
+ # #<Path path/to>
101
+ # #<Path path>
102
+ #
103
+ # It doesn't access actual filesystem.
104
+ def ascend
105
+ return to_enum(:ascend) unless block_given?
106
+ path = @path
107
+ yield self
108
+ while r = chop_basename(path)
109
+ path, = r
110
+ break if path.empty?
111
+ yield Path.new(del_trailing_separator(path))
112
+ end
113
+ end
114
+ alias :ancestors :ascend
115
+ end
@@ -0,0 +1,35 @@
1
+ class Path
2
+ # Whether a path is absolute.
3
+ def absolute?
4
+ !relative?
5
+ end
6
+
7
+ # Whether a path is relative.
8
+ def relative?
9
+ path = @path
10
+ while r = chop_basename(path)
11
+ path, = r
12
+ end
13
+ path == ''
14
+ end
15
+
16
+ # #root? is a predicate for root directories. I.e. it returns +true+ if the
17
+ # path consists of consecutive slashes.
18
+ #
19
+ # It doesn't access actual filesystem. So it may return +false+ for some
20
+ # paths which points to roots such as +/usr/..+.
21
+ def root?
22
+ !!(chop_basename(@path) == nil && @path.include?('/'))
23
+ end
24
+
25
+ # #mountpoint? returns +true+ if +self+ points to a mountpoint.
26
+ def mountpoint?
27
+ begin
28
+ stat1 = lstat
29
+ stat2 = parent.lstat
30
+ stat1.dev != stat2.dev or stat1.ino == stat2.ino
31
+ rescue Errno::ENOENT
32
+ false
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ class Path
2
+ def self.require_tree(directory = nil)
3
+ if directory
4
+ new(directory).require_tree
5
+ else
6
+ file = Path.file(caller)
7
+ file.dir.require_tree(file)
8
+ end
9
+ end
10
+
11
+ def require_tree(source = nil)
12
+ glob('**/*.rb').sort.each { |file| require file.expand(dir) unless file == source }
13
+ end
14
+ end
metadata CHANGED
@@ -1,49 +1,95 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: epath
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.1
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - eregon
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2011-12-16 00:00:00.000000000 Z
13
- dependencies: []
17
+
18
+ date: 2012-03-16 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
14
34
  description:
15
35
  email: eregontp@gmail.com
16
36
  executables: []
37
+
17
38
  extensions: []
39
+
18
40
  extra_rdoc_files: []
19
- files:
41
+
42
+ files:
43
+ - lib/epath/dir.rb
44
+ - lib/epath/file.rb
45
+ - lib/epath/file_dir.rb
46
+ - lib/epath/file_predicates.rb
47
+ - lib/epath/fileutils.rb
48
+ - lib/epath/find.rb
49
+ - lib/epath/identity.rb
20
50
  - lib/epath/implementation.rb
51
+ - lib/epath/io.rb
52
+ - lib/epath/load.rb
53
+ - lib/epath/parts.rb
54
+ - lib/epath/predicates.rb
55
+ - lib/epath/require_tree.rb
21
56
  - lib/epath.rb
22
57
  - README.md
23
58
  - LICENSE
24
59
  - epath.gemspec
25
60
  homepage: https://github.com/eregon/epath
26
61
  licenses: []
62
+
27
63
  post_install_message:
28
64
  rdoc_options: []
29
- require_paths:
65
+
66
+ require_paths:
30
67
  - lib
31
- required_ruby_version: !ruby/object:Gem::Requirement
68
+ required_ruby_version: !ruby/object:Gem::Requirement
32
69
  none: false
33
- requirements:
34
- - - ! '>='
35
- - !ruby/object:Gem::Version
36
- version: '0'
37
- required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
78
  none: false
39
- requirements:
40
- - - ! '>='
41
- - !ruby/object:Gem::Version
42
- version: '0'
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
43
86
  requirements: []
87
+
44
88
  rubyforge_project:
45
- rubygems_version: 1.8.11
89
+ rubygems_version: 1.8.15
46
90
  signing_key:
47
91
  specification_version: 3
48
92
  summary: a Path manipulation library
49
93
  test_files: []
94
+
95
+ has_rdoc: