pathname-glob 0.1.20170628

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a3ec02f39ad57d3a9145f5c5351fe5465b644b65
4
+ data.tar.gz: '0186eac3b450b872b2462abfea27c8b2a0e85c34'
5
+ SHA512:
6
+ metadata.gz: 6754c95223973ec62b6f9caf32465c9666b869909e909cee2771d6c9eff25c8cdaee4bc95344a8dab288b2e21a46a10ae8a596a6c91e0c16dd277f1e577afd2a
7
+ data.tar.gz: d87cf2fdcd9086d4bcb68f6d2d88ab08358510d54c2d0d596f3b78ddfda910477c6c70720f528314958b1e9a7370aa8b562b8ccf73edf92b18d4ec9d4fa1d154
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ Implements a much needed `Pathname#glob` method.
2
+
3
+ To install run `gem install pathname-glob`, and then `require "pathname-glob"` in your code.
4
+
5
+ ## Usage
6
+
7
+ It supports similar interface to `Dir.glob` and `Pathname::glob`.
8
+
9
+ ```ruby
10
+ Pathname("foo").glob("*.txt")
11
+ Pathname("foo").glob("*.txt", File::FNM_DOTMATCH)
12
+ Pathname("foo").glob(["*.txt", "*.html"])
13
+ Pathname("foo").glob("*.txt") do |path|
14
+ # yields Pathname objects
15
+ end # returns nil
16
+ ```
17
+
18
+ ## What's the advantage over other globbing methods?
19
+
20
+ For simple case these are equivalent:
21
+
22
+ ```ruby
23
+ Pathname("/foo/bar").glob("*.txt")
24
+ Pathname.glob("/foo/bar/*.txt")
25
+ Dir.glob("/foo/bar/*.txt").map{|path| Pathname(path)}
26
+ ```
27
+
28
+ However in addition to greater convenience, `Pathname#glob` is the only one that can handle special characters in folder name:
29
+
30
+ ```ruby
31
+ Pathname("very*special*folder/{real} [special] ? yes!").glob("*.txt")
32
+ ```
33
+
34
+ `Dir.glob` and `Pathname.glob` inherently can't as they don't know where folder name ends and pattern begins.
35
+
36
+ If you're dealing with user data, running into special characters like that is pretty much guaranteed.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ task "default" => "spec"
2
+ task "test" => "spec"
3
+
4
+ desc "Run tests"
5
+ task "spec" do
6
+ system "rspec"
7
+ end
8
+
9
+ desc "Clean up"
10
+ task "clean" do
11
+ system "trash pathname-glob-*.gem"
12
+ end
13
+
14
+ desc "Build gem"
15
+ task "gem:build" do
16
+ system "gem build pathname-glob.gemspec"
17
+ end
18
+
19
+ desc "Upload gem"
20
+ task "gem:push" => "gem:build" do
21
+ gem_file = Dir["pathname-glob-*.gem"][-1] or raise "No gem found"
22
+ system "gem", "push", gem_file
23
+ end
@@ -0,0 +1,13 @@
1
+ require "pathname"
2
+
3
+ class Pathname
4
+ def glob(pattern, options=0, &block)
5
+ self_pat = self.to_s.gsub(/([\[\]\{\}\\\*\?])/, "\\\\\\1")
6
+ if pattern.is_a?(Array)
7
+ pattern = pattern.map{|subpattern| File.join(self_pat, subpattern) }
8
+ else
9
+ pattern = File.join(self_pat, pattern)
10
+ end
11
+ Pathname::glob(pattern, options, &block)
12
+ end
13
+ end
@@ -0,0 +1,132 @@
1
+ describe "Pathname#glob" do
2
+ let(:files) {[
3
+ "foo/1.txt",
4
+ "foo/2.txt",
5
+ "foo/3.html",
6
+ "bar/4.html",
7
+ "bar/abc/5.html",
8
+ "bar/6.gif",
9
+ "lol/[]{}/?*?*?*?!\\\\?\\/lol/1.txt",
10
+ "lol/[]{}/?*?*?*?!\\\\?\\/lol/2.gif",
11
+ "lol/[]{}/?*?*?*?!\\\\?\\/lol/3.txt",
12
+ ]}
13
+ let(:tmpdir) {
14
+ Pathname(@dir)
15
+ }
16
+ let(:result) {
17
+ (tmpdir+base_path).glob(*args).map{|path| path.relative_path_from(tmpdir)}
18
+ }
19
+
20
+ before(:each) do
21
+ @dir = Dir.mktmpdir
22
+ files.each do |path|
23
+ (tmpdir+path).parent.mkpath
24
+ FileUtils.touch (tmpdir+path)
25
+ end
26
+ end
27
+
28
+ after(:each) do
29
+ FileUtils.remove_entry(@dir)
30
+ end
31
+
32
+ describe do
33
+ let(:base_path) { "foo" }
34
+ let(:args) { ["*.txt"] }
35
+ it do
36
+ expect(result).to eq([
37
+ Pathname("foo/1.txt"),
38
+ Pathname("foo/2.txt"),
39
+ ])
40
+ end
41
+ end
42
+
43
+ describe do
44
+ let(:base_path) { "bar" }
45
+ let(:args) { ["*.html"] }
46
+ it do
47
+ expect(result).to eq([
48
+ Pathname("bar/4.html"),
49
+ ])
50
+ end
51
+ end
52
+
53
+ describe do
54
+ let(:base_path) { "bar" }
55
+ let(:args) { ["**/*.html"] }
56
+ it do
57
+ expect(result).to eq([
58
+ Pathname("bar/4.html"),
59
+ Pathname("bar/abc/5.html"),
60
+ ])
61
+ end
62
+ end
63
+
64
+ describe "array of arguments" do
65
+ let(:base_path) { "foo" }
66
+ let(:args) { [["?.html", "?.txt"]] }
67
+ it do
68
+ expect(result).to eq([
69
+ Pathname("foo/3.html"),
70
+ Pathname("foo/1.txt"),
71
+ Pathname("foo/2.txt"),
72
+ ])
73
+ end
74
+ end
75
+
76
+ describe "options" do
77
+ let(:base_path) { "foo" }
78
+ let(:args) { ["*", 0] }
79
+ it do
80
+ expect(result).to eq([
81
+ Pathname("foo/1.txt"),
82
+ Pathname("foo/2.txt"),
83
+ Pathname("foo/3.html"),
84
+ ])
85
+ end
86
+ end
87
+
88
+ describe "options" do
89
+ let(:base_path) { "foo" }
90
+ let(:args) { ["*", File::FNM_DOTMATCH] }
91
+ it do
92
+ expect(result).to eq([
93
+ Pathname("foo"), # .
94
+ Pathname("."), # ..
95
+ Pathname("foo/1.txt"),
96
+ Pathname("foo/2.txt"),
97
+ Pathname("foo/3.html"),
98
+ ])
99
+ end
100
+ end
101
+
102
+ # Special characters as per dir.c in ruby sources:
103
+ # hard special (unescapable): / \0
104
+ # unix: { } \ [ ] * ?
105
+ # There's also something about Win32 and . and ~ that I don't really get
106
+ describe "escaping" do
107
+ let(:base_path) { "lol/[]{}/?*?*?*?!\\\\?\\/lol" }
108
+ let(:args) { ["*.txt"] }
109
+ it do
110
+ expect(result).to eq([
111
+ Pathname("lol/[]{}/?*?*?*?!\\\\?\\/lol/1.txt"),
112
+ Pathname("lol/[]{}/?*?*?*?!\\\\?\\/lol/3.txt",),
113
+ ])
114
+ end
115
+ end
116
+
117
+ describe "block" do
118
+ let(:base_path) { "foo" }
119
+ let(:args) { ["*.txt"] }
120
+ it do
121
+ yields = []
122
+ result = (tmpdir+base_path).glob(*args){|*x|
123
+ yields << x
124
+ }
125
+ expect(result).to eq(nil)
126
+ expect(yields).to eq([
127
+ [tmpdir + "foo/1.txt"],
128
+ [tmpdir + "foo/2.txt"],
129
+ ])
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,4 @@
1
+ require_relative "../lib/pathname-glob"
2
+
3
+ RSpec.configure do |config|
4
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pathname-glob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.20170628
5
+ platform: ruby
6
+ authors:
7
+ - Tomasz Wegrzanowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Find files under certain path
56
+ email: Tomasz.Wegrzanowski@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".rspec"
62
+ - README.md
63
+ - Rakefile
64
+ - lib/pathname-glob.rb
65
+ - spec/pathname_glob_spec.rb
66
+ - spec/spec_helper.rb
67
+ homepage: https://github.com/taw/pathname-glob
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.5.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Pathname#glob method
91
+ test_files: []