hash_filterer 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.
Files changed (5) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +22 -0
  3. data/README.md +36 -0
  4. data/lib/hash_filterer.rb +112 -0
  5. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 440ade367f017439edb55f6fb4b81f8cab972716
4
+ data.tar.gz: 1fb93dfcb6472f0314af8a52ef5da645dee9cfc0
5
+ SHA512:
6
+ metadata.gz: 5b58ce93c4bb097c4e85d12596948e161b2c4f806d04519add10f74fe5c622980bd16748b6b5e7377842b08245e48e661c16c83911a68cf2560233285a2988f1
7
+ data.tar.gz: 4f5cf9b1774267d8f355a3666d034b80f8b9c406cff1590fc6e1bf2342ca97c52e0c259de83a005824c6802ce97c1219fdff62ec0cf4ece45a7fdd50a579711f
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 gleseur
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.
22
+
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # hash_filterer
2
+ Check hash against a set of rules
3
+
4
+ ## Usage
5
+ Usage is pretty simple, there are only 2 methods:
6
+ - accept? => Is the hash passing the rules
7
+ - error_messages => If not passing, messages detailling why
8
+ - rules are defined as an array of hashes, with string keys (so can be stored directly in a hstore or json field)
9
+
10
+ ```ruby
11
+ require 'hash_filterer'
12
+
13
+ rules = [{
14
+ # The key path to the value to be tested (can be an array for nested fields)
15
+ 'key' => %w(foo plouf),
16
+ # Operator to check with
17
+ 'operator' => '==',
18
+ # value to check against
19
+ 'value' => 'bar',
20
+ # Is nil (or missing key) ok or not - default is false
21
+ 'nil_ok' => false,
22
+ # optional preprocessor for the value to be tested before being checked. must be a public function of the value
23
+ 'preprocessor' => 'downcase',
24
+ # If an intermediate level is an array, do we need all values to pass, or just one ?
25
+ 'at_least_one' => true
26
+ }]
27
+
28
+ filterer = HashFilterer.new rules
29
+ filterer.accept? 'foo' => { 'plouf' => 'bar' } # true
30
+ filterer.accept? 'foo' => { 'plouf' => 'bar-bar' } # false
31
+ filterer.error_messages # ["Not bar-bar == bar"]
32
+ filterer.accept? 'foo-foo' => { 'plouf' => 'bar-bar' } # false (as nil_ok is false)
33
+ filterer.accept? 'foo' => {} # false (as nil_ok is false)
34
+ filterer.accept? 'foo' => [{ 'plouf' => 'bar' }, { 'plouf' => 'bar-bar' }] # true (as at_least_one is true)
35
+ filterer.error_messages # [] # As apply to previous check
36
+ ```
@@ -0,0 +1,112 @@
1
+ require 'active_support/all'
2
+
3
+ # Can be used to check if a given hash respects a list of rules
4
+ class HashFilterer
5
+ # Corresponds to a single rule
6
+ class Rule
7
+ class InvalidOperator < ArgumentError; end
8
+ class InvalidPreprocessor < ArgumentError; end
9
+
10
+ attr_reader :error_message
11
+
12
+ def self.allowed_operators
13
+ ['==', '!=', '>', '<', 'IN', '=~']
14
+ end
15
+
16
+ def self.allowed_preprocessors
17
+ ['downcase']
18
+ end
19
+
20
+ # nil_ok should be true or false to specify the behavior when the value is nil
21
+ def initialize(key, operator, value, nil_ok, preprocessor, at_least_one) # rubocop:disable Metrics/ParameterLists
22
+ @keys = Array.wrap key
23
+ @operator = operator
24
+ @value = value
25
+ @nil_ok = nil_ok || false
26
+ @preprocessor = preprocessor
27
+ @at_least_one = at_least_one || false
28
+ check_operator!
29
+ check_preprocessor!
30
+ end
31
+
32
+ def accept?(hash)
33
+ values = read_values hash
34
+ msgs = values.map do |actual|
35
+ build_message actual unless ok_for actual
36
+ end.compact
37
+ return true if @at_least_one && values.length > msgs.length
38
+ return true if !@at_least_one && msgs.empty?
39
+ @error_message = msgs.join ' and '
40
+ false
41
+ end
42
+
43
+ private
44
+
45
+ def ok_for(actual)
46
+ return @nil_ok if actual.nil?
47
+ return @value.include? actual if @operator == 'IN'
48
+ return actual =~ Regexp.new(@value) if @operator == '=~'
49
+ actual.public_send @operator, @value
50
+ end
51
+
52
+ def build_message(actual)
53
+ actual = 'nil' if actual.nil?
54
+ "Not #{actual} #{@operator} #{@value}"
55
+ end
56
+
57
+ def read_values(hash)
58
+ extract_values(hash, @keys.dup).map do |val|
59
+ preprocess_value val
60
+ end
61
+ end
62
+
63
+ def preprocess_value(val)
64
+ return nil if val.nil?
65
+ return val unless @preprocessor
66
+ val.public_send @preprocessor
67
+ end
68
+
69
+ def extract_values(hash, current_keys)
70
+ key = current_keys.shift
71
+ return [nil] unless hash.key? key
72
+
73
+ val = hash[key]
74
+ return [val] if current_keys.length == 0
75
+ return val.map { |v| extract_values v, current_keys.dup }.sum if val.is_a? Array
76
+ extract_values val, current_keys
77
+ end
78
+
79
+ def check_operator!
80
+ fail InvalidOperator unless self.class.allowed_operators.include? @operator
81
+ end
82
+
83
+ def check_preprocessor!
84
+ return unless @preprocessor
85
+ fail InvalidPreprocessor unless self.class.allowed_preprocessors.include? @preprocessor
86
+ end
87
+ end
88
+
89
+ attr_reader :error_messages
90
+
91
+ def initialize(rules)
92
+ @rules = []
93
+ (rules || []).each { |r| add_rule r }
94
+ end
95
+
96
+ def accept?(hash)
97
+ @error_messages = []
98
+ @rules.all? do |rule|
99
+ ok = rule.accept? hash
100
+ @error_messages << rule.error_message unless ok
101
+ ok
102
+ end
103
+ end
104
+
105
+ private
106
+
107
+ def add_rule(rule)
108
+ @rules << Rule.new(
109
+ rule['key'], rule['operator'], rule['value'], rule['nil_ok'], rule['preprocessor'], rule['at_least_one']
110
+ )
111
+ end
112
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_filterer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Guillaume Leseur
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Applies rules to a hash deciding if it fits or not
14
+ email: guileseur@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - lib/hash_filterer.rb
22
+ homepage: http://rubygems.org/gems/hash_filterer
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.4.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Applies rules to a hash
46
+ test_files: []