keyword_prospector 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -135,6 +135,28 @@ class KeywordProspector
135
135
  end
136
136
  end
137
137
 
138
+ def to_hash
139
+ retval = {}
140
+
141
+ each_pair do |x,y|
142
+ retval[x] = y
143
+ end
144
+
145
+ retval
146
+ end
147
+
148
+ def each_pair(&block)
149
+ @start.keys.each do |key|
150
+ next if @start[key] == @start
151
+
152
+ @start[key].walk([key.chr], &block)
153
+ end
154
+ end
155
+
156
+ def inspect
157
+ to_hash.inspect
158
+ end
159
+
138
160
  private
139
161
  WORD_CHARS=[?a..?z, ?A..?Z, ?0..?9, ?_]
140
162
 
@@ -82,4 +82,16 @@ class State
82
82
 
83
83
  next_state
84
84
  end
85
+
86
+ def walk(array, &block)
87
+ block.call(array.join(''), output) if output
88
+
89
+ self.keys.each do |key|
90
+ array << key.chr
91
+
92
+ self[key].walk(array, &block)
93
+
94
+ array.pop
95
+ end
96
+ end
85
97
  end
@@ -229,4 +229,50 @@ describe KeywordProspector do
229
229
  end
230
230
  end
231
231
  end
232
+
233
+ describe :to_hash do
234
+ it "should return a hash showing the associations represented" do
235
+ kp = KeywordProspector.new()
236
+
237
+ kp.add('foo', :foo)
238
+ kp.add('bar', :bar)
239
+ kp.add('baz', :baz)
240
+
241
+ kp.construct_fail
242
+
243
+ kp.to_hash.should == {'foo' => :foo, 'bar' => :bar, 'baz' => :baz}
244
+ end
245
+ end
246
+
247
+ describe :each_pair do
248
+ it "should call the block, passing in each pair" do
249
+ kp = KeywordProspector.new()
250
+
251
+ kp.add('foo', :foo)
252
+ kp.add('bar', :bar)
253
+ kp.add('baz', :baz)
254
+
255
+ kp.construct_fail
256
+
257
+ target = mock(Object)
258
+ target.should_receive(:execute).with('foo', :foo)
259
+ target.should_receive(:execute).with('bar', :bar)
260
+ target.should_receive(:execute).with('baz', :baz)
261
+
262
+ kp.each_pair do |x,y|
263
+ target.execute(x, y)
264
+ end
265
+ end
266
+ end
267
+
268
+ describe :inspect do
269
+ it "should return the results of to_hash.inspect" do
270
+ kp = KeywordProspector.new()
271
+ hash = mock(Object)
272
+ hash.should_receive(:inspect).and_return(:hash_inspect)
273
+ kp.should_receive(:to_hash).and_return(hash)
274
+
275
+ kp.inspect.should == :hash_inspect
276
+ end
277
+ end
232
278
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: keyword_prospector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - FIXME full name
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-11 00:00:00 -07:00
12
+ date: 2008-10-07 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency