regex_sieve 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.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +21 -0
- data/README.md +31 -0
- data/lib/regex_sieve.rb +53 -0
- data/lib/regex_sieve/version.rb +3 -0
- data/regex_sieve.gemspec +23 -0
- data/spec/regex_sieve_spec.rb +40 -0
- metadata +69 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b611b4d789b0afe15cc5b2fc78129cc9189ea71f
|
4
|
+
data.tar.gz: 6b2c88dfcabed49ea2d67f3edbb1166b0d282e21
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e1b3abb05b7de18ac325a98ca9f2c6804f2ddbb400ea2744108888e376b3a6a6583b21a5835900d4dfbe235112e5b648e57ba2ec0668e8597ddf47de42375ffd
|
7
|
+
data.tar.gz: 18af4b19c0597ba93024f7e6a03b20c4534143db79364ece99bedc1a1c6d7f77f747f305b12b20da2d873d3554376e78d31cb1470f74c7a958f98fe6290fa6f9
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
regex_sieve (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rspec (3.4.0)
|
11
|
+
rspec-core (~> 3.4.0)
|
12
|
+
rspec-expectations (~> 3.4.0)
|
13
|
+
rspec-mocks (~> 3.4.0)
|
14
|
+
rspec-core (3.4.2)
|
15
|
+
rspec-support (~> 3.4.0)
|
16
|
+
rspec-expectations (3.4.0)
|
17
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
18
|
+
rspec-support (~> 3.4.0)
|
19
|
+
rspec-mocks (3.4.1)
|
20
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
21
|
+
rspec-support (~> 3.4.0)
|
22
|
+
rspec-support (3.4.1)
|
23
|
+
|
24
|
+
PLATFORMS
|
25
|
+
ruby
|
26
|
+
|
27
|
+
DEPENDENCIES
|
28
|
+
regex_sieve!
|
29
|
+
rspec (~> 3.3)
|
30
|
+
|
31
|
+
BUNDLED WITH
|
32
|
+
1.10.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Sterling Paramore
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# RegexSeive
|
2
|
+
|
3
|
+
The RegexSieve functions in a manner similar a hash. The regex sieve
|
4
|
+
is initialized with a hash where the keys are regular expressions and
|
5
|
+
the values can be any valid Ruby object. The order of the keys
|
6
|
+
matters. When the regex sieve is accessed using the array accessor
|
7
|
+
[], it returns the first matching record.
|
8
|
+
|
9
|
+
````ruby
|
10
|
+
sieve = RegexSieve.new({
|
11
|
+
/(Invincible|Born & Raised) IPA/ => 'Craft IPA',
|
12
|
+
/(Fresh Squeezed) IPA/ => 'Microbrew IPA',
|
13
|
+
/IPA/ => 'Other IPA'
|
14
|
+
})
|
15
|
+
|
16
|
+
sieve['Invincible IPA'] # => 'Craft IPA'
|
17
|
+
sieve['Awesome IPA'] # => 'Other IPA'
|
18
|
+
sieve['Kinda Pale Ale'] # => nil
|
19
|
+
````
|
20
|
+
|
21
|
+
By default, only the values are returned, but the key and all matching
|
22
|
+
capture groups can optionally be returned.
|
23
|
+
|
24
|
+
````ruby
|
25
|
+
result_and_match = sieve['Invincible IPA', :match] # => {:value=>"Craft IPA", :match=>#<MatchData "Invincible IPA" 1:"Invincible">}
|
26
|
+
result_and_match[:match][1] # => 'Invincible'
|
27
|
+
|
28
|
+
result_and_key = sieve['Invincible IPA', :regex] # => {:value=>"Craft IPA", :regex=>/(Invincible|Born & Raised) IPA/}
|
29
|
+
|
30
|
+
result_all = sieve['Invincible IPA', :regex, :match] # => {:value=>"Craft IPA", :regex=>/(Invincible|Born & Raised) IPA/, :match=>#<MatchData "Invincible IPA" 1:"Invincible">}
|
31
|
+
````
|
data/lib/regex_sieve.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Public: RegexSieve class. The RegexSieve functions in a manner similar
|
2
|
+
# a hash. The regex sieve is initialized with a hash where the keys are
|
3
|
+
# regular expressions and the values can be any valid Ruby object. The order
|
4
|
+
# of the keys matters. When the regex sieve is accessed using the array
|
5
|
+
# accessor [], it returns the first matching record. By default, only
|
6
|
+
# the values are returned, but the key and all matching capture groups
|
7
|
+
# can optionally be returned.
|
8
|
+
#
|
9
|
+
# Examples:
|
10
|
+
#
|
11
|
+
# r = RegexSieve.new({
|
12
|
+
# /something/ => 'Something',
|
13
|
+
# /something else/ => 'This will never get matched because the one above will match first',
|
14
|
+
# /cool$/ => 'Cool',
|
15
|
+
# /cool beans/ => 'Really Cool'
|
16
|
+
# })
|
17
|
+
#
|
18
|
+
# r['something else'] # => 'Something'
|
19
|
+
# r['cool beans'] # => 'Really Cool'
|
20
|
+
class RegexSieve
|
21
|
+
def initialize(sieve)
|
22
|
+
@sieve = sieve
|
23
|
+
end
|
24
|
+
|
25
|
+
# Public: Array accessor for Regex Sieve.
|
26
|
+
#
|
27
|
+
# key - The string that will be matched to the keys in the sieve.
|
28
|
+
# opt - By default, only the values in the hash used to initialize the sieve
|
29
|
+
# will be returned. However, if you want to return the keys or the
|
30
|
+
# capture groups then use :regex, :match, or both, respectively.
|
31
|
+
#
|
32
|
+
# Example:
|
33
|
+
# r['something'] # => 'Something
|
34
|
+
# r['something', :regex] # => { value: 'Something', regex: /something/ }
|
35
|
+
# r['sometinng', :match, :regex] # => { value: 'Something', regex: /something/, match: #<MatchData "something"> }
|
36
|
+
def [](key, *opt)
|
37
|
+
opt = opt | [:value]
|
38
|
+
|
39
|
+
regex_match = nil
|
40
|
+
found = @sieve.find do |regex, v|
|
41
|
+
regex_match = regex.match(key)
|
42
|
+
end
|
43
|
+
|
44
|
+
full_result = if found.nil?
|
45
|
+
{ value: nil, regex: nil, match: nil }
|
46
|
+
else
|
47
|
+
{ value: found[1], regex: found[0], match: regex_match }
|
48
|
+
end
|
49
|
+
|
50
|
+
full_result.select! { |k, v| opt.include?(k) }
|
51
|
+
full_result.size > 1 ? full_result : full_result.values.first
|
52
|
+
end
|
53
|
+
end
|
data/regex_sieve.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 mode: ruby -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "regex_sieve/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "regex_sieve"
|
7
|
+
s.version = RegexSieve::VERSION
|
8
|
+
s.authors = ["Sterling Paramore"]
|
9
|
+
s.email = ["gnilrets@gmail.com"]
|
10
|
+
s.homepage = "https://github.com/gnilrets/regex_sieve"
|
11
|
+
s.license = "MIT"
|
12
|
+
s.summary = "Regex Sieve"
|
13
|
+
s.description = "A simple lookup sieve using regular expressions"
|
14
|
+
|
15
|
+
s.rubyforge_project = "RegexSieve"
|
16
|
+
|
17
|
+
s.add_development_dependency "rspec", ["~> 3.3"]
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
$LOAD_PATH << '../lib'
|
2
|
+
require 'regex_sieve'
|
3
|
+
|
4
|
+
describe RegexSieve do
|
5
|
+
before do
|
6
|
+
@sieve = RegexSieve.new({
|
7
|
+
/(Invincible|Born & Raised) IPA/ => 'Craft IPA',
|
8
|
+
/(Fresh Squeezed) IPA/ => 'Microbrew IPA',
|
9
|
+
/IPA/ => 'Other IPA'
|
10
|
+
})
|
11
|
+
end
|
12
|
+
|
13
|
+
describe 'looking up a value' do
|
14
|
+
it 'returns the value of the first match' do
|
15
|
+
expect(@sieve['Invincible IPA']).to eq 'Craft IPA'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'is not just a hash lookup' do
|
19
|
+
expect(@sieve['Random IPA']).to eq 'Other IPA'
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'returns nil if no match is found' do
|
23
|
+
expect(@sieve['Does not match']).to be nil
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'using matching groups' do
|
28
|
+
it 'returns the match groups when specified' do
|
29
|
+
match = @sieve['Invincible IPA', :match][:match][1]
|
30
|
+
expect(match).to eq 'Invincible'
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'using regex and matching groups' do
|
35
|
+
it 'returns the matched regex when specified' do
|
36
|
+
regex = @sieve['Invincible IPA', :regex, :match][:regex]
|
37
|
+
expect(regex).to eq /(Invincible|Born & Raised) IPA/
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: regex_sieve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sterling Paramore
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.3'
|
27
|
+
description: A simple lookup sieve using regular expressions
|
28
|
+
email:
|
29
|
+
- gnilrets@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- lib/regex_sieve.rb
|
41
|
+
- lib/regex_sieve/version.rb
|
42
|
+
- regex_sieve.gemspec
|
43
|
+
- spec/regex_sieve_spec.rb
|
44
|
+
homepage: https://github.com/gnilrets/regex_sieve
|
45
|
+
licenses:
|
46
|
+
- MIT
|
47
|
+
metadata: {}
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubyforge_project: RegexSieve
|
64
|
+
rubygems_version: 2.4.5
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: Regex Sieve
|
68
|
+
test_files:
|
69
|
+
- spec/regex_sieve_spec.rb
|