search_lingo 2.0.0.pre3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/lib/search_lingo/abstract_search.rb +34 -24
- data/lib/search_lingo/parsers/date_parser.rb +2 -6
- data/lib/search_lingo/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d697959cb461bb00ba17b4dbd3fe71037db81c65d3253fc22c42c27b69228f2e
|
4
|
+
data.tar.gz: 2a96f4b480d60d052bd1bf7f7ae02f645386144dbebccfacaa9ecfbf56fbbd30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa98221d0840199c0ed21f96412f39fd82567210d8f99d1b2767a0b8f718ba2f1039f1624e7f9d04a63bd7b0c6a22d00ecbe81ddb4c8986322dfdda99da84b4f
|
7
|
+
data.tar.gz: 3ecc60bea0e32cc52548bbe5999e80704bc52e89ec0218f3076473220c14f8f4b2a5214bd8dadca5490b90a2229291dea960289819f53c9689bb4f3ab8859ac7
|
data/.travis.yml
CHANGED
@@ -33,7 +33,7 @@ module SearchLingo
|
|
33
33
|
# end
|
34
34
|
# end
|
35
35
|
class AbstractSearch
|
36
|
-
attr_reader :query, :scope
|
36
|
+
attr_reader :query, :scope, :logger
|
37
37
|
|
38
38
|
##
|
39
39
|
# Instantiates a new search object. +query+ is the string that is to be
|
@@ -42,9 +42,10 @@ module SearchLingo
|
|
42
42
|
# should be sent, e.g., an +ActiveRecord::Relation+.
|
43
43
|
#
|
44
44
|
# MySearchClass.new 'foo bar: baz "froz quux"', Task.all
|
45
|
-
def initialize(query, scope)
|
46
|
-
@query
|
47
|
-
@scope
|
45
|
+
def initialize(query, scope, logger: nil)
|
46
|
+
@query = query || ''
|
47
|
+
@scope = scope
|
48
|
+
@logger = logger
|
48
49
|
end
|
49
50
|
|
50
51
|
##
|
@@ -98,26 +99,12 @@ module SearchLingo
|
|
98
99
|
##
|
99
100
|
# Load search results by composing query string tokens into a query chain.
|
100
101
|
#
|
101
|
-
# @query is
|
102
|
-
#
|
103
|
-
#
|
104
|
-
# the token is compound, the token is simplified and reprocessed as before.
|
105
|
-
# If still no parser succeeds, fall back on +#default_parse+.
|
102
|
+
# @query is broken down into tokens and parses each one in turn. The
|
103
|
+
# results of parsing each token are chained onto the end of +scope+ to
|
104
|
+
# compose the query.
|
106
105
|
def load_results
|
107
106
|
tokenizer.reduce(scope) do |chain, token|
|
108
|
-
|
109
|
-
# 1. Try each parser with token until :match is thrown.
|
110
|
-
parse token, chain
|
111
|
-
|
112
|
-
# 2. If :match not thrown and token is compund, simplify and retry.
|
113
|
-
if token.compound?
|
114
|
-
token = tokenizer.simplify
|
115
|
-
parse token, chain
|
116
|
-
end
|
117
|
-
|
118
|
-
# 3. If :match still not thrown, fall back on default parser.
|
119
|
-
default_parse token, chain
|
120
|
-
end
|
107
|
+
parse token, chain
|
121
108
|
end
|
122
109
|
end
|
123
110
|
|
@@ -127,6 +114,26 @@ module SearchLingo
|
|
127
114
|
@tokenizer ||= Tokenizer.new query
|
128
115
|
end
|
129
116
|
|
117
|
+
##
|
118
|
+
# Passes +token+ and +chain+ through the array of parsers until +:match+ is
|
119
|
+
# thrown. If none of the parsers match and the token is compound,
|
120
|
+
# simplifies the token and reruns the parsers. If no parsers match after
|
121
|
+
# the second pass or if the token was not compound, falls back on
|
122
|
+
# `#default_parse`.
|
123
|
+
def parse(token, chain)
|
124
|
+
catch(:match) do
|
125
|
+
run_parsers token, chain
|
126
|
+
|
127
|
+
if token.compound?
|
128
|
+
token = tokenizer.simplify
|
129
|
+
run_parsers token, chain
|
130
|
+
end
|
131
|
+
|
132
|
+
logger&.debug "default_parse token=#{token.inspect}"
|
133
|
+
default_parse token, chain
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
130
137
|
##
|
131
138
|
# Passes +token+ to each parser in turn. If a parser succeeds, throws
|
132
139
|
# +:match+ with the result.
|
@@ -134,10 +141,13 @@ module SearchLingo
|
|
134
141
|
# A parser succeeds if +call+ returns a truthy value. A successful parser
|
135
142
|
# will typically send something to +chain+ and return the result. In this
|
136
143
|
# way, the tokens of the search are reduced into a composed query.
|
137
|
-
def
|
144
|
+
def run_parsers(token, chain)
|
138
145
|
parsers.each do |parser|
|
139
146
|
result = parser.call token, chain
|
140
|
-
|
147
|
+
if result
|
148
|
+
logger&.debug "parser:#{parser.inspect} token=#{token.inspect}"
|
149
|
+
throw :match, result
|
150
|
+
end
|
141
151
|
end
|
142
152
|
nil
|
143
153
|
end
|
@@ -39,11 +39,7 @@ module SearchLingo
|
|
39
39
|
def initialize(column, modifier: nil, &block)
|
40
40
|
@column = column
|
41
41
|
@prefix = /#{modifier}:[[:space:]]*/ if modifier
|
42
|
-
@append =
|
43
|
-
block
|
44
|
-
else
|
45
|
-
->(chain) { chain }
|
46
|
-
end
|
42
|
+
@append = block_given? ? block : ->(chain) { chain }
|
47
43
|
end
|
48
44
|
|
49
45
|
##
|
@@ -63,7 +59,7 @@ module SearchLingo
|
|
63
59
|
format '#<%<cls>s @prefix=%<prefix>s @column=%<column>s>',
|
64
60
|
cls: self.class,
|
65
61
|
prefix: prefix.inspect,
|
66
|
-
column: column.inspect
|
62
|
+
column: "#{column.relation.name}.#{column.name}".inspect
|
67
63
|
end
|
68
64
|
|
69
65
|
private
|
data/lib/search_lingo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: search_lingo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Parker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-11-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -185,12 +185,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
185
185
|
version: '2.3'
|
186
186
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
187
187
|
requirements:
|
188
|
-
- - "
|
188
|
+
- - ">="
|
189
189
|
- !ruby/object:Gem::Version
|
190
|
-
version:
|
190
|
+
version: '0'
|
191
191
|
requirements: []
|
192
192
|
rubyforge_project:
|
193
|
-
rubygems_version: 2.7.
|
193
|
+
rubygems_version: 2.7.8
|
194
194
|
signing_key:
|
195
195
|
specification_version: 4
|
196
196
|
summary: Framework for defining and parsing search queries.
|