glob_matcher 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/README.md +63 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/glob_matcher.gemspec +38 -0
- data/lib/glob_matcher.rb +6 -0
- data/lib/glob_matcher/matcher.rb +60 -0
- data/lib/glob_matcher/version.rb +3 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0df67154443913e6d8ba50b247882da29f1485fa7f1990675d9f7798277727fb
|
4
|
+
data.tar.gz: 2a8a046aaca3d27263a9d4e8e6ef1b3a078da76d8081eb99440ac3dfa6afe0a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d9a54b6674e1c39a1567b104f844393199d741d15fcaa11a713d58b413e514ef69cfda63de8ee99b89484081ea3ebf838ae4a5f019da3d8d7200479951e8a403
|
7
|
+
data.tar.gz: 307350e70d4ce377b3f6c602c48ffafcdb45d22301588ece0ce35d04391d1df04b742722f96b28a04bc11bc88e074457e4a8b10fa291f7d8f7826387a1ebca70
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
glob_matcher
|
data/.ruby-version
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
glob_matcher (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.3)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.9.0)
|
12
|
+
rspec-core (~> 3.9.0)
|
13
|
+
rspec-expectations (~> 3.9.0)
|
14
|
+
rspec-mocks (~> 3.9.0)
|
15
|
+
rspec-core (3.9.1)
|
16
|
+
rspec-support (~> 3.9.1)
|
17
|
+
rspec-expectations (3.9.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.9.0)
|
20
|
+
rspec-mocks (3.9.1)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.9.0)
|
23
|
+
rspec-support (3.9.2)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 2.0)
|
30
|
+
glob_matcher!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.9)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
2.1.4
|
data/README.md
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
<img src="http://cdn2-cloud66-com.s3.amazonaws.com/images/oss-sponsorship.png" width=150/>
|
2
|
+
|
3
|
+
# GlobMatcher
|
4
|
+
|
5
|
+
GlobMatcher is a string matcher for Ruby. You can use it to check for matches using wildcards. It supports multiple patterns as well as negative patterns.
|
6
|
+
|
7
|
+
GlobMatcher is used in [Cloud 66 Skycap](https://www.cloud66.com/containers/skycap/) to filter git branches before deployment.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'glob_matcher'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install glob_matcher
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
GlobMatcher checks strings against a pattern. A pattern is a space delimited list of single patterns to check against. Here are some patterns:
|
28
|
+
|
29
|
+
```
|
30
|
+
abc -> Matches abc
|
31
|
+
a* -> Matches ab, abc, azyz, ...
|
32
|
+
foo*bar -> Matches fooxbar, fooxxxxbar, ...
|
33
|
+
!abc -> Matches any value except for abc
|
34
|
+
!ab* -> Matches any value except for anything that matches ab*
|
35
|
+
foo{abc,xyz}bar -> Matches fooabcbar and fooxyzbar
|
36
|
+
```
|
37
|
+
|
38
|
+
You can use multiple patterns:
|
39
|
+
|
40
|
+
```
|
41
|
+
abc xyz -> Matches abc or xyz (OR)
|
42
|
+
!abc !xyz -> Matches anything expcet abc and xyz (AND)
|
43
|
+
foo bar !fuzz !buzz -> Matches foo or bar but not fuzz and buzz
|
44
|
+
```
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
matcher = ::GlobMatcher::Matcher.new('hel*')
|
48
|
+
matcher.is_match? 'hello' # true
|
49
|
+
|
50
|
+
matcher = ::GlobMatcher::Matcher.new('fo* !bar')
|
51
|
+
matcher.is_match? 'foo' # true
|
52
|
+
matcher.is_match? 'bar' # false
|
53
|
+
```
|
54
|
+
|
55
|
+
## Development
|
56
|
+
|
57
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
58
|
+
|
59
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cloud66-oss/glob_matcher.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "glob_matcher"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "glob_matcher/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "glob_matcher"
|
8
|
+
spec.version = GlobMatcher::VERSION
|
9
|
+
spec.authors = ["Khash Sajadi"]
|
10
|
+
spec.email = ["khash@cloud66.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{This is a simple string matcher for glob patterns}
|
13
|
+
spec.description = %q{Use wildcard placeholders to find string matches as well as using ! as a negative matcher}
|
14
|
+
spec.homepage = "https://www.cloud66.com/oss/"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
if spec.respond_to?(:metadata)
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/cloud66-oss/glob_matcher"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
36
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
37
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
38
|
+
end
|
data/lib/glob_matcher.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
module GlobMatcher
|
2
|
+
class Matcher
|
3
|
+
attr_reader :glob
|
4
|
+
|
5
|
+
def initialize(glob)
|
6
|
+
@glob = glob
|
7
|
+
end
|
8
|
+
|
9
|
+
# checks if the given value matches the pattern
|
10
|
+
def is_match?(value)
|
11
|
+
pattern_groups = group_patterns
|
12
|
+
if pattern_groups[:negatives].empty?
|
13
|
+
negatives = true
|
14
|
+
else
|
15
|
+
negatives = pattern_groups[:negatives].all? { |x| is_single_match?(x, value) }
|
16
|
+
end
|
17
|
+
|
18
|
+
if pattern_groups[:positives].empty?
|
19
|
+
positives = true
|
20
|
+
else
|
21
|
+
positives = pattern_groups[:positives].any? { |x| is_single_match?(x, value) }
|
22
|
+
end
|
23
|
+
|
24
|
+
return false if pattern_groups[:positives].empty? && pattern_groups[:negatives].empty?
|
25
|
+
return positives && negatives
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# groups patterns into 2 groups: negatives
|
31
|
+
# and positives.
|
32
|
+
def group_patterns
|
33
|
+
patterns = @glob.split(' ')
|
34
|
+
negatives = []
|
35
|
+
positives = []
|
36
|
+
patterns.each do |x|
|
37
|
+
if is_negative?(x)
|
38
|
+
negatives << x
|
39
|
+
else
|
40
|
+
positives << x
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
return { negatives: negatives, positives: positives }
|
45
|
+
end
|
46
|
+
|
47
|
+
def is_negative?(pattern)
|
48
|
+
pattern.start_with? '!'
|
49
|
+
end
|
50
|
+
|
51
|
+
def is_single_match?(pattern, value)
|
52
|
+
if is_negative?(pattern)
|
53
|
+
# this will allow escaping of !
|
54
|
+
!File.fnmatch?(pattern[1..-1], value, File::FNM_EXTGLOB)
|
55
|
+
else
|
56
|
+
File.fnmatch?(pattern, value, File::FNM_EXTGLOB)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glob_matcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Khash Sajadi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.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: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.9'
|
55
|
+
description: Use wildcard placeholders to find string matches as well as using ! as
|
56
|
+
a negative matcher
|
57
|
+
email:
|
58
|
+
- khash@cloud66.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rspec"
|
65
|
+
- ".ruby-gemset"
|
66
|
+
- ".ruby-version"
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- glob_matcher.gemspec
|
74
|
+
- lib/glob_matcher.rb
|
75
|
+
- lib/glob_matcher/matcher.rb
|
76
|
+
- lib/glob_matcher/version.rb
|
77
|
+
homepage: https://www.cloud66.com/oss/
|
78
|
+
licenses: []
|
79
|
+
metadata:
|
80
|
+
homepage_uri: https://www.cloud66.com/oss/
|
81
|
+
source_code_uri: https://github.com/cloud66-oss/glob_matcher
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 2.7.6.2
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: This is a simple string matcher for glob patterns
|
102
|
+
test_files: []
|