hash_conditions 0.1.11 → 0.1.12
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 +15 -0
- data/.travis.yml +4 -2
- data/Gemfile +3 -0
- data/README.md +3 -0
- data/lib/hash_conditions/core.rb +14 -14
- data/lib/hash_conditions/matcher.rb +14 -14
- data/lib/hash_conditions/parser.rb +4 -2
- data/lib/hash_conditions/version.rb +1 -1
- metadata +5 -19
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NzAzN2UyMzc4M2E0MjkwYzJmOTM0MzQwYWIwNDI2YjJjNGQ4MTRlZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MjExZmQxNGJiMTI1ZmU0OTg1NDEyMWM4OGNlMGZlZGU4NGQzYjEyOQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NDliZDZlMjQwMDEwYmEwOGZjYzM4MTE0ZDY3MDBhM2I1M2E5ZTk4YjAyNGM2
|
10
|
+
MDg1OWM3Mjc1MDg1ZWJlYmJhMzkxMTVmYTlmMzMzMjJlNDZlYmU1NzYzNzIz
|
11
|
+
OWJmYzYzZGJjZDcwY2FjZDEzMjA1NTBmODdhYWNhZWVkMWMyYjU=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NTA1YjFhMmYyNzRkYTExYzkyZWM2NzE5MTY2ZGE4ZjhhODQzYTgwYTgwOTFh
|
14
|
+
MjEyNTMyZGM4NDc1ZjFkZjM5NzliNWNjMmQ4NjgxODkzNjBlOTdiMWUzNjAw
|
15
|
+
ODc1ZWZiNWQ0NGI3MmFmM2NlNTUyMzBlMjZlMGQ4MGZiNjhjOWQ=
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
[](https://codeclimate.com/github/kasthor/hash_conditions)
|
2
|
+
[](https://codeclimate.com/github/kasthor/hash_conditions/coverage)
|
3
|
+
|
1
4
|
# HashConditions
|
2
5
|
|
3
6
|
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/hash_conditions`. To experiment with that code, run `bin/console` for an interactive prompt.
|
data/lib/hash_conditions/core.rb
CHANGED
@@ -9,14 +9,14 @@ module HashConditions
|
|
9
9
|
@@modules = []
|
10
10
|
end
|
11
11
|
|
12
|
-
def bundles
|
12
|
+
def bundles
|
13
13
|
@@bundles ||= []
|
14
14
|
end
|
15
15
|
|
16
16
|
def add_bundle name
|
17
17
|
bundles << name
|
18
18
|
end
|
19
|
-
def contains_bundle name
|
19
|
+
def contains_bundle name
|
20
20
|
bundles.include? name
|
21
21
|
end
|
22
22
|
|
@@ -24,13 +24,13 @@ module HashConditions
|
|
24
24
|
writer = block if block
|
25
25
|
modules << ModuleMatcher.new(matcher, writer, operations)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# Iterator
|
29
29
|
|
30
30
|
def iterator conditions, options = {}
|
31
31
|
options = { glue: :and }.merge options
|
32
32
|
|
33
|
-
result = case conditions
|
33
|
+
result = case conditions
|
34
34
|
when ::Hash
|
35
35
|
conditions.map do | key, value |
|
36
36
|
case key
|
@@ -46,7 +46,7 @@ module HashConditions
|
|
46
46
|
iterator condition, options
|
47
47
|
end
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
50
|
options[:finalize].call result, options
|
51
51
|
end
|
52
52
|
|
@@ -55,10 +55,10 @@ module HashConditions
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def eval_expression value
|
58
|
-
raise "Invalid eval expression" unless value.is_a? Array and value.length == 3
|
58
|
+
raise "Invalid eval expression" unless value.is_a? Array and value.length == 3
|
59
59
|
|
60
60
|
Hash[ [ :key, :operator, :value ].zip( value ) ].tap do | exp |
|
61
|
-
exp[ :operator ] = get_op exp[ :operator ]
|
61
|
+
exp[ :operator ] = get_op exp[ :operator ]
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
@@ -79,8 +79,8 @@ module HashConditions
|
|
79
79
|
|
80
80
|
result[:operator] = get_op key
|
81
81
|
result[:value] = value
|
82
|
-
else
|
83
|
-
case
|
82
|
+
else
|
83
|
+
case
|
84
84
|
when value.keys.include?('$between')
|
85
85
|
result[:operator] = :between
|
86
86
|
result[:value] = value.values_at [ '$between', '$and' ]
|
@@ -102,7 +102,7 @@ module HashConditions
|
|
102
102
|
def ops
|
103
103
|
{
|
104
104
|
:== => [ '$eq', '$equal' ],
|
105
|
-
:!= => [ '$ne' ],
|
105
|
+
:!= => [ '$ne', '$not_equal' ],
|
106
106
|
:> => [ '$gt' ],
|
107
107
|
:< => [ '$lt' ],
|
108
108
|
:>= => [ '$gte' ],
|
@@ -126,7 +126,7 @@ module HashConditions
|
|
126
126
|
case data
|
127
127
|
when /\d{4}-\d{2}-\d{2}[\sT]\d{2}:\d{2}:\d{2}(\.\d{3})?Z?/ then DateTime.parse( data ).to_time
|
128
128
|
else data
|
129
|
-
end
|
129
|
+
end
|
130
130
|
else
|
131
131
|
data
|
132
132
|
end
|
@@ -143,17 +143,17 @@ module HashConditions
|
|
143
143
|
mod = _ext_get_module key,condition, options
|
144
144
|
parser = mod.replacement
|
145
145
|
|
146
|
-
case parser
|
146
|
+
case parser
|
147
147
|
when String then options[:result].call(extract_expression( parser, condition ), options)
|
148
148
|
when Hash then _ext_read_module( { '$eval' => [ parser, op, value ] }, options )
|
149
149
|
when Proc then _ext_read_module( parser.call( key, condition, options ), options )
|
150
|
-
end
|
150
|
+
end
|
151
151
|
end
|
152
152
|
|
153
153
|
def _ext_get_module key, condition, options
|
154
154
|
modules.select{ |m| m.for_operation? options[:operation] }.find do | matcher |
|
155
155
|
matcher.apply_for key, condition
|
156
|
-
end
|
156
|
+
end
|
157
157
|
end
|
158
158
|
|
159
159
|
def _ext_read_module parser_output, options
|
@@ -1,8 +1,8 @@
|
|
1
1
|
module HashConditions
|
2
2
|
class Matcher
|
3
|
-
extend Core
|
3
|
+
extend Core
|
4
4
|
|
5
|
-
def self.configurations
|
5
|
+
def self.configurations
|
6
6
|
@@configurations ||= {}
|
7
7
|
end
|
8
8
|
|
@@ -21,20 +21,20 @@ module HashConditions
|
|
21
21
|
match_single hash, expression, options
|
22
22
|
},
|
23
23
|
finalize: lambda{ | array, options |
|
24
|
-
finalize hash, array, options
|
24
|
+
finalize hash, array, options
|
25
25
|
}
|
26
26
|
}.merge options
|
27
27
|
|
28
28
|
iterator conditions, options
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
def self.finalize hash, array, options
|
32
32
|
case options[:glue]
|
33
33
|
when :or then array.any?
|
34
34
|
when :and then array.all?
|
35
35
|
end
|
36
|
-
end
|
37
|
-
|
36
|
+
end
|
37
|
+
|
38
38
|
ARITMETIC_OPERATORS = {
|
39
39
|
'$add' => :+,
|
40
40
|
'$substract' => :-,
|
@@ -69,13 +69,13 @@ module HashConditions
|
|
69
69
|
end
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def self.match_single hash, expression, options
|
74
74
|
hash_value = get_key hash, expression[:key], options
|
75
75
|
comparisson_value = expression[ :value ]
|
76
76
|
|
77
77
|
case expression[:operator]
|
78
|
-
when :==
|
78
|
+
when :==
|
79
79
|
if configuration( :force_string_comparations )
|
80
80
|
hash_value = hash_value.to_s
|
81
81
|
comparisson_value = comparisson_value.to_s
|
@@ -86,7 +86,7 @@ module HashConditions
|
|
86
86
|
hash_value = hash_value.to_s
|
87
87
|
comparisson_value = comparisson_value.map(&:to_s)
|
88
88
|
end
|
89
|
-
|
89
|
+
|
90
90
|
comparisson_value.include? hash_value
|
91
91
|
when :between
|
92
92
|
hash_value > comparisson_value[0] and hash_value < comparisson_value[1]
|
@@ -99,7 +99,7 @@ module HashConditions
|
|
99
99
|
|
100
100
|
def self.when hash, query
|
101
101
|
now_result = match hash, query
|
102
|
-
test_times = critical_times( hash, time_expressions( query ) )
|
102
|
+
test_times = critical_times( hash, time_expressions( query ) )
|
103
103
|
test_times.
|
104
104
|
sort.
|
105
105
|
drop_while{ |t| t < Time.now }.
|
@@ -108,9 +108,9 @@ module HashConditions
|
|
108
108
|
|
109
109
|
def self.critical_times hash, expressions
|
110
110
|
expressions.
|
111
|
-
map{ | e |
|
111
|
+
map{ | e |
|
112
112
|
case e[:operator]
|
113
|
-
when :<, :<=, :>, :>= then
|
113
|
+
when :<, :<=, :>, :>= then
|
114
114
|
diff = e[:value] - get_key(hash, e[:key]) + 1
|
115
115
|
when :==, :!= then Time.now + s[:diff]
|
116
116
|
diff = e[:value] - get_key(hash, e[:key])
|
@@ -124,8 +124,8 @@ module HashConditions
|
|
124
124
|
end
|
125
125
|
|
126
126
|
def self.time_expressions conditions
|
127
|
-
expressions = []
|
128
|
-
iterator conditions,
|
127
|
+
expressions = []
|
128
|
+
iterator conditions,
|
129
129
|
operation: :match,
|
130
130
|
result: lambda{ | expression, options |
|
131
131
|
expressions << expression if uses_now? expression
|
@@ -22,16 +22,18 @@ module HashConditions
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def self._parse_key_value_condition expression
|
25
|
-
# "#{key} #{_parse_value_condition condition}"
|
25
|
+
# "#{key} #{_parse_value_condition condition}"
|
26
26
|
comparisson = case expression[:operator]
|
27
27
|
when :==
|
28
28
|
"= #{_parse_value expression[:value]}"
|
29
|
+
when :!=
|
30
|
+
"!= #{_parse_value expression[:value]}"
|
29
31
|
when :>, :<, :>=, :<=
|
30
32
|
"#{ expression[:operator] } #{_parse_value expression[:value]}"
|
31
33
|
when :in
|
32
34
|
"IN ( #{ expression[:value].map{ |v| _parse_value v }.join ", " } )"
|
33
35
|
when :contains
|
34
|
-
"LIKE #{ _parse_value(expression[:value], '%', '%') }"
|
36
|
+
"LIKE #{ _parse_value(expression[:value], '%', '%') }"
|
35
37
|
when :between
|
36
38
|
"BETWEEN #{ _parse_value expression[:value].shift } AND #{ _parse_value expression[:value].shift }"
|
37
39
|
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_conditions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.12
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Giancarlo Palavicini
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2015-06-
|
11
|
+
date: 2015-06-22 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: bundler
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :development
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: rake
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rspec
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -90,32 +83,25 @@ files:
|
|
90
83
|
homepage: http://kasthor.com
|
91
84
|
licenses:
|
92
85
|
- MIT
|
86
|
+
metadata: {}
|
93
87
|
post_install_message:
|
94
88
|
rdoc_options: []
|
95
89
|
require_paths:
|
96
90
|
- lib
|
97
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
92
|
requirements:
|
100
93
|
- - ! '>='
|
101
94
|
- !ruby/object:Gem::Version
|
102
95
|
version: '0'
|
103
|
-
segments:
|
104
|
-
- 0
|
105
|
-
hash: -563002779968632233
|
106
96
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
-
none: false
|
108
97
|
requirements:
|
109
98
|
- - ! '>='
|
110
99
|
- !ruby/object:Gem::Version
|
111
100
|
version: '0'
|
112
|
-
segments:
|
113
|
-
- 0
|
114
|
-
hash: -563002779968632233
|
115
101
|
requirements: []
|
116
102
|
rubyforge_project:
|
117
|
-
rubygems_version:
|
103
|
+
rubygems_version: 2.4.8
|
118
104
|
signing_key:
|
119
|
-
specification_version:
|
105
|
+
specification_version: 4
|
120
106
|
summary: Ability to parse and match hashes against Mongo-like querys
|
121
107
|
test_files: []
|