keyword_search 1.4.1 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +21 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +112 -0
- data/Rakefile +11 -30
- data/keyword_search.gemspec +19 -44
- data/lib/keyword_search.rb +1638 -664
- data/lib/keyword_search/definition.rb +12 -12
- data/lib/keyword_search/version.rb +3 -0
- data/test/test_keyword_search.rb +33 -34
- metadata +66 -42
- data/History.txt +0 -36
- data/README.markdown +0 -125
- data/VERSION +0 -1
@@ -1,17 +1,17 @@
|
|
1
1
|
module KeywordSearch
|
2
|
-
|
2
|
+
|
3
3
|
class Definition
|
4
|
-
|
4
|
+
|
5
5
|
class Keyword
|
6
|
-
|
6
|
+
|
7
7
|
attr_reader :name, :description, :handler
|
8
8
|
def initialize(name, description=nil, &handler)
|
9
9
|
@name, @description = name, description
|
10
10
|
@handler = handler
|
11
11
|
end
|
12
|
-
|
12
|
+
|
13
13
|
def handle(value, sign)
|
14
|
-
# If the handler is only expecting one argument,
|
14
|
+
# If the handler is only expecting one argument,
|
15
15
|
# only give them the positive matches
|
16
16
|
if handler.arity == 1
|
17
17
|
handler.call(value) if sign
|
@@ -19,14 +19,14 @@ module KeywordSearch
|
|
19
19
|
handler.call(value, sign)
|
20
20
|
end
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
end
|
24
24
|
|
25
25
|
def initialize
|
26
26
|
@default_keyword = nil
|
27
27
|
yield self if block_given?
|
28
28
|
end
|
29
|
-
|
29
|
+
|
30
30
|
def keywords
|
31
31
|
@keywords ||= []
|
32
32
|
end
|
@@ -34,24 +34,24 @@ module KeywordSearch
|
|
34
34
|
def keyword(name, description=nil, &block)
|
35
35
|
keywords << Keyword.new(name, description, &block)
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
def default_keyword(name)
|
39
39
|
@default_keyword = name
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def handle(key, values)
|
43
43
|
key = @default_keyword if key == :default
|
44
44
|
return false unless key
|
45
45
|
true_values, false_values = *values.partition { |v| v[1] }
|
46
|
-
|
46
|
+
|
47
47
|
# Get just the values
|
48
48
|
true_values.collect! { |v| v[0] }
|
49
49
|
false_values.collect! { |v| v[0] }
|
50
|
-
|
50
|
+
|
51
51
|
if k = keywords.detect { |kw| kw.name == key.to_sym}
|
52
52
|
k.handle(true_values, true) unless true_values.empty?
|
53
53
|
k.handle(false_values, false) unless false_values.empty?
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
57
|
-
end
|
57
|
+
end
|
data/test/test_keyword_search.rb
CHANGED
@@ -1,11 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
|
3
|
-
require 'rubygems' rescue nil
|
4
|
-
require 'test/spec'
|
1
|
+
require 'minitest/spec'
|
2
|
+
require 'minitest/autorun'
|
5
3
|
|
6
4
|
require File.dirname(__FILE__) + '/../lib/keyword_search'
|
7
5
|
|
8
|
-
|
6
|
+
describe "KeywordSearch" do
|
9
7
|
|
10
8
|
NAME_AND_AGE = %<bruce williams age:26>
|
11
9
|
NAME_QUOTED_AND_AGE = %<"bruce williams" age:26>
|
@@ -18,7 +16,7 @@ context "KeywordSearch" do
|
|
18
16
|
NAME_AND_GROUPED_QUOTED_AGES = %<coda hale age:("27" 34 '48')>
|
19
17
|
GROUPED_NAMES_AND_AGE = %<(coda bruce 'hale' "williams") age:20>
|
20
18
|
|
21
|
-
|
19
|
+
it "default keyword" do
|
22
20
|
result = nil
|
23
21
|
KeywordSearch.search(NAME_AND_AGE) do |with|
|
24
22
|
with.default_keyword :name
|
@@ -26,10 +24,11 @@ context "KeywordSearch" do
|
|
26
24
|
result = values.join(' ')
|
27
25
|
end
|
28
26
|
end
|
27
|
+
result.must_equal 'bruce williams'
|
29
28
|
assert_equal 'bruce williams', result
|
30
29
|
end
|
31
30
|
|
32
|
-
|
31
|
+
it "grouped default keywords" do
|
33
32
|
result = nil
|
34
33
|
KeywordSearch.search(GROUPED_NAMES_AND_AGE) do |with|
|
35
34
|
with.default_keyword :name
|
@@ -40,7 +39,7 @@ context "KeywordSearch" do
|
|
40
39
|
assert_equal ['coda', 'bruce', 'hale', 'williams'], result
|
41
40
|
end
|
42
41
|
|
43
|
-
|
42
|
+
it "unquoted keyword term" do
|
44
43
|
result = nil
|
45
44
|
KeywordSearch.search(NAME_AND_AGE) do |with|
|
46
45
|
with.keyword :age do |values|
|
@@ -50,7 +49,7 @@ context "KeywordSearch" do
|
|
50
49
|
assert_equal 26, result
|
51
50
|
end
|
52
51
|
|
53
|
-
|
52
|
+
it "unquoted grouped keyword term" do
|
54
53
|
result = nil
|
55
54
|
KeywordSearch.search(NAME_AND_GROUPED_AGE) do |with|
|
56
55
|
with.keyword :age do |values|
|
@@ -60,7 +59,7 @@ context "KeywordSearch" do
|
|
60
59
|
assert_equal 27, result
|
61
60
|
end
|
62
61
|
|
63
|
-
|
62
|
+
it "quoted grouped keyword term" do
|
64
63
|
result = nil
|
65
64
|
KeywordSearch.search(NAME_AND_GROUPED_QUOTED_AGE) do |with|
|
66
65
|
with.keyword :age do |values|
|
@@ -70,7 +69,7 @@ context "KeywordSearch" do
|
|
70
69
|
assert_equal 27, result
|
71
70
|
end
|
72
71
|
|
73
|
-
|
72
|
+
it "mixed grouped keyword terms" do
|
74
73
|
result = nil
|
75
74
|
KeywordSearch.search(NAME_AND_GROUPED_QUOTED_AGES) do |with|
|
76
75
|
with.keyword :age do |values|
|
@@ -80,7 +79,7 @@ context "KeywordSearch" do
|
|
80
79
|
assert_equal [27, 34, 48], result
|
81
80
|
end
|
82
81
|
|
83
|
-
|
82
|
+
it "quoted default keyword term" do
|
84
83
|
result = nil
|
85
84
|
KeywordSearch.search(NAME_QUOTED_AND_AGE) do |with|
|
86
85
|
with.default_keyword :name
|
@@ -91,7 +90,7 @@ context "KeywordSearch" do
|
|
91
90
|
assert_equal 'bruce williams', result
|
92
91
|
end
|
93
92
|
|
94
|
-
|
93
|
+
it "quoted keyword term" do
|
95
94
|
result = nil
|
96
95
|
KeywordSearch.search(NAME_AND_QUOTED_AGE) do |with|
|
97
96
|
with.keyword :age do |values|
|
@@ -101,7 +100,7 @@ context "KeywordSearch" do
|
|
101
100
|
assert_equal 26, result
|
102
101
|
end
|
103
102
|
|
104
|
-
|
103
|
+
it "quoted keyword term with whitespace" do
|
105
104
|
result = nil
|
106
105
|
KeywordSearch.search(DEFAULT_AGE_WITH_QUOTED_AGE) do |with|
|
107
106
|
with.default_keyword :age
|
@@ -112,7 +111,7 @@ context "KeywordSearch" do
|
|
112
111
|
assert_equal 'bruce williams', result
|
113
112
|
end
|
114
113
|
|
115
|
-
|
114
|
+
it "single quoted keyword term with whitespace" do
|
116
115
|
result = nil
|
117
116
|
r = KeywordSearch.search(DEFAULT_AGE_WITH_SINGLE_QUOTED_AGE) do |with|
|
118
117
|
with.default_keyword :age
|
@@ -123,7 +122,7 @@ context "KeywordSearch" do
|
|
123
122
|
assert_equal 'bruce williams', result
|
124
123
|
end
|
125
124
|
|
126
|
-
|
125
|
+
it "nested single quote is accumulated" do
|
127
126
|
result = nil
|
128
127
|
KeywordSearch.search(NAME_WITH_NESTED_SINGLE_QUOTES) do |with|
|
129
128
|
with.default_keyword :name
|
@@ -134,7 +133,7 @@ context "KeywordSearch" do
|
|
134
133
|
assert_equal %<d'arcy d'uberville>, result
|
135
134
|
end
|
136
135
|
|
137
|
-
|
136
|
+
it "nested double quote is accumulated" do
|
138
137
|
result = nil
|
139
138
|
KeywordSearch.search(%<'he was called "jake"'>) do |with|
|
140
139
|
with.default_keyword :text
|
@@ -145,7 +144,7 @@ context "KeywordSearch" do
|
|
145
144
|
assert_equal %<he was called "jake">, result
|
146
145
|
end
|
147
146
|
|
148
|
-
|
147
|
+
it "bare single quote in unquoted literal is accumulated" do
|
149
148
|
result = nil
|
150
149
|
KeywordSearch.search(%<bruce's age:27>) do |with|
|
151
150
|
with.default_keyword :text
|
@@ -156,7 +155,7 @@ context "KeywordSearch" do
|
|
156
155
|
assert_equal %<bruce's>, result
|
157
156
|
end
|
158
157
|
|
159
|
-
|
158
|
+
it "single quoted literal is accumulated" do
|
160
159
|
result = nil
|
161
160
|
KeywordSearch.search(%<foo 'bruce williams' age:27>) do |with|
|
162
161
|
with.default_keyword :text
|
@@ -167,7 +166,7 @@ context "KeywordSearch" do
|
|
167
166
|
assert_equal %<bruce williams>, result
|
168
167
|
end
|
169
168
|
|
170
|
-
|
169
|
+
it "period in literal is accumulated" do
|
171
170
|
result = nil
|
172
171
|
KeywordSearch.search(%<okay... age:27>) do |with|
|
173
172
|
with.default_keyword :text
|
@@ -178,7 +177,7 @@ context "KeywordSearch" do
|
|
178
177
|
assert_equal %<okay...>, result
|
179
178
|
end
|
180
179
|
|
181
|
-
|
180
|
+
it "parse error results in exception" do
|
182
181
|
assert_raises(KeywordSearch::ParseError) do
|
183
182
|
KeywordSearch.search(%<we_do_not_allow:! or ::>) do |with|
|
184
183
|
with.default_keyword :text
|
@@ -189,7 +188,7 @@ context "KeywordSearch" do
|
|
189
188
|
end
|
190
189
|
end
|
191
190
|
|
192
|
-
|
191
|
+
it "can use apostrophes in unquoted literal" do
|
193
192
|
result = nil
|
194
193
|
KeywordSearch.search(%<d'correct>) do |with|
|
195
194
|
with.default_keyword :text
|
@@ -200,7 +199,7 @@ context "KeywordSearch" do
|
|
200
199
|
assert_equal "d'correct", result
|
201
200
|
end
|
202
201
|
|
203
|
-
|
202
|
+
it "can use apostrophes in unquoted literal values" do
|
204
203
|
result = nil
|
205
204
|
KeywordSearch.search(%<text:d'correct>) do |with|
|
206
205
|
with.default_keyword :text
|
@@ -211,7 +210,7 @@ context "KeywordSearch" do
|
|
211
210
|
assert_equal "d'correct", result
|
212
211
|
end
|
213
212
|
|
214
|
-
|
213
|
+
it "cannot use an apostrophe at the beginning on an unquoted literal" do
|
215
214
|
assert_raises(KeywordSearch::ParseError) do
|
216
215
|
KeywordSearch.search(%<'thisiswrong>) do |with|
|
217
216
|
with.default_keyword :text
|
@@ -222,7 +221,7 @@ context "KeywordSearch" do
|
|
222
221
|
end
|
223
222
|
end
|
224
223
|
|
225
|
-
|
224
|
+
it "keywords are case sensitive" do
|
226
225
|
result = nil
|
227
226
|
KeywordSearch.search(%<Text:justtesting>) do |with|
|
228
227
|
with.keyword :text do |values|
|
@@ -235,7 +234,7 @@ context "KeywordSearch" do
|
|
235
234
|
assert_equal :big, result
|
236
235
|
end
|
237
236
|
|
238
|
-
|
237
|
+
it "values are case sensitive" do
|
239
238
|
result = nil
|
240
239
|
KeywordSearch.search(%<text:Big>) do |with|
|
241
240
|
with.keyword :text do |values|
|
@@ -245,7 +244,7 @@ context "KeywordSearch" do
|
|
245
244
|
assert_equal 'Big', result
|
246
245
|
end
|
247
246
|
|
248
|
-
|
247
|
+
it "spaces are condensed" do
|
249
248
|
result = nil
|
250
249
|
KeywordSearch.search(%< this is some text >) do |with|
|
251
250
|
with.default_keyword :text
|
@@ -256,7 +255,7 @@ context "KeywordSearch" do
|
|
256
255
|
assert_equal %w(this is some text), result
|
257
256
|
end
|
258
257
|
|
259
|
-
|
258
|
+
it "an empty search is successful" do
|
260
259
|
result = nil
|
261
260
|
KeywordSearch.search(%<>) do |with|
|
262
261
|
with.default_keyword :text
|
@@ -267,7 +266,7 @@ context "KeywordSearch" do
|
|
267
266
|
assert_nil result
|
268
267
|
end
|
269
268
|
|
270
|
-
|
269
|
+
it 'a negative search' do
|
271
270
|
result = nil
|
272
271
|
|
273
272
|
KeywordSearch.search(%<-site:google.com>) do |with|
|
@@ -278,7 +277,7 @@ context "KeywordSearch" do
|
|
278
277
|
assert_equal [ [ 'google.com' ], false ], result
|
279
278
|
end
|
280
279
|
|
281
|
-
|
280
|
+
it 'a positive search' do
|
282
281
|
result = nil
|
283
282
|
|
284
283
|
KeywordSearch.search(%<+site:google.com>) do |with|
|
@@ -289,7 +288,7 @@ context "KeywordSearch" do
|
|
289
288
|
assert_equal [ [ 'google.com' ], true ], result
|
290
289
|
end
|
291
290
|
|
292
|
-
|
291
|
+
it 'a search with no sign' do
|
293
292
|
result = nil
|
294
293
|
|
295
294
|
KeywordSearch.search(%<site:google.com>) do |with|
|
@@ -300,7 +299,7 @@ context "KeywordSearch" do
|
|
300
299
|
assert_equal [ [ 'google.com' ], true ], result
|
301
300
|
end
|
302
301
|
|
303
|
-
|
302
|
+
it 'a term should default to positive with no sign' do
|
304
303
|
result = nil
|
305
304
|
|
306
305
|
KeywordSearch.search(%<-site:google.com inurl:atom>) do |with|
|
@@ -311,7 +310,7 @@ context "KeywordSearch" do
|
|
311
310
|
assert_equal [ %w(atom), true ], result
|
312
311
|
end
|
313
312
|
|
314
|
-
|
313
|
+
it 'a negative and positive search to the default keyword' do
|
315
314
|
result = []
|
316
315
|
|
317
316
|
KeywordSearch.search(%<text -google.com search>) do |with|
|
@@ -323,7 +322,7 @@ context "KeywordSearch" do
|
|
323
322
|
assert_equal [ [ %w(text search), true ], [ %w(google.com), false ] ], result
|
324
323
|
end
|
325
324
|
|
326
|
-
|
325
|
+
it 'a negative search to the default keyword with quotes' do
|
327
326
|
result = []
|
328
327
|
|
329
328
|
KeywordSearch.search(%<-google.com>) do |with|
|
metadata
CHANGED
@@ -1,67 +1,91 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: keyword_search
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Bruce Williams
|
8
8
|
- Eric Lindvall
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
12
|
+
date: 2013-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.3'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.3'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - ! '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
17
42
|
description:
|
18
|
-
email:
|
19
|
-
-
|
43
|
+
email:
|
44
|
+
- brwcodes@gmail.com
|
20
45
|
- eric@sevenscale.com
|
21
46
|
executables: []
|
22
|
-
|
23
47
|
extensions: []
|
24
|
-
|
25
|
-
|
26
|
-
-
|
27
|
-
|
28
|
-
-
|
48
|
+
extra_rdoc_files: []
|
49
|
+
files:
|
50
|
+
- .gitignore
|
51
|
+
- .travis.yml
|
52
|
+
- Gemfile
|
53
|
+
- LICENSE
|
29
54
|
- Manifest
|
30
|
-
- README.
|
55
|
+
- README.md
|
31
56
|
- Rakefile
|
32
|
-
- VERSION
|
33
57
|
- keyword_search.gemspec
|
34
58
|
- lib/keyword_search.rb
|
35
59
|
- lib/keyword_search.rl
|
36
60
|
- lib/keyword_search/definition.rb
|
61
|
+
- lib/keyword_search/version.rb
|
37
62
|
- test/test_keyword_search.rb
|
38
|
-
has_rdoc: true
|
39
63
|
homepage: http://github.com/bruce/keyword_search
|
40
|
-
licenses:
|
41
|
-
|
64
|
+
licenses:
|
65
|
+
- MIT
|
66
|
+
metadata: {}
|
42
67
|
post_install_message:
|
43
|
-
rdoc_options:
|
68
|
+
rdoc_options:
|
44
69
|
- --charset=UTF-8
|
45
|
-
require_paths:
|
70
|
+
require_paths:
|
46
71
|
- lib
|
47
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
requirements:
|
49
|
-
- -
|
50
|
-
- !ruby/object:Gem::Version
|
51
|
-
version:
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
version: "0"
|
58
|
-
version:
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
59
82
|
requirements: []
|
60
|
-
|
61
|
-
|
62
|
-
rubygems_version: 1.3.5
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 2.0.3
|
63
85
|
signing_key:
|
64
|
-
specification_version:
|
65
|
-
summary: Generic library to parse GMail-style search strings for keyword/value pairs;
|
66
|
-
|
86
|
+
specification_version: 4
|
87
|
+
summary: Generic library to parse GMail-style search strings for keyword/value pairs;
|
88
|
+
supports definition of valid keywords and handling of quoted values.
|
89
|
+
test_files:
|
67
90
|
- test/test_keyword_search.rb
|
91
|
+
has_rdoc:
|