keyword_search 1.0.4 → 1.0.5

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.
data/History.txt CHANGED
@@ -1,3 +1,8 @@
1
+ = 1.0.5 / 2007-01-29
2
+
3
+ * Added single quoting support and nested quoting (apostrophes, etc)
4
+ * Added a few more punctuation marks as valid input
5
+
1
6
  = 1.0.4 / 2007-01-15
2
7
 
3
8
  * Updated Dhaka dependency to lock to version 0.0.6 due to breakage of backwards compatibility in 1.0.0.
@@ -7,7 +7,7 @@ end
7
7
 
8
8
  module KeywordSearch
9
9
 
10
- VERSION = '1.0.4'
10
+ VERSION = '1.0.5'
11
11
 
12
12
  class << self
13
13
  def search(input_string, definition=nil, &block)
@@ -4,46 +4,46 @@ class KeywordSearch::Parser < Dhaka::CompiledParser
4
4
 
5
5
  start_with 0
6
6
 
7
- at_state(3) {
8
- for_symbol('k') { reduce_with 'default_keyword_term' }
9
- for_symbol('_End_') { reduce_with 'default_keyword_term' }
10
- for_symbol('s') { reduce_with 'default_keyword_term' }
11
- }
12
-
13
- at_state(0) {
14
- for_symbol('k') { shift_to 4 }
15
- for_symbol('Pairs') { shift_to 1 }
16
- for_symbol('s') { shift_to 3 }
7
+ at_state(5) {
8
+ for_symbol('k') { shift_to 2 }
9
+ for_symbol('_End_') { reduce_with 'start' }
17
10
  for_symbol('Pair') { shift_to 6 }
11
+ for_symbol('s') { shift_to 1 }
18
12
  }
19
13
 
20
- at_state(4) {
21
- for_symbol('s') { shift_to 5 }
14
+ at_state(2) {
15
+ for_symbol('s') { shift_to 3 }
22
16
  }
23
17
 
24
- at_state(5) {
18
+ at_state(3) {
25
19
  for_symbol('k') { reduce_with 'keyword_and_term' }
26
20
  for_symbol('_End_') { reduce_with 'keyword_and_term' }
27
21
  for_symbol('s') { reduce_with 'keyword_and_term' }
28
22
  }
29
23
 
30
- at_state(1) {
31
- for_symbol('k') { shift_to 4 }
32
- for_symbol('_End_') { reduce_with 'start' }
33
- for_symbol('s') { shift_to 3 }
34
- for_symbol('Pair') { shift_to 2 }
24
+ at_state(6) {
25
+ for_symbol('k') { reduce_with 'multiple_pairs' }
26
+ for_symbol('_End_') { reduce_with 'multiple_pairs' }
27
+ for_symbol('s') { reduce_with 'multiple_pairs' }
35
28
  }
36
29
 
37
- at_state(6) {
30
+ at_state(4) {
38
31
  for_symbol('k') { reduce_with 'one_pair' }
39
32
  for_symbol('_End_') { reduce_with 'one_pair' }
40
33
  for_symbol('s') { reduce_with 'one_pair' }
41
34
  }
42
35
 
43
- at_state(2) {
44
- for_symbol('k') { reduce_with 'multiple_pairs' }
45
- for_symbol('_End_') { reduce_with 'multiple_pairs' }
46
- for_symbol('s') { reduce_with 'multiple_pairs' }
36
+ at_state(1) {
37
+ for_symbol('k') { reduce_with 'default_keyword_term' }
38
+ for_symbol('_End_') { reduce_with 'default_keyword_term' }
39
+ for_symbol('s') { reduce_with 'default_keyword_term' }
40
+ }
41
+
42
+ at_state(0) {
43
+ for_symbol('k') { shift_to 2 }
44
+ for_symbol('Pairs') { shift_to 5 }
45
+ for_symbol('Pair') { shift_to 4 }
46
+ for_symbol('s') { shift_to 1 }
47
47
  }
48
48
 
49
49
  end
@@ -6,12 +6,12 @@ module KeywordSearch
6
6
  # TODO: Add further character support; this is just for initial release
7
7
  letters = ('a'..'z').to_a
8
8
  numbers = ('0'..'9').to_a
9
- extras = %w|_ - ' / \ [ ] { } 1 @ # $ % ^ & * ( )|
9
+ extras = %w|_ - ' / \ [ ] { } 1 @ # $ % ^ & * ( ) . , ? < > |
10
10
  printables = letters + numbers + extras
11
11
  whitespace = [' ']
12
- quotes = ['"']
12
+ quotes = %w|' "|
13
13
  keyword_separator = [':']
14
- all_characters = keyword_separator + printables + whitespace
14
+ all_characters = keyword_separator + printables + whitespace + quotes
15
15
 
16
16
  for_state :idle_state do
17
17
 
@@ -20,10 +20,17 @@ module KeywordSearch
20
20
  switch_to :unquoted_literal_state
21
21
  end
22
22
 
23
- for_characters quotes do
24
- self.accumulator = ''
25
- advance
26
- switch_to :quoted_literal_state
23
+ for_characters(quotes) do
24
+ advance if self.accumulator && !self.accumulator.empty?
25
+ self.accumulator = ''
26
+ case curr_char
27
+ when %<">
28
+ advance
29
+ switch_to :double_quoted_literal_state
30
+ when %<'>
31
+ advance
32
+ switch_to :single_quoted_literal_state
33
+ end
27
34
  end
28
35
 
29
36
  for_characters whitespace do
@@ -42,24 +49,40 @@ module KeywordSearch
42
49
 
43
50
  for_characters(keyword_separator) do
44
51
  tokens << Dhaka::Token.new(Grammar.symbol_for_name('k'), self.accumulator)
52
+ self.accumulator = ''
45
53
  advance
46
54
  switch_to :idle_state
47
55
  end
48
56
 
49
57
  for_characters(whitespace) do
50
58
  tokens << Dhaka::Token.new(Grammar.symbol_for_name('s'), self.accumulator)
59
+ self.accumulator = ''
51
60
  switch_to :idle_state
52
61
  end
53
62
 
54
63
  end
55
64
 
56
- for_state :quoted_literal_state do
57
- for_characters(all_characters - quotes) do
65
+ for_state :double_quoted_literal_state do
66
+ for_characters(all_characters - %w<">) do
58
67
  self.accumulator += curr_char
59
68
  advance
60
69
  end
61
- for_characters quotes do
70
+ for_characters %w<"> do
62
71
  tokens << Dhaka::Token.new(Grammar.symbol_for_name('s'), self.accumulator)
72
+ self.accumulator = ''
73
+ advance
74
+ switch_to :idle_state
75
+ end
76
+ end
77
+
78
+ for_state :single_quoted_literal_state do
79
+ for_characters(all_characters - %w<'>) do
80
+ self.accumulator += curr_char
81
+ advance
82
+ end
83
+ for_characters %w<'> do
84
+ tokens << Dhaka::Token.new(Grammar.symbol_for_name('s'), self.accumulator)
85
+ self.accumulator = ''
63
86
  advance
64
87
  switch_to :idle_state
65
88
  end
@@ -5,8 +5,10 @@ class TestKeywordSearch < Test::Unit::TestCase
5
5
 
6
6
  NAME_AND_AGE = %<bruce williams age:26>
7
7
  NAME_QUOTED_AND_AGE = %<"bruce williams" age:26>
8
- NAME_AND_QUOTED_AGE = %<bruce williams age:"26">
8
+ NAME_AND_QUOTED_AGE = %<bruce williams age:"26">
9
9
  DEFAULT_AGE_WITH_QUOTED_AGE = %<26 name:"bruce williams">
10
+ DEFAULT_AGE_WITH_SINGLE_QUOTED_AGE = %<26 name:'bruce williams'>
11
+ NAME_WITH_NESTED_SINGLE_QUOTES = %<"d'arcy d'uberville" age:28>
10
12
 
11
13
  def test_default_keyword
12
14
  result = nil
@@ -61,6 +63,72 @@ class TestKeywordSearch < Test::Unit::TestCase
61
63
  assert_equal 'bruce williams', result
62
64
  end
63
65
 
66
+ def test_single_quoted_keyword_term_with_whitespace
67
+ result = nil
68
+ KeywordSearch.search(DEFAULT_AGE_WITH_SINGLE_QUOTED_AGE) do |with|
69
+ with.default_keyword :age
70
+ with.keyword :name do |values|
71
+ result = values.first
72
+ end
73
+ end
74
+ assert_equal 'bruce williams', result
75
+ end
76
+
77
+ def test_nested_single_quote_is_accumulated
78
+ result = nil
79
+ KeywordSearch.search(NAME_WITH_NESTED_SINGLE_QUOTES) do |with|
80
+ with.default_keyword :name
81
+ with.keyword :name do |values|
82
+ result = values.first
83
+ end
84
+ end
85
+ assert_equal %<d'arcy d'uberville>, result
86
+ end
87
+
88
+ def test_nested_double_quote_is_accumulated
89
+ result = nil
90
+ KeywordSearch.search(%<'he was called "jake"'>) do |with|
91
+ with.default_keyword :text
92
+ with.keyword :text do |values|
93
+ result = values.first
94
+ end
95
+ end
96
+ assert_equal %<he was called "jake">, result
97
+ end
98
+
99
+ def test_bare_single_quote_in_unquoted_literal_is_accumulated
100
+ result = nil
101
+ KeywordSearch.search(%<bruce's age:27>) do |with|
102
+ with.default_keyword :text
103
+ with.keyword :text do |values|
104
+ result = values.first
105
+ end
106
+ end
107
+ assert_equal %<bruce's>, result
108
+ end
109
+
110
+ def test_single_quoted_literal_is_accumulated
111
+ result = nil
112
+ KeywordSearch.search(%<foo 'bruce williams' age:27>) do |with|
113
+ with.default_keyword :text
114
+ with.keyword :text do |values|
115
+ result = values.last
116
+ end
117
+ end
118
+ assert_equal %<bruce williams>, result
119
+ end
120
+
121
+ def test_period_in_literal_is_accumulated
122
+ result = nil
123
+ KeywordSearch.search(%<okay... age:27>) do |with|
124
+ with.default_keyword :text
125
+ with.keyword :text do |values|
126
+ result = values.first
127
+ end
128
+ end
129
+ assert_equal %<okay...>, result
130
+ end
131
+
64
132
  end
65
133
 
66
134
 
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
3
3
  specification_version: 1
4
4
  name: keyword_search
5
5
  version: !ruby/object:Gem::Version
6
- version: 1.0.4
7
- date: 2007-01-15 00:00:00 -07:00
6
+ version: 1.0.5
7
+ date: 2007-01-29 00:00:00 -07:00
8
8
  summary: Generic support for extracting GMail-style search keywords/values from strings
9
9
  require_paths:
10
10
  - lib