rubypath 0.3.2 → 1.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.
@@ -1,42 +0,0 @@
1
- class Path
2
- class << self
3
- # @!group Mocking / Virtual File System
4
-
5
- # Configure current path backend. Can be used to configure specified
6
- # test scenario. If no virtual or scoped path backend is set the default
7
- # one will be used.
8
- #
9
- # Do not forget to use mock file system in your specs:
10
- # See more {Backend.mock}.
11
- #
12
- # around do |example|
13
- # Path::Backend.mock &example
14
- # end
15
- #
16
- # *Note*: Not all operations are supported.
17
- #
18
- # @example
19
- # Path.mock do |root|
20
- # root.mkpath '/a/b/c/d/e'
21
- # root.touch '/a/b/test.txt'
22
- # root.join('/a/c/lorem.yaml').write YAML.dump({'lorem' => 'ipsum'})
23
- # #...
24
- # end
25
- #
26
- # @example Configure backend (only with virtual file system)
27
- # Path.mock do |root, backend|
28
- # backend.current_user = 'test'
29
- # backend.homes = {'test' => '/path/to/test/home'}
30
- # #...
31
- # end
32
- #
33
- # @yield |root, backend| Yield file system root path and current backend.
34
- # @yieldparam root [Path] Root path of current packend.
35
- # @yieldparam backend [Backend] Current backend.
36
- #
37
- def mock(opts = {})
38
- yield Path('/'), Backend.instance.backend if block_given?
39
- nil
40
- end
41
- end
42
- end
@@ -1,311 +0,0 @@
1
- class Path
2
- # @!group Path Operations
3
-
4
- # Join path with given arguments.
5
- #
6
- # @overload initialize([[Path, String, #to_path, #path, #to_s], ...]
7
- # Join all given arguments to build a new path.
8
- #
9
- # @example
10
- # Path('/').join('test', %w(a b), 5, Pathname.new('file'))
11
- # # => <Path:"/test/a/b/5/file">
12
- #
13
- # @return [Path]
14
- #
15
- def join(*args)
16
- parts = args.flatten
17
- case parts.size
18
- when 0
19
- self
20
- when 1
21
- join = Path parts.shift
22
- join.absolute? ? join : Path(::File.join(path, join.path))
23
- else
24
- join(parts.shift).join(*parts)
25
- end
26
- end
27
-
28
- # Iterate over all path components.
29
- #
30
- # @overload each_component
31
- # Return a enumerator to iterate over all path components.
32
- #
33
- # @example Iterate over path components using a enumerator
34
- # enum = Path('/path/to/file.txt').each_component
35
- # enum.each{|fn| puts fn}
36
- # # => "path"
37
- # # => "to"
38
- # # => "file.txt"
39
- #
40
- # @example Map each path component and create a new path
41
- # path = Path('/path/to/file.txt')
42
- # Path path.each_component.map{|fn| fn.length}
43
- # # => <Path:"/4/2/8">
44
- #
45
- # @return [Enumerator] Return a enumerator for all path components.
46
- #
47
- # @overload each_component(&block)
48
- # Yield given block for each path components.
49
- #
50
- # @example Print each file name
51
- # Path('/path/to/file.txt').each_component{|fn| puts fn}
52
- # # => "path"
53
- # # => "to"
54
- # # => "file.txt"
55
- #
56
- # @param block [Proc] Block to invoke with each path component.
57
- # If no block is given an enumerator will returned.
58
- # @return [self] Self.
59
- #
60
- def each_component(opts = {}, &block)
61
- rv = if opts[:empty]
62
- # split eats leading slashes
63
- ary = path.split(Path.separator)
64
- # so add an empty string if path ends with slash
65
- ary << '' if path[-1] == Path.separator
66
- ary.each(&block)
67
- else
68
- Pathname(path).each_filename(&block)
69
- end
70
- block ? self : rv
71
- end
72
-
73
- # Return an array with all path components.
74
- #
75
- # @example
76
- # Path('path/to/file').components
77
- # # => ["path", "to", "file"]
78
- #
79
- # @example
80
- # Path('/path/to/file').components
81
- # # => ["path", "to", "file"]
82
- #
83
- # @return [Array<String>] File names.
84
- #
85
- def components(*args)
86
- each_component(*args).to_a
87
- end
88
-
89
- # Converts a pathname to an absolute pathname. Given arguments will be
90
- # joined to current path before expanding path. Relative paths are referenced
91
- # from the current working directory of the process unless the `:base` option
92
- # is set, which will be used as the starting point.
93
- #
94
- # The given pathname may start with a "~", which expands to the process
95
- # owner's home directory (the environment variable HOME must be set
96
- # correctly). "~user" expands to the named user's home directory.
97
- #
98
- # @example
99
- # Path('path/to/../tmp').expand
100
- # #=> <Path:"path/tmp">
101
- #
102
- # @example
103
- # Path('~/tmp').expand
104
- # #=> <Path:"/home/user/tmp">
105
- #
106
- # @example
107
- # Path('~oma/tmp').expand
108
- # #=> <Path:"/home/oma/tmp">
109
- #
110
- # @example
111
- # Path('~/tmp').expand('../file.txt')
112
- # #=> <Path:"/home/user/file.txt">
113
- #
114
- # @return [Path] Expanded path.
115
- # @see ::File#expand_path
116
- #
117
- def expand(*args)
118
- opts = args.last.is_a?(Hash) ? args.pop : {}
119
-
120
- with_path(*args) do |path|
121
- base = Path.like_path(opts[:base] || Backend.instance.getwd)
122
- expanded_path = Backend.instance.expand_path(path, base)
123
- if expanded_path != internal_path
124
- Path expanded_path
125
- else
126
- self
127
- end
128
- end
129
- end
130
-
131
- alias_method :expand_path, :expand
132
- alias_method :absolute, :expand
133
- alias_method :absolute_path, :expand
134
-
135
- # Check if path consists of only a filename.
136
- #
137
- # @example
138
- # Path('file.txt').only_filename?
139
- # #=> true
140
- #
141
- # @return [Boolean] True if path consists of only a filename.
142
- #
143
- def only_filename?
144
- internal_path.index(Path.separator).nil?
145
- end
146
-
147
- # Return path to parent directory. If path is already an absolute or relative
148
- # root nil will be returned.
149
- #
150
- # @example Get parent directory:
151
- # Path.new('/path/to/file').dir.path
152
- # #=> '/path/to'
153
- #
154
- # @example Try to get parent of absolute root:
155
- # Path.new('/').dir
156
- # #=> nil
157
- #
158
- # @example Try to get parent of relative root:
159
- # Path.new('.').dir
160
- # #=> nil
161
- #
162
- # @return [Path] Parent path or nil if path already points to an absolute
163
- # or relative root.
164
- #
165
- def dirname
166
- return nil if %w(. /).include? internal_path
167
-
168
- dir = ::File.dirname internal_path
169
- dir.empty? ? nil : self.class.new(dir)
170
- end
171
-
172
- alias_method :parent, :dirname
173
-
174
- # Yield given block for path and each ancestor.
175
- #
176
- # @example
177
- # Path('/path/to/file.txt').ascend{|path| p path}
178
- # #<Path:/path/to/file.txt>
179
- # #<Path:/path/to>
180
- # #<Path:/path>
181
- # #<Path:/>
182
- # #=> <Path:/path/to/file.txt>
183
- #
184
- # @example
185
- # Path('path/to/file.txt').ascend{|path| p path}
186
- # #<Path:path/to/file.txt>
187
- # #<Path:path/to>
188
- # #<Path:path>
189
- # #<Path:.>
190
- # #=> <Path:path/to/file.txt>
191
- #
192
- # @yield |path| Yield path and ancestors.
193
- # @yieldparam path [Path] Path or ancestor.
194
- # @return [Path] Self.
195
- #
196
- def ascend
197
- return to_enum(:ascend) unless block_given?
198
-
199
- path = self
200
- loop do
201
- yield path
202
- break unless (path = path.parent)
203
- end
204
-
205
- self
206
- end
207
-
208
- alias_method :each_ancestors, :ascend
209
-
210
- # Return an array of all ancestors.
211
- #
212
- # @example
213
- # Path('/path/to/file').ancestors
214
- # # => [<Path:/path/to/file.txt>, <Path:/path/to>, <Path:/path>, <Path:/>]
215
- #
216
- # @return [Array<Path>] All ancestors.
217
- #
218
- def ancestors
219
- each_ancestors.to_a
220
- end
221
-
222
- # Return given path as a relative path by just striping leading slashes.
223
- #
224
- # @example
225
- # Path.new('/path/to/file').as_relative
226
- # #=> <Path 'path/to/file'>
227
- #
228
- # @return [Path] Path transformed to relative path.
229
- #
230
- def as_relative
231
- if (rel_path = internal_path.gsub(/^\/+/, '')) != internal_path
232
- Path rel_path
233
- else
234
- self
235
- end
236
- end
237
-
238
- # Return given path as a absolute path by just prepending a leading slash.
239
- #
240
- # @example
241
- # Path.new('path/to/file').as_absolute
242
- # #=> <Path '/path/to/file'>
243
- #
244
- # @return [Path] Path transformed to absolute path.
245
- #
246
- def as_absolute
247
- if internal_path[0] != '/'
248
- Path "/#{internal_path}"
249
- else
250
- self
251
- end
252
- end
253
-
254
- # Return a relative path from the given base path to the receiver path.
255
- #
256
- # Both paths need to be either absolute or relative otherwise an error
257
- # will be raised. The file system will not be accessed and no symlinks are
258
- # assumed.
259
- #
260
- # @example
261
- # relative = Path('src/lib/module1/class.rb')
262
- # .relative_from('src/lib/module2')
263
- # #=> <Path '../module1/class.rb'>
264
- #
265
- # @return [Path] Relative path from argument to receiver.
266
- # @see Pathname#relative_path_from
267
- #
268
- def relative_from(base)
269
- base, path = Path(base).cleanpath, cleanpath
270
-
271
- return Path '.' if base == path
272
-
273
- if (base.relative? && path.absolute?) || (base.absolute? && path.relative?)
274
- raise ArgumentError.new \
275
- "Different prefix: #{base.inspect} and #{path.inspect}"
276
- end
277
-
278
- base, path = base.components(empty: true), path.components(empty: true)
279
- base.shift && path.shift while base.first == path.first && !(base.empty? || path.empty?)
280
-
281
- Path(*((['..'] * base.size) + path))
282
- end
283
- alias_method :relative_path_from, :relative_from
284
-
285
- # Return cleaned path with all dot components removed.
286
- #
287
- # No file system will accessed and not symlinks will be resolved.
288
- #
289
- # @example
290
- # Path('./file.txt').cleanpath
291
- # #=> <Path file.txt>
292
- #
293
- # @example
294
- # Path('path/to/another/../file/../../txt').cleanpath
295
- # #=> <Path path/txt>
296
- #
297
- # @return [Path] Cleaned path.
298
- #
299
- def cleanpath
300
- path = Pathname.new(self).cleanpath
301
- if path == internal_path
302
- self
303
- else
304
- if internal_path[-1] == Path.separator
305
- Path path, ''
306
- else
307
- Path path
308
- end
309
- end
310
- end
311
- end
@@ -1,61 +0,0 @@
1
- class Path
2
- # @!group Path Predicates
3
-
4
- # Check if path is an absolute path.
5
- #
6
- # An absolute path is a path with a leading slash.
7
- #
8
- # @return [Boolean] True if path is absolute.
9
- # @see #relative?
10
- #
11
- def absolute?
12
- internal_path[0] == '/'
13
- end
14
-
15
- # Check if path is a relative path.
16
- #
17
- # A relative path does not start with a slash.
18
- #
19
- # @return [Boolean] True if path is relative.
20
- # @see #absolute?
21
- #
22
- def relative?
23
- !absolute?
24
- end
25
-
26
- # @overload mountpoint?([Path, String], ...)
27
- # Join current and given paths and check if resulting
28
- # path points to a mountpoint.
29
- #
30
- # @example
31
- # Path('/').mountpoint?('tmp')
32
- # #=> true
33
- #
34
- # @overload mountpoint?
35
- # Check if current path is a mountpoint.
36
- #
37
- # @example
38
- # Path('/tmp').mountpoint?
39
- # #=> true
40
- #
41
- # @return [Boolean] True if path is a mountpoint, false otherwise.
42
- # @see Pathname#mountpoint?
43
- #
44
- def mountpoint?(*args)
45
- with_path(*args) do |path|
46
- Backend.instance.mountpoint? path
47
- end
48
- end
49
-
50
- # Check if file or directory is a dot file.
51
- #
52
- # @example
53
- # Path("~/.gitconfig").dotfile?
54
- # #=> true
55
- #
56
- # @return [Boolean] True if file is a dot file otherwise false.
57
- #
58
- def dotfile?
59
- name[0] == '.'
60
- end
61
- end
@@ -1,11 +0,0 @@
1
- class Path
2
- module VERSION
3
- MAJOR = 0
4
- MINOR = 3
5
- PATCH = 2
6
- STAGE = nil
7
- STRING = [MAJOR, MINOR, PATCH, STAGE].reject(&:nil?).join('.').freeze
8
-
9
- def self.to_s; STRING end
10
- end
11
- end
@@ -1,22 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'rubypath/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "rubypath"
8
- spec.version = Path::VERSION
9
- spec.authors = ["Jan Graichen"]
10
- spec.email = ["jg@altimos.de"]
11
- spec.description = %q{Path library incorporating File, Dir, Pathname, IO methods as well as a virtual mock filesystem.}
12
- spec.summary = %q{Path library incorporating File, Dir, Pathname, IO methods as well as a virtual mock filesystem.}
13
- spec.homepage = "https://github.com/jgraichen/rubypath"
14
- spec.license = "LGPLv3"
15
-
16
- spec.files = Dir['**/*'].grep(%r{^((bin|lib|test|spec|features)/|.*\.gemspec|.*LICENSE.*|.*README.*|.*CHANGELOG.*)})
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- end