object_selector 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 073e00b38b71a11f7152e757e8999291f92d50e8
4
- data.tar.gz: dd38d826d08a4e5231649bd3b0eab15c0b63e6f7
3
+ metadata.gz: cbd053025df7ac52e17a117c5e7a0dc22d64fc52
4
+ data.tar.gz: 100090edd145c02e1cc199ca2a7a481e81a96948
5
5
  SHA512:
6
- metadata.gz: 9ca79636e73b07ccdacefa02a0a4c4fc1ca0ac89305b0f1f4e74d832bbaadda8030602055b1d93c0f00c498445307b6db5614d27416a53e50a7b7836656c8c09
7
- data.tar.gz: 860e3b7d8faf357326e6ba80fcbe16ddd9183fb1e35051b186e381d19fc20f725c3efdcd2a4a788844664bfccf9d7dbacd628ea705f98d28ba8c96f8b2bec1d1
6
+ metadata.gz: a8aafff0b3d2898ef5b5a669f916491fad23fae155000b02f68460a2793c7e38f672668796ac4f58d08202c55b4d6c08f703d708ca78746f67a8274304817439
7
+ data.tar.gz: 8452389e765a4f5cf2297cdb17cb1953238301ad02fed31c68e83caf56b05c6194f2014f5abdb7830ce20f3fb59677216886cbb451515d64e2262f052edcee49
data/.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "object_selector"
3
- spec.version = "1.0.1"
3
+ spec.version = "1.0.2"
4
4
  spec.authors = ["Charles Mulder"]
5
5
  spec.email = ["charles.mulder@hetzner.co.za"]
6
6
 
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  *.gem
2
+ Gemfile.lock
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # Object Selector
2
2
 
3
- ## Regex Rule List Provider
3
+ ## Quickstart
4
+
5
+ ### Regex Rule List Provider
4
6
 
5
- ### Quickstart
6
7
  ```ruby
7
8
  > Struct.new("MyFirstObject")
8
9
  > Struct.new("MySecondObject")
@@ -27,6 +28,33 @@ Struct::MyFirstObject
27
28
  Struct::MySecondObject
28
29
  ```
29
30
 
31
+ ### First needle found in sorted haystack provider
32
+
33
+ ```ruby
34
+ > Struct.new("MyFirstObject")
35
+ > Struct.new("MySecondObject")
36
+ > selector = ObjectSelector.new(
37
+ ObjectSelector::Provider::FirstNeedleFoundInSortedHaystack.new({
38
+ rules: [
39
+ {
40
+ needle: 'a',
41
+ object: Struct::MyFirstObject.new
42
+ },
43
+ {
44
+ needle: 'b',
45
+ object: Struct::MySecondObject.new
46
+ }
47
+ ]
48
+ })
49
+ )
50
+
51
+ > selector.select(['b', 'a'])
52
+ Struct::MyFirstObject
53
+
54
+ > selector.select(['c', 'b'])
55
+ Struct::MySecondObject
56
+ ```
57
+
30
58
  ### Testing
31
59
 
32
60
  #### Local
@@ -36,8 +64,8 @@ $ bundle exec rspec
36
64
 
37
65
  #### CI
38
66
  ```bash
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
67
+ docker-compose --file docker-compose.yml --project-name object_selector up --abort-on-container-exit --remove-orphans --build --force-recreate
68
+ EXIT_CODE=$(docker ps -a -f "name=objectselector_tests" -q | xargs docker inspect -f "{{ .State.ExitCode }}")
69
+ docker-compose --file docker-compose.yml --project-name object_selector down --rmi local
42
70
  exit $EXIT_CODE
43
71
  ```
@@ -1,4 +1,5 @@
1
1
  require 'object_selector/provider/regex_rule_list'
2
+ require 'object_selector/provider/first_needle_found_in_sorted_haystack'
2
3
  require 'object_selector/error'
3
4
 
4
5
  module ObjectSelector
@@ -12,8 +13,9 @@ module ObjectSelector
12
13
  end
13
14
 
14
15
  ##
15
- # @param [String] value to match
16
+ # @param [String, Array] value to match against
16
17
  # @return [Object]
18
+ # @raise [ObjectSelector::Error::NoMatchError] when no match
17
19
  ##
18
20
  def select(value)
19
21
  @provider.select(value)
@@ -0,0 +1,33 @@
1
+
2
+ module ObjectSelector
3
+ module Provider
4
+
5
+ class FirstNeedleFoundInSortedHaystack
6
+
7
+ ##
8
+ # @param [Array<Hash>] rules
9
+ # @option rules [String, Number] :needle
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 [Array<String>] haystack
19
+ # @return [Object] returns selected object key in rules
20
+ # @raise [ObjectSelector::Error::NoMatchError]
21
+ ##
22
+ def select(haystack)
23
+ object = @rules.map { |rule|
24
+ break rule[:object] if haystack.sort.include?(rule[:needle])
25
+ }
26
+ raise ObjectSelector::Error::NoMatchError, 'No rules matched' if object.is_a?(Array) and object.all? &:nil?
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: 1.0.1
4
+ version: 1.0.2
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-20 00:00:00.000000000 Z
11
+ date: 2017-03-14 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -24,12 +24,12 @@ files:
24
24
  - ".ruby-version"
25
25
  - Dockerfile
26
26
  - Gemfile
27
- - Gemfile.lock
28
27
  - README.md
29
28
  - docker-compose.yml
30
29
  - lib/object_selector.rb
31
30
  - lib/object_selector/error.rb
32
31
  - lib/object_selector/model.rb
32
+ - lib/object_selector/provider/first_needle_found_in_sorted_haystack.rb
33
33
  - lib/object_selector/provider/regex_rule_list.rb
34
34
  homepage: https://github.com/hetznerZA/object_selector-regex_rule_list
35
35
  licenses: []
@@ -1,36 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- object_selector (1.0.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- diff-lcs (1.3)
10
- faker (1.7.3)
11
- i18n (~> 0.5)
12
- i18n (0.8.0)
13
- rspec (3.5.0)
14
- rspec-core (~> 3.5.0)
15
- rspec-expectations (~> 3.5.0)
16
- rspec-mocks (~> 3.5.0)
17
- rspec-core (3.5.4)
18
- rspec-support (~> 3.5.0)
19
- rspec-expectations (3.5.0)
20
- diff-lcs (>= 1.2.0, < 2.0)
21
- rspec-support (~> 3.5.0)
22
- rspec-mocks (3.5.0)
23
- diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.5.0)
25
- rspec-support (3.5.0)
26
-
27
- PLATFORMS
28
- ruby
29
-
30
- DEPENDENCIES
31
- faker (~> 1.6, >= 1.6.3)
32
- object_selector!
33
- rspec (~> 3.5)
34
-
35
- BUNDLED WITH
36
- 1.14.4