picky 4.11.2 → 4.11.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -48,6 +48,7 @@ module Picky
48
48
  #
49
49
  @source = Source.from options[:source], true, @index.name
50
50
  @tokenizer = Tokenizer.from options[:indexing], @index.name, name
51
+ @ranger = options[:ranging] || Range
51
52
 
52
53
  @key_format = options.delete :key_format
53
54
  @backend = options.delete :backend
@@ -175,4 +176,4 @@ module Picky
175
176
 
176
177
  end
177
178
 
178
- end
179
+ end
@@ -18,12 +18,9 @@ module Picky
18
18
  def weight token
19
19
  bundle = bundle_for token
20
20
  if range = token.range
21
- # The math is not perfectly correct, but you
22
- # get my idea. Also, we could return early.
21
+ # TODO We might be able to return early?
23
22
  #
24
- # TODO Possible to speed up more?
25
- #
26
- range.inject(nil) do |sum, text|
23
+ @ranger.new(*range).inject(nil) do |sum, text|
27
24
  weight = bundle.weight(text)
28
25
  weight && (weight + (sum || 0)) || sum
29
26
  end
@@ -40,7 +37,7 @@ module Picky
40
37
  # Adding all to an array, then flattening
41
38
  # is faster than using ary + ary.
42
39
  #
43
- range.inject([]) do |result, text|
40
+ @ranger.new(*range).inject([]) do |result, text|
44
41
  # It is 30% faster using the empty check
45
42
  # than just << [].
46
43
  #
@@ -69,4 +66,4 @@ module Picky
69
66
 
70
67
  end
71
68
 
72
- end
69
+ end
@@ -201,7 +201,7 @@ module Picky
201
201
  end
202
202
  def rangify
203
203
  if @text.include? @@range_character
204
- @range = Range.new *@text.split(@@range_character, 2)
204
+ @range = @text.split(@@range_character, 2)
205
205
  end
206
206
  end
207
207
  def range
@@ -331,4 +331,4 @@ module Picky
331
331
 
332
332
  end
333
333
 
334
- end
334
+ end
@@ -85,5 +85,53 @@ describe 'range queries' do
85
85
  # #
86
86
  # try.search('198-200*').ids.should == [8,3,1,4,5,7,6,2]
87
87
  # end
88
+
89
+ describe 'custom ranges' do
90
+ class Wrap12Hours
91
+ include Enumerable
92
+
93
+ def initialize(min, max)
94
+ @hours = 12
95
+ @min = min.to_i
96
+ @top = max.to_i
97
+ @top += @hours if @top < @min
98
+ end
99
+
100
+ def each
101
+ @min.upto(@top).each do |i|
102
+ yield (i % @hours).to_s
103
+ end
104
+ end
105
+ end
106
+
107
+ let(:index) do
108
+ index = Picky::Index.new :range_queries do
109
+ category :hour,
110
+ ranging: Wrap12Hours,
111
+ partial: Picky::Partial::None.new
112
+ end
113
+
114
+ rangy = Struct.new :id, :hour
115
+
116
+ index.add rangy.new(1, 0)
117
+ index.add rangy.new(2, 1)
118
+ index.add rangy.new(3, 4)
119
+ index.add rangy.new(4, 5)
120
+ index.add rangy.new(5, 11)
121
+ index.add rangy.new(6, 10)
122
+ index.add rangy.new(7, 2)
123
+ index.add rangy.new(8, 3)
124
+
125
+ index
126
+ end
127
+
128
+ it 'allows injection of custom range classes' do
129
+ try.search('hour:10-2').ids.should == [6, 5, 1, 2, 7]
130
+ end
131
+
132
+ it 'allows injection of custom range classes' do
133
+ try.search('hour:0-11').ids.should == [1, 2, 7, 8, 3, 4, 6, 5]
134
+ end
135
+ end
88
136
 
89
- end
137
+ end
@@ -92,7 +92,7 @@ describe Picky::Category do
92
92
  end
93
93
  context 'with range' do
94
94
  before :each do
95
- token.stub! :range => (1..3)
95
+ token.stub! :range => [1, 3]
96
96
  end
97
97
  context 'partial bundle' do
98
98
  before(:each) do
@@ -154,7 +154,7 @@ describe Picky::Category do
154
154
  end
155
155
  end
156
156
  context 'with range' do
157
- before(:each) { token.stub! :range => (1..3) }
157
+ before(:each) { token.stub! :range => [1, 3] }
158
158
  context 'partial bundle' do
159
159
  before(:each) do
160
160
  @category.stub! :bundle_for => @partial
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.11.2
4
+ version: 4.11.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-06 00:00:00.000000000 Z
12
+ date: 2012-11-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 4.11.2
37
+ version: 4.11.3
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 4.11.2
45
+ version: 4.11.3
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: text
48
48
  requirement: !ruby/object:Gem::Requirement