beawesomeinstead-filter 0.1.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.
data/filter.gemspec ADDED
@@ -0,0 +1,13 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "filter"
3
+ s.version = "0.1.0"
4
+ s.date = "2008-07-11"
5
+ s.summary = "Simple conditions parser."
6
+ s.email = "beawesomeinstead@yahoo.com"
7
+ s.homepage = "http://github.com/beawesomeinstead/filter/wikis"
8
+ s.description = s.summary
9
+ s.authors = ["beawesomeinstead"]
10
+ s.has_rdoc = false
11
+ s.require_paths = ["libraries"]
12
+ s.files = ["libraries/filter", "libraries/filter/definition.rb", "libraries/filter/keyword.rb", "libraries/filter/search.rb", "libraries/filter.rb", "specifications/integration", "specifications/integration/filter_spec.rb", "specifications/spec_helper.rb", "rakefile", "readme", "filter.gemspec"]
13
+ end
@@ -0,0 +1,4 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ %w(filter/definition filter/keyword filter/search).map { |x| require x }
@@ -0,0 +1,28 @@
1
+ module Filter #:nodoc:
2
+ class Definition #:nodoc:
3
+ def initialize
4
+ @default_keyword = nil
5
+ yield self if block_given?
6
+ end
7
+
8
+ def keywords
9
+ @keywords ||= []
10
+ end
11
+
12
+ def keyword(name, description = nil, &block)
13
+ keywords << Keyword.new(name, description, &block)
14
+ end
15
+
16
+ def default_keyword(name)
17
+ @default_keyword = name
18
+ end
19
+
20
+ def handle(key, values)
21
+ key = @default_keyword if key == :default
22
+ return false unless key
23
+ if k = keywords.detect { |kw| kw.name == key.to_sym}
24
+ k.handle(values)
25
+ end
26
+ end
27
+ end # class Definition
28
+ end # module Filter
@@ -0,0 +1,14 @@
1
+ module Filter #:nodoc:
2
+ class Keyword #:nodoc:
3
+ attr_reader :name, :description, :handler
4
+
5
+ def initialize(name, description = nil, &handler)
6
+ @name, @description = name, description
7
+ @handler = handler
8
+ end
9
+
10
+ def handle(value)
11
+ handler.call(value)
12
+ end
13
+ end # class Keyword
14
+ end # module Filter
@@ -0,0 +1,282 @@
1
+ #require File.dirname(__FILE__) << '/keyword_search/definition.rb'
2
+ module Filter
3
+
4
+ class ParseError < ::SyntaxError; end
5
+
6
+ class << self
7
+
8
+ # line 54 "lib/keyword_search.rl"
9
+
10
+
11
+ def search(input_string, definition=nil, &block)
12
+ definition ||= Definition.new(&block)
13
+ results = parse(input_string)
14
+ results.each do |key, terms|
15
+ definition.handle(key, terms)
16
+ end
17
+ results
18
+ end
19
+
20
+ #######
21
+ private
22
+ #######
23
+
24
+ def parse(input) #:nodoc:
25
+ data = input + ' '
26
+
27
+ # line 30 "lib/keyword_search.rb"
28
+ class << self
29
+ attr_accessor :_parser_actions
30
+ private :_parser_actions, :_parser_actions=
31
+ end
32
+ self._parser_actions = [
33
+ 0, 1, 3, 1, 5, 1, 6, 2,
34
+ 0, 2, 2, 1, 0, 3, 0, 2,
35
+ 4, 3, 1, 0, 4
36
+ ]
37
+
38
+ class << self
39
+ attr_accessor :_parser_key_offsets
40
+ private :_parser_key_offsets, :_parser_key_offsets=
41
+ end
42
+ self._parser_key_offsets = [
43
+ 0, 0, 6, 10, 15, 19, 20, 21,
44
+ 22, 23
45
+ ]
46
+
47
+ class << self
48
+ attr_accessor :_parser_trans_keys
49
+ private :_parser_trans_keys, :_parser_trans_keys=
50
+ end
51
+ self._parser_trans_keys = [
52
+ 0, 32, 34, 39, 40, 58, 32, 34,
53
+ 41, 58, 32, 34, 39, 40, 58, 32,
54
+ 34, 41, 58, 34, 32, 39, 41, 32,
55
+ 34, 41, 58, 0
56
+ ]
57
+
58
+ class << self
59
+ attr_accessor :_parser_single_lengths
60
+ private :_parser_single_lengths, :_parser_single_lengths=
61
+ end
62
+ self._parser_single_lengths = [
63
+ 0, 6, 4, 5, 4, 1, 1, 1,
64
+ 1, 4
65
+ ]
66
+
67
+ class << self
68
+ attr_accessor :_parser_range_lengths
69
+ private :_parser_range_lengths, :_parser_range_lengths=
70
+ end
71
+ self._parser_range_lengths = [
72
+ 0, 0, 0, 0, 0, 0, 0, 0,
73
+ 0, 0
74
+ ]
75
+
76
+ class << self
77
+ attr_accessor :_parser_index_offsets
78
+ private :_parser_index_offsets, :_parser_index_offsets=
79
+ end
80
+ self._parser_index_offsets = [
81
+ 0, 0, 7, 12, 18, 23, 25, 27,
82
+ 29, 31
83
+ ]
84
+
85
+ class << self
86
+ attr_accessor :_parser_trans_targs_wi
87
+ private :_parser_trans_targs_wi, :_parser_trans_targs_wi=
88
+ end
89
+ self._parser_trans_targs_wi = [
90
+ 9, 0, 5, 7, 8, 0, 2, 1,
91
+ 0, 0, 3, 2, 0, 5, 7, 8,
92
+ 0, 4, 1, 0, 0, 0, 4, 6,
93
+ 5, 1, 0, 6, 7, 6, 8, 1,
94
+ 0, 0, 3, 2, 0
95
+ ]
96
+
97
+ class << self
98
+ attr_accessor :_parser_trans_actions_wi
99
+ private :_parser_trans_actions_wi, :_parser_trans_actions_wi=
100
+ end
101
+ self._parser_trans_actions_wi = [
102
+ 7, 5, 13, 13, 13, 5, 7, 1,
103
+ 0, 0, 0, 0, 0, 17, 17, 17,
104
+ 0, 10, 1, 0, 0, 0, 0, 3,
105
+ 0, 1, 0, 3, 0, 3, 0, 1,
106
+ 0, 0, 0, 0, 0
107
+ ]
108
+
109
+ class << self
110
+ attr_accessor :parser_start
111
+ end
112
+ self.parser_start = 1;
113
+ class << self
114
+ attr_accessor :parser_first_final
115
+ end
116
+ self.parser_first_final = 9;
117
+ class << self
118
+ attr_accessor :parser_error
119
+ end
120
+ self.parser_error = 0;
121
+
122
+ class << self
123
+ attr_accessor :parser_en_main
124
+ end
125
+ self.parser_en_main = 1;
126
+
127
+ # line 72 "lib/keyword_search.rl"
128
+ p = 0
129
+ pe = data.length
130
+ key = nil
131
+ tokstart = nil
132
+ results = {}
133
+ quotes = 0
134
+
135
+ # line 138 "lib/keyword_search.rb"
136
+ begin
137
+ cs = parser_start
138
+ end
139
+ # line 79 "lib/keyword_search.rl"
140
+
141
+ # line 144 "lib/keyword_search.rb"
142
+ begin
143
+ _klen, _trans, _keys, _acts, _nacts = nil
144
+ if p != pe
145
+ if cs != 0
146
+ while true
147
+ _break_resume = false
148
+ begin
149
+ _break_again = false
150
+ _keys = _parser_key_offsets[cs]
151
+ _trans = _parser_index_offsets[cs]
152
+ _klen = _parser_single_lengths[cs]
153
+ _break_match = false
154
+
155
+ begin
156
+ if _klen > 0
157
+ _lower = _keys
158
+ _upper = _keys + _klen - 1
159
+
160
+ loop do
161
+ break if _upper < _lower
162
+ _mid = _lower + ( (_upper - _lower) >> 1 )
163
+
164
+ if data[p] < _parser_trans_keys[_mid]
165
+ _upper = _mid - 1
166
+ elsif data[p] > _parser_trans_keys[_mid]
167
+ _lower = _mid + 1
168
+ else
169
+ _trans += (_mid - _keys)
170
+ _break_match = true
171
+ break
172
+ end
173
+ end # loop
174
+ break if _break_match
175
+ _keys += _klen
176
+ _trans += _klen
177
+ end
178
+ _klen = _parser_range_lengths[cs]
179
+ if _klen > 0
180
+ _lower = _keys
181
+ _upper = _keys + (_klen << 1) - 2
182
+ loop do
183
+ break if _upper < _lower
184
+ _mid = _lower + (((_upper-_lower) >> 1) & ~1)
185
+ if data[p] < _parser_trans_keys[_mid]
186
+ _upper = _mid - 2
187
+ elsif data[p] > _parser_trans_keys[_mid+1]
188
+ _lower = _mid + 2
189
+ else
190
+ _trans += ((_mid - _keys) >> 1)
191
+ _break_match = true
192
+ break
193
+ end
194
+ end # loop
195
+ break if _break_match
196
+ _trans += _klen
197
+ end
198
+ end while false
199
+ cs = _parser_trans_targs_wi[_trans]
200
+ break if _parser_trans_actions_wi[_trans] == 0
201
+ _acts = _parser_trans_actions_wi[_trans]
202
+ _nacts = _parser_actions[_acts]
203
+ _acts += 1
204
+ while _nacts > 0
205
+ _nacts -= 1
206
+ _acts += 1
207
+ case _parser_actions[_acts - 1]
208
+ when 0:
209
+ # line 13 "lib/keyword_search.rl"
210
+ begin
211
+
212
+ tokstart = p;
213
+ end
214
+ # line 13 "lib/keyword_search.rl"
215
+ when 1:
216
+ # line 17 "lib/keyword_search.rl"
217
+ begin
218
+
219
+ key = data[tokstart...p-1]
220
+ results[key] ||= []
221
+ end
222
+ # line 17 "lib/keyword_search.rl"
223
+ when 2:
224
+ # line 22 "lib/keyword_search.rl"
225
+ begin
226
+
227
+ key = nil
228
+ end
229
+ # line 22 "lib/keyword_search.rl"
230
+ when 3:
231
+ # line 26 "lib/keyword_search.rl"
232
+ begin
233
+
234
+ value = data[tokstart..p-1]
235
+ if ["("].include?(value[0,1])
236
+ value = parse(value[1..-2])[:default]
237
+ elsif ["'", '"'].include?(value[0,1])
238
+ value = value[1..-2]
239
+ end
240
+ (results[key || :default] ||= []) << value
241
+ end
242
+ # line 26 "lib/keyword_search.rl"
243
+ when 4:
244
+ # line 36 "lib/keyword_search.rl"
245
+ begin
246
+ quotes += 1 end
247
+ # line 36 "lib/keyword_search.rl"
248
+ when 5:
249
+ # line 38 "lib/keyword_search.rl"
250
+ begin
251
+ quotes -= 1 end
252
+ # line 38 "lib/keyword_search.rl"
253
+ when 6:
254
+ # line 52 "lib/keyword_search.rl"
255
+ begin
256
+ raise ParseError, "At offset #{p}, near: '#{data[p,10]}'" end
257
+ # line 52 "lib/keyword_search.rl"
258
+ # line 261 "lib/keyword_search.rb"
259
+ end # action switch
260
+ end
261
+ end while false
262
+ break if _break_resume
263
+ break if cs == 0
264
+ p += 1
265
+ break if p == pe
266
+ end
267
+ end
268
+ end
269
+ end
270
+ # line 80 "lib/keyword_search.rl"
271
+
272
+ # line 275 "lib/keyword_search.rb"
273
+ # line 81 "lib/keyword_search.rl"
274
+ unless quotes.zero?
275
+ raise ParseError, "Unclosed quotes"
276
+ end
277
+ results
278
+ end
279
+
280
+ end
281
+
282
+ end
data/rakefile ADDED
@@ -0,0 +1,6 @@
1
+ %w(rubygems rake/gempackagetask spec/rake/spectask).each { |x| require x }
2
+
3
+ Spec::Rake::SpecTask.new("specs") do |t|
4
+ t.spec_opts << '--format' << 'specdoc' << '--colour'
5
+ t.spec_files = Dir.glob("specifications/**/*_spec.rb")
6
+ end
data/readme ADDED
File without changes
@@ -0,0 +1,204 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), "..", "spec_helper"))
2
+
3
+ describe Filter do
4
+ NAME_AND_AGE = %<bruce williams age:26>
5
+ NAME_QUOTED_AND_AGE = %<"bruce williams" age:26>
6
+ NAME_AND_QUOTED_AGE = %<bruce williams age:"26">
7
+ DEFAULT_AGE_WITH_QUOTED_AGE = %<26 name:"bruce williams">
8
+ DEFAULT_AGE_WITH_SINGLE_QUOTED_AGE = %<26 name:'bruce williams'>
9
+ NAME_WITH_NESTED_SINGLE_QUOTES = %<"d'arcy d'uberville" age:28>
10
+ NAME_AND_GROUPED_AGE = %<coda hale age:(27)>
11
+ NAME_AND_GROUPED_QUOTED_AGE = %<coda hale age:("27")>
12
+ NAME_AND_GROUPED_QUOTED_AGES = %<coda hale age:("27" 34 '48')>
13
+ GROUPED_NAMES_AND_AGE = %<(coda bruce 'hale' "williams") age:20>
14
+
15
+ before(:each) do
16
+ @result = nil
17
+ end
18
+
19
+ it "should parse grouped default keywords properly" do
20
+ Filter.search(NAME_AND_AGE) do |with|
21
+ with.default_keyword :name
22
+ with.keyword :name do |values|
23
+ @result = values.join(' ')
24
+ end
25
+ end
26
+ @result.should == "bruce williams"
27
+ end
28
+
29
+ it "should parse grouped default keywords properly" do
30
+ Filter.search(GROUPED_NAMES_AND_AGE) do |with|
31
+ with.default_keyword :name
32
+ with.keyword :name do |values|
33
+ @result = values.first
34
+ end
35
+ end
36
+ @result.should == ['coda', 'bruce', 'hale', 'williams']
37
+ end
38
+
39
+ it "should parse unquoted keyword term properly" do
40
+ Filter.search(NAME_AND_AGE) do |with|
41
+ with.keyword :age do |values|
42
+ @result = Integer(values.first)
43
+ end
44
+ end
45
+ @result.should == 26
46
+ end
47
+
48
+ it "should parse unquoted grouped keyword term properly" do
49
+ Filter.search(NAME_AND_GROUPED_AGE) do |with|
50
+ with.keyword :age do |values|
51
+ @result = Integer(values.first.first)
52
+ end
53
+ end
54
+ @result.should == 27
55
+ end
56
+
57
+ it "should parse quoted grouped keyword term properly" do
58
+ Filter.search(NAME_AND_GROUPED_QUOTED_AGE) do |with|
59
+ with.keyword :age do |values|
60
+ @result = Integer(values.first.first)
61
+ end
62
+ end
63
+ @result.should == 27
64
+ end
65
+
66
+ it "should parse mixed grouped keyword terms properly" do
67
+ Filter.search(NAME_AND_GROUPED_QUOTED_AGES) do |with|
68
+ with.keyword :age do |values|
69
+ @result = values.first.map { |v| v.to_i }
70
+ end
71
+ end
72
+ @result.should == [27, 34, 48]
73
+ end
74
+
75
+ it "should parse quoted default keyword term properly" do
76
+ Filter.search(NAME_QUOTED_AND_AGE) do |with|
77
+ with.default_keyword :name
78
+ with.keyword :name do |values|
79
+ @result = values.join(' ')
80
+ end
81
+ end
82
+ @result.should == "bruce williams"
83
+ end
84
+
85
+ it "should parse quoted keyword term properly" do
86
+ Filter.search(NAME_AND_QUOTED_AGE) do |with|
87
+ with.keyword :age do |values|
88
+ @result = Integer(values.first)
89
+ end
90
+ end
91
+ @result.should == 26
92
+ end
93
+
94
+ it "should parse quoted keyword term with whitespace properly" do
95
+ Filter.search(DEFAULT_AGE_WITH_QUOTED_AGE) do |with|
96
+ with.default_keyword :age
97
+ with.keyword :name do |values|
98
+ @result = values.first
99
+ end
100
+ end
101
+ @result.should == "bruce williams"
102
+ end
103
+
104
+ it "should parse single quoted keyword term with whitespace" do
105
+ Filter.search(DEFAULT_AGE_WITH_SINGLE_QUOTED_AGE) do |with|
106
+ with.default_keyword :age
107
+ with.keyword :name do |values|
108
+ @result = values.first
109
+ end
110
+ end
111
+ @result.should == "bruce williams"
112
+ end
113
+
114
+ it "should parse nested single quote is accumulated" do
115
+ Filter.search(NAME_WITH_NESTED_SINGLE_QUOTES) do |with|
116
+ with.default_keyword :name
117
+ with.keyword :name do |values|
118
+ @result = values.first
119
+ end
120
+ end
121
+ @result.should == %<d'arcy d'uberville>
122
+ end
123
+
124
+ it "should parse nested single quote is accumulated" do
125
+ Filter.search(%<'he was called "jake"'>) do |with|
126
+ with.default_keyword :text
127
+ with.keyword :text do |values|
128
+ @result = values.first
129
+ end
130
+ end
131
+ @result.should == %<he was called "jake">
132
+ end
133
+
134
+ it "should parse bare single quote in unquoted literal is accumulated" do
135
+ Filter.search(%<bruce's age:27>) do |with|
136
+ with.default_keyword :text
137
+ with.keyword :text do |values|
138
+ @result = values.first
139
+ end
140
+ end
141
+ @result.should == %<bruce's>
142
+ end
143
+
144
+ it "should parse single quoted literal is accumulated" do
145
+ Filter.search(%<foo 'bruce williams' age:27>) do |with|
146
+ with.default_keyword :text
147
+ with.keyword :text do |values|
148
+ @result = values.last
149
+ end
150
+ end
151
+ @result.should == %<bruce williams>
152
+ end
153
+
154
+ it "should parse period in literal is accumulated" do
155
+ Filter.search(%<okay... age:27>) do |with|
156
+ with.default_keyword :text
157
+ with.keyword :text do |values|
158
+ @result = values.first
159
+ end
160
+ end
161
+ @result.should == %<okay...>
162
+ end
163
+
164
+ it "should parse data with apostrophes in unquoted literal" do
165
+ Filter.search(%<d'correct>) do |with|
166
+ with.default_keyword :text
167
+ with.keyword :text do |values|
168
+ @result = values.first
169
+ end
170
+ end
171
+ @result.should == "d'correct"
172
+ end
173
+
174
+ it "should parse data with apostrophes in unquoted literal values" do
175
+ Filter.search(%<text:d'correct>) do |with|
176
+ with.default_keyword :text
177
+ with.keyword :text do |values|
178
+ @result = values.first
179
+ end
180
+ end
181
+ @result.should == "d'correct"
182
+ end
183
+
184
+ it "should parse case-sensitive keywords" do
185
+ Filter.search(%<Text:justtesting>) do |with|
186
+ with.keyword :text do |values|
187
+ @result = :small
188
+ end
189
+ with.keyword :Text do |values|
190
+ @result = :big
191
+ end
192
+ end
193
+ @result.should == :big
194
+ end
195
+
196
+ it "should parse case-sensitive values" do
197
+ Filter.search(%<text:Big>) do |with|
198
+ with.keyword :text do |values|
199
+ @result = values.first
200
+ end
201
+ end
202
+ @result.should == "Big"
203
+ end
204
+ end
@@ -0,0 +1,2 @@
1
+ %w(rubygems spec).each { |x| require x }
2
+ require File.join(File.dirname(__FILE__), "..", "libraries", "filter")
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beawesomeinstead-filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - beawesomeinstead
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-07-11 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Simple conditions parser.
17
+ email: beawesomeinstead@yahoo.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - libraries/filter
26
+ - libraries/filter/definition.rb
27
+ - libraries/filter/keyword.rb
28
+ - libraries/filter/search.rb
29
+ - libraries/filter.rb
30
+ - specifications/integration
31
+ - specifications/integration/filter_spec.rb
32
+ - specifications/spec_helper.rb
33
+ - rakefile
34
+ - readme
35
+ - filter.gemspec
36
+ has_rdoc: false
37
+ homepage: http://github.com/beawesomeinstead/filter/wikis
38
+ post_install_message:
39
+ rdoc_options: []
40
+
41
+ require_paths:
42
+ - libraries
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ version:
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ requirements: []
56
+
57
+ rubyforge_project:
58
+ rubygems_version: 1.2.0
59
+ signing_key:
60
+ specification_version: 2
61
+ summary: Simple conditions parser.
62
+ test_files: []
63
+