command_search 0.11.2 → 0.11.3

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: c9a7f59d4e3aefbd46963b94d52d18d49d63263ab51ceb31a207ee17ca23bf1b
4
- data.tar.gz: 43fe311f65ec44dd89dcb997f0b61daa1347466f63fae1721c0168eb62bfa377
3
+ metadata.gz: 8c1b899c749aee5b7a81173484fb0fa69eaffd2ab96a26cc3bff7fb7293c5058
4
+ data.tar.gz: 95c7404d1176cbfb7db75b07323c2e51c13d449c7dc111f53db15db04982d5cf
5
5
  SHA512:
6
- metadata.gz: f9751ac591a874cf99009d28fc691b6543b5fc038dbbb6d7a7b063cf47f134da0e8ee500300877565e5c2b4164f82a446d983c9a6730c2eab35a86a6ae6a92ed
7
- data.tar.gz: 47173f4d0904edcb9ddcbd1f2a24e00f9cd70d528c9234cd1840c861cb4cb7e85681bc6b3362fb73fefff095d16cb6aa277f83027185bc5068ed43c3b5b9b757
6
+ metadata.gz: bbcf93763eceef8ce66e809603557bf8a21273c41167fdbac2ab080138af2341dd1e98fb79aa66294e8d6bca32af163b88b38f888d80c929f73f0ec9b5aec3dd
7
+ data.tar.gz: c748c0a6c709c665c1028a46072c6536966d07f6d4e665ca55058e28d11f82253fdc94b3d5d78f594e0b3df2c65d55eff4e33ae865d34abc319e4e788a3d76c0
@@ -40,7 +40,7 @@ module CommandSearch
40
40
  end
41
41
 
42
42
  def search(source, query, options)
43
- if source.respond_to?(:mongo_client) && source.queryable
43
+ if source.respond_to?(:mongo_client)
44
44
  ast = CommandSearch.build(:mongo, query, options)
45
45
  return source.where(ast)
46
46
  end
@@ -2,127 +2,136 @@ module CommandSearch
2
2
  module Parser
3
3
  module_function
4
4
 
5
- def group_parens!(input)
5
+ def group_parens!(ast)
6
6
  i = 0
7
7
  opening_idxs = []
8
- while i < input.length
9
- next i += 1 unless input[i][:type] == :paren
10
- if input[i][:value] == '('
8
+ while i < ast.length
9
+ next i += 1 unless ast[i][:type] == :paren
10
+ if ast[i][:value] == '('
11
11
  opening_idxs.push(i)
12
- input.delete_at(i)
12
+ ast.delete_at(i)
13
13
  next
14
14
  end
15
- input.delete_at(i)
15
+ ast.delete_at(i)
16
16
  opening = opening_idxs.pop()
17
17
  next unless opening
18
- val = input.slice(opening, i - opening)
18
+ val = ast.slice(opening, i - opening)
19
19
  if val.count > 1
20
- input[opening..(i - 1)] = { type: :and, value: val }
20
+ ast[opening..(i - 1)] = { type: :and, value: val }
21
21
  i -= val.length
22
22
  next
23
23
  elsif val.count == 1
24
- input[opening] = val.first
24
+ ast[opening] = val.first
25
25
  end
26
26
  end
27
27
  end
28
28
 
29
- def cluster_cmds!(input)
29
+ def cluster_cmds!(ast)
30
30
  i = 1
31
- while i < input.length - 1
32
- type = input[i][:type]
31
+ while i < ast.length - 1
32
+ type = ast[i][:type]
33
33
  next i += 1 unless type == :colon || type == :compare
