command_search 0.7.2 → 0.8.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06faff1f0e62e2367a2a30bd36f28b0659e4d385c2e5d50ddbf2a3f73c8d7b88
4
- data.tar.gz: 4a644540546ca3f4f1357640c2c5cde78e20bb2769e119a52552dd94ab8e9f35
3
+ metadata.gz: 747b007a76d4735046cb93429393b4b6ca85cb110efa57602f80535d2474bd3c
4
+ data.tar.gz: 3957d43828f6ce74718bfaed58ffd65072806c9cd8db3401e1913b57c8d05e2d
5
5
  SHA512:
6
- metadata.gz: 57839d8fee294a20c5303a958b8b966aef308a0b3366f62e74803998265c41e03b43eb1dd4263f3f9065c14d0e6f761b55eb199ed971e5396b196e61758593d1
7
- data.tar.gz: 8306e47f90008256cb30191329645d8fbb96083f7b68fb489b0b19241686eb9c0eaaccc5b111eddca26f82c75ed6e87e8d4304a0c20c1bd9061803a983484840
6
+ metadata.gz: 3c05e1d7b8adcbb822346980dc0b9efaa1f8dc9c58a27f6cc2cec5ce70bf87b49daa364f733a97bb58fc81bb45a93d2918db02c004d36bd65bb734d2484aced6
7
+ data.tar.gz: c8f2d50a7dcbdde4070dbe1fe1bbac1ce757475d4c07df3d256019c368e6c833b1c0e274fa1a4c9b9fbc996450ef354c03e47469c27f0997d5a95ed474412ecf
@@ -111,7 +111,7 @@ module CommandSearch
111
111
  when :pipe
112
112
  val.any? { |v| check(item, v, fields, command_types) }
113
113
  when :minus
114
- val.none? { |v| check(item, v, fields, command_types) }
114
+ !val.all? { |v| check(item, v, fields, command_types) }
115
115
  when :paren
116
116
  val.all? { |v| check(item, v, fields, command_types) }
117
117
  end
@@ -27,29 +27,16 @@ module CommandSearch
27
27
  else
28
28
  regex = /#{Regexp.escape(str)}/i
29
29
  end
30
- if ast_node[:negate]
31
- forms = fields.map do |field|
32
- if numeric_field?(field, command_types)
33
- { field => { '$ne' => str } }
34
- else
35
- { field => { '$not' => regex } }
36
- end
37
- end
38
- else
39
- forms = fields.map do |field|
40
- if numeric_field?(field, command_types)
41
- { field => str }
42
- else
43
- { field => regex }
44
- end
30
+
31
+ forms = fields.map do |field|
32
+ if numeric_field?(field, command_types)
33
+ { field => str }
34
+ else
35
+ { field => regex }
45
36
  end
46
37
  end
47
38
  return forms if forms.count < 2
48
- if ast_node[:negate]
49
- { '$and' => forms }
50
- else
51
- { '$or' => forms }
52
- end
39
+ { '$or' => forms }
53
40
  end
54
41
 
55
42
  def is_bool_str?(str)
@@ -79,20 +66,14 @@ module CommandSearch
79
66
 
80
67
  if type == Boolean
81
68
  bool = make_boolean(raw_val)
82
- bool = !bool if field_node[:negate]
83
69
  val = [
84
70
  { key => { '$exists' => true } },
85
71
  { key => { '$ne' => !bool } }
86
72
  ]
87
73
  key = '$and'
88
74
  elsif is_bool
89
- # This returns true for empty arrays, when it probably should not.
90
- # Alternativly, something like tags>5 could return things that have more
91
- # than 5 tags in the array.
92
- # https://stackoverflow.com/questions/22367335/mongodb-check-if-value-exists-for-a-field-in-a-document
93
- # val = { '$exists' => make_boolean(raw_val) }
75
+ # These queries return true for empty arrays.
94
76
  bool = make_boolean(raw_val)
95
- bool = !bool if field_node[:negate]
96
77
  if bool
97
78
  val = [
98
79
  { key => { '$exists' => true } },
@@ -132,27 +113,13 @@ module CommandSearch
132
113
  date_begin = date.begin
133
114
  date_end = date.end
134
115
  end
135
- if field_node[:negate]
136
- val = [
137
- { key => { '$gt' => date_end } },
138
- { key => { '$lt' => date_begin } }
139
- ]
140
- key = '$or'
141
- else
142
- val = [
143
- { key => { '$gte' => date_begin } },
144
- { key => { '$lte' => date_end } }
145
- ]
146
- key = '$and'
147
- end
148
- end
149
- if field_node[:negate] && [Numeric, Integer].include?(type)
150
- { key => { '$ne' => val } }
151
- elsif field_node[:negate] && type == String
152
- { key => { '$not' => val } }
153
- else
154
- { key => val }
116
+ val = [
117
+ { key => { '$gte' => date_begin } },
118
+ { key => { '$lte' => date_end } }
119
+ ]
120
+ key = '$and'
155
121
  end
122
+ { key => val }
156
123
  end
157
124
 
158
125
  def build_compare(ast_node, command_types)
@@ -162,12 +129,6 @@ module CommandSearch
162
129
  '<=' => '>=',
163
130
  '>=' => '<='
164
131
  }
165
- reverse_ops = {
166
- '<' => '>=',
167
- '<=' => '>',
168
- '>' => '<=',
169
- '>=' => '<'
170
- }
171
132
  mongo_op_map = {
172
133
  '<' => '$lt',
173
134
  '>' => '$gt',
@@ -180,7 +141,6 @@ module CommandSearch
180
141
  key = first_node[:value]
181
142
  val = last_node[:value]
182
143
  op = ast_node[:nest_op]
183
- op = reverse_ops[op] if first_node[:negate]
184
144
 
185
145
  if keys.include?(val.to_sym)
186
146
  (key, val) = [val, key]
@@ -254,46 +214,29 @@ module CommandSearch
254
214
  end
255
215
 
256
216
  def build_tree!(ast)
257
- mongo_types = { paren: '$and', pipe: '$or', minus: '$not' }
258
- ast.each do |x|
259
- next x unless x[:nest_type]
260
- build_tree!(x[:value])
261
- key = mongo_types[x[:nest_type]]
262
- x[key] = x[:value]
263
- x.delete(:nest_type)
264
- x.delete(:nest_op)
265
- x.delete(:value)
266
- x.delete(:type)
267
- end
268
- end
269
-
270
- def collapse_ors!(ast)
271
- ast.each do |x|
272
- next unless x['$or']
273
- x['$or'].map! { |kid| kid['$or'] || kid }.flatten!
274
- end
275
- end
276
-
277
- def decompose_nots(ast, not_depth = 0)
278
- ast.flat_map do |x|
279
- if x[:nest_type] == :minus
280
- decompose_nots(x[:value], not_depth + 1)
281
- elsif x[:nest_type]
282
- x[:value] = decompose_nots(x[:value], not_depth)
283
- x
217
+ mongo_types = { paren: '$and', pipe: '$or', minus: '$nor' }
218
+ ast.each do |node|
219
+ next node unless node[:nest_type]
220
+ build_tree!(node[:value])
221
+ key = mongo_types[node[:nest_type]]
222
+ if key == '$nor' && node[:value].count > 1
223
+ node[key] = [{ '$and' => node[:value] }]
284
224
  else
285
- x[:negate] = not_depth.odd?
286
- x
225
+ node[key] = node[:value]
287
226
  end
227
+ node['$or'].map! { |x| x['$or'] || x }.flatten! if node['$or']
228
+ node['$nor'].map! { |x| x['$or'] || x }.flatten! if node['$nor']
229
+ node.delete(:nest_type)
230
+ node.delete(:nest_op)
231
+ node.delete(:value)
232
+ node.delete(:type)
288
233
  end
289
234
  end
290
235
 
291
236
  def build_query(ast, fields, command_types = {})
292
237
  out = ast
293
- out = decompose_nots(out)
294
238
  build_searches!(out, fields, command_types)
295
239
  build_tree!(out)
296
- collapse_ors!(out)
297
240
  out = {} if out == []
298
241
  out = out.first if out.count == 1
299
242
  out = { '$and' => out } if out.count > 1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zumbalogy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-08 00:00:00.000000000 Z
11
+ date: 2019-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic