object_selector 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gemspec +1 -1
- data/Dockerfile +6 -0
- data/Gemfile.lock +3 -3
- data/README.md +30 -12
- data/docker-compose.yml +4 -0
- data/lib/object_selector.rb +9 -0
- data/lib/object_selector/error.rb +6 -0
- data/lib/object_selector/model.rb +24 -0
- data/lib/object_selector/provider/regex_rule_list.rb +33 -0
- metadata +8 -3
- data/lib/object_selector/regex_rule_list.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 66e717c3577951bc8852113b1608059aa2a97a3d
|
4
|
+
data.tar.gz: f38b7984116c716d0dd23873e35feb4060fea914
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e080caef68f3343f750c59781ac1d4491ec6549a75c1494543303ef025cbe7e696efd4667cb7c06a457a56689b733b2669599798ce24aa3a3a89e8badcace27
|
7
|
+
data.tar.gz: eb15a6030a0279f87f7db9dbf91073dee0fe53e92e15e48a5c3cff2de59413086f1df415e6bf4409bc9f578ebb0e713e0607010825ab564001f97194aa40bee7
|
data/.gemspec
CHANGED
data/Dockerfile
ADDED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
object_selector
|
4
|
+
object_selector (0.0.2)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -25,8 +25,8 @@ PLATFORMS
|
|
25
25
|
ruby
|
26
26
|
|
27
27
|
DEPENDENCIES
|
28
|
-
object_selector
|
28
|
+
object_selector!
|
29
29
|
rspec (~> 3.5)
|
30
30
|
|
31
31
|
BUNDLED WITH
|
32
|
-
1.14.
|
32
|
+
1.14.4
|
data/README.md
CHANGED
@@ -1,25 +1,43 @@
|
|
1
1
|
# Object Selector
|
2
2
|
|
3
|
-
## Regex Rule List
|
3
|
+
## Regex Rule List Provider
|
4
4
|
|
5
5
|
### Quickstart
|
6
6
|
```ruby
|
7
|
-
> Struct.new(
|
8
|
-
>
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
7
|
+
> Struct.new("MyFirstObject")
|
8
|
+
> Struct.new("MySecondObject")
|
9
|
+
> selector = ObjectSelector.new(
|
10
|
+
ObjectSelector::Provider::RegexRuleList.new({
|
11
|
+
rules: [
|
12
|
+
{
|
13
|
+
regex: /\D/,
|
14
|
+
object: Struct::MyFirstObject.new
|
15
|
+
}, {
|
16
|
+
regex: /\d/,
|
17
|
+
object: Struct::MySecondObject.new
|
18
|
+
}
|
19
|
+
]
|
20
|
+
})
|
21
|
+
)
|
22
|
+
|
23
|
+
> selector.select("not a digit")
|
24
|
+
Struct::MyFirstObject
|
25
|
+
|
26
|
+
> selector.select(123)
|
27
|
+
Struct::MySecondObject
|
17
28
|
```
|
18
29
|
|
19
30
|
### Testing
|
20
31
|
|
21
32
|
#### Local
|
33
|
+
```bash
|
34
|
+
$ bundle exec rspec
|
35
|
+
```
|
22
36
|
|
37
|
+
#### CI
|
23
38
|
```bash
|
24
|
-
|
39
|
+
docker-compose --file docker-compose.yml --project-name object_selector-provider-regex_rule_list up --abort-on-container-exit --remove-orphans --build --force-recreate
|
40
|
+
EXIT_CODE=$(docker ps -a -f "name=objectselectorproviderregexrulelist_tests" -q | xargs docker inspect -f "{{ .State.ExitCode }}")
|
41
|
+
docker-compose --file docker-compose.yml --project-name object_selector-provider-regex_rule_list down --rmi local
|
42
|
+
exit $EXIT_CODE
|
25
43
|
```
|
data/docker-compose.yml
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'object_selector/provider/regex_rule_list'
|
2
|
+
require 'object_selector/error'
|
3
|
+
|
4
|
+
module ObjectSelector
|
5
|
+
|
6
|
+
class Model
|
7
|
+
##
|
8
|
+
# @param [ObjectSelector::Provider::RegexRuleList] provider
|
9
|
+
##
|
10
|
+
def initialize(provider)
|
11
|
+
@provider = provider
|
12
|
+
end
|
13
|
+
|
14
|
+
##
|
15
|
+
# @param [String] value to match
|
16
|
+
# @return [Object]
|
17
|
+
##
|
18
|
+
def select(value)
|
19
|
+
@provider.select(value)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
module ObjectSelector
|
3
|
+
module Provider
|
4
|
+
|
5
|
+
class RegexRuleList
|
6
|
+
|
7
|
+
##
|
8
|
+
# @param [Array<Hash>] rules
|
9
|
+
# @option rules [Regex] :regex
|
10
|
+
# @option rules [Object] :object
|
11
|
+
##
|
12
|
+
def initialize(rules:)
|
13
|
+
raise ArgumentError, 'Rules should be an array' if not rules.is_a?(Array)
|
14
|
+
@rules = rules
|
15
|
+
end
|
16
|
+
|
17
|
+
##
|
18
|
+
# @param [String] value to match against the regex key in rules
|
19
|
+
# @return [Object] returns selected object key in rules
|
20
|
+
# @raise [ObjectSelector::Error::NoMatchError]
|
21
|
+
##
|
22
|
+
def select(value)
|
23
|
+
object = @rules.each { |rule|
|
24
|
+
break rule[:object] if rule[:regex].match(value)
|
25
|
+
}
|
26
|
+
raise ObjectSelector::RegexRuleList::NoMatchError, 'No rules matched' if object.is_a?(Array) and object.empty?
|
27
|
+
return object
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: object_selector
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Mulder
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-02-
|
11
|
+
date: 2017-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -22,10 +22,15 @@ files:
|
|
22
22
|
- ".rspec"
|
23
23
|
- ".ruby-gemset"
|
24
24
|
- ".ruby-version"
|
25
|
+
- Dockerfile
|
25
26
|
- Gemfile
|
26
27
|
- Gemfile.lock
|
27
28
|
- README.md
|
28
|
-
-
|
29
|
+
- docker-compose.yml
|
30
|
+
- lib/object_selector.rb
|
31
|
+
- lib/object_selector/error.rb
|
32
|
+
- lib/object_selector/model.rb
|
33
|
+
- lib/object_selector/provider/regex_rule_list.rb
|
29
34
|
homepage: https://github.com/hetznerZA/object_selector-regex_rule_list
|
30
35
|
licenses: []
|
31
36
|
metadata: {}
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module ObjectSelector
|
2
|
-
class RegexRuleList
|
3
|
-
|
4
|
-
class NoMatchError < StandardError; end;
|
5
|
-
|
6
|
-
##
|
7
|
-
# @param rules [Array] List of hashes keyed by regex and object
|
8
|
-
# @param value [String] Value use to match against the regex key in rules
|
9
|
-
# @return [Object] Returns selected object key in rules
|
10
|
-
# @raise [ObjectSelector::RegexRuleList::NoMatchError]
|
11
|
-
##
|
12
|
-
def self.select(rules: [], value: "")
|
13
|
-
object = rules.each { |rule|
|
14
|
-
break rule[:object] if rule[:regex].match(value)
|
15
|
-
}
|
16
|
-
raise ObjectSelector::RegexRuleList::NoMatchError, 'No rules matched' if object.is_a?(Array) and object.empty?
|
17
|
-
return object
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
end
|