34
- input[(i - 1)..(i + 1)] = {
34
+ ast[(i - 1)..(i + 1)] = {
35
35
  type: type,
36
- nest_op: input[i][:value],
37
- value: [input[i - 1], input[i + 1]]
36
+ nest_op: ast[i][:value],
37
+ value: [ast[i - 1], ast[i + 1]]
38
38
  }
39
39
  end
40
40
  end
41
41
 
42
- def cluster_or!(input)
42
+ def cluster_or!(ast)
43
43
  i = 0
44
- while i < input.length
45
- type = input[i][:type]
46
- cluster_or!(input[i][:value]) if type == :and || type == :not
44
+ while i < ast.length
45
+ type = ast[i][:type]
46
+ cluster_or!(ast[i][:value]) if type == :and || type == :not
47
47
  next i += 1 unless type == :pipe
48
- if i == 0 || i == input.length - 1
49
- input.delete_at(i)
48
+ if i == 0 || i == ast.length - 1
49
+ ast.delete_at(i)
50
50
  next
51
51
  end
52
- val = [input[i - 1], input[i + 1]]
52
+ val = [ast[i - 1], ast[i + 1]]
53
53
  cluster_or!(val)
54
- input[(i - 1)..(i + 1)] = { type: :or, value: val }
54
+ ast[i][:type] = :or
55
+ ast[i][:value] = val
56
+ ast.delete_at(i + 1)
57
+ ast.delete_at(i - 1)
55
58
  end
56
59
  end
57
60
 
58
- def cluster_not!(input)
59
- i = input.length
61
+ def cluster_not!(ast)
62
+ i = ast.length
60
63
  while i > 0
61
64
  i -= 1
62
- type = input[i][:type]
63
- cluster_not!(input[i][:value]) if type == :and
65
+ type = ast[i][:type]
66
+ cluster_not!(ast[i][:value]) if type == :and
64
67
  next unless type == :minus
65
- if i == input.length - 1
66
- input.delete_at(i)
68
+ if i == ast.length - 1
69
+ ast.delete_at(i)
67
70
  next
68
71
  end
69
- input[i..(i + 1)] = {
70
- type: :not,
71
- value: [input[i + 1]]
72
- }
72
+ ast[i][:type] = :not
73
+ ast[i][:value] = [ast[i + 1]]
74
+ ast.delete_at(i + 1)
73
75
  end
74
76
  end
75
77
 
76
- def unchain!(input, types)
77
- i = 0
78
- while i < input.length - 2
79
- left = input[i][:type]
80
- right = input[i + 2][:type]
81
- if types.include?(left) && types.include?(right)
82
- input.insert(i + 1, input[i + 1].clone())
83
- end
78
+ def unchain!(ast)
79
+ i = 1
80
+ while i < ast.length - 3
81
+ left = ast[i][:type]
82
+ right = ast[i + 2][:type]
84
83
  i += 1
84
+ next unless left == :colon || left == :compare
85
+ next unless right == :colon || right == :compare
86
+ ast.insert(i, ast[i].clone())
85
87
  end
86
88
  end
87
89
 
88
- def merge_strs(input, (x, y))
89
- if input[y] && input[y][:type] == :str
90
- values = input.map { |x| x[:value] }
91
- { type: :str, value: values.join() }
92
- else
93
- input[x][:type] = :str
94
- input
95
- end
90
+ def r_merge!(ast, i)
91
+ ast[i][:type] = :str
92
+ return unless ast[i + 1] && ast[i + 1][:type] == :str
93
+ ast[i][:value] = ast[i][:value] + ast[i + 1][:value]
94
+ ast.delete_at(i + 1)
96
95
  end
97
96
 
98
- def clean_ununusable!(input)
99
- i = 1
100
- while i < input.length
101
- next i += 1 unless input[i][:type] == :minus
102
- next i += 1 unless [:compare, :colon].include?(input[i - 1][:type])
103
- input[i..i + 1] = merge_strs(input[i..i + 1], [0, 1])
104
- end
105
- i = 0
106
- while i < input.length
107
- next i += 1 if ![:compare, :colon].include?(input[i][:type])
108
- next i += 1 if i > 0 &&
109
- (i < input.count - 1) &&
110
- [:str, :number, :quote].include?(input[i - 1][:type]) &&
111
- [:str, :number, :quote].include?(input[i + 1][:type])
97
+ def l_merge!(ast, i)
98
+ ast[i][:type] = :str
99
+ return unless ast[i - 1] && ast[i - 1][:type] == :str
100
+ ast[i][:value] = ast[i - 1][:value] + ast[i][:value]
101
+ ast.delete_at(i - 1)
102
+ end
112
103
 
113
- input[i..i + 1] = merge_strs(input[i..i + 1], [0, 1])
114
- input[i - 1..i] = merge_strs(input[i - 1..i], [1, 0]) if i > 0
104
+ def clean!(ast)
105
+ return unless ast.any?
106
+ if ast[0][:type] == :colon || ast[0][:type] == :compare
107
+ r_merge!(ast, 0)
108
+ end
109
+ if ast[-1][:type] == :colon || ast[-1][:type] == :compare
110
+ l_merge!(ast, ast.length - 1)
111
+ end
112
+ i = 1
113
+ while i < ast.length - 1
114
+ next i += 1 unless ast[i][:type] == :colon || ast[i][:type] == :compare
115
+ if ast[i + 1][:type] == :minus
116
+ r_merge!(ast, i + 1)
117
+ elsif ![:str, :number, :quote].include?(ast[i - 1][:type])
118
+ r_merge!(ast, i)
119
+ elsif ![:str, :number, :quote].include?(ast[i + 1][:type])
120
+ l_merge!(ast, i)
121
+ else
122
+ i += 1
123
+ end
115
124
  end
116
125
  end
117
126
 
118
- def parse!(input)
119
- clean_ununusable!(input)
120
- unchain!(input, [:colon, :compare])
121
- cluster_cmds!(input)
122
- group_parens!(input)
123
- cluster_not!(input)
124
- cluster_or!(input)
125
- input
127
+ def parse!(ast)
128
+ clean!(ast)
129
+ unchain!(ast)
130
+ cluster_cmds!(ast)
131
+ group_parens!(ast)
132
+ cluster_not!(ast)
133
+ cluster_or!(ast)
134
+ ast
126
135
  end
127
136
  end
128
137
  end
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.11.2
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - zumbalogy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-19 00:00:00.000000000 Z
11
+ date: 2020-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic
@@ -60,7 +60,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  requirements: []
63
- rubygems_version: 3.0.3
63
+ rubygems_version: 3.0.6
64
64
  signing_key:
65
65
  specification_version: 4
66
66
  summary: Let users query collections with ease.