redis_autocomplete 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,17 +1,23 @@
1
1
  require 'redis'
2
2
 
3
3
  class RedisAutocomplete
4
+ DEFAULT_DISALLOWED_CHARS = /[^a-zA-Z0-9_-]/
5
+ DEFAULT_TERMINAL = '+'
6
+ DEFAULT_CASE_SENSITIVITY = true
7
+
4
8
  attr_reader :redis, :terminal
5
9
 
6
- def initialize(set, disallowed_chars = /[^a-zA-Z0-9_-]/, terminal = '+')
10
+ def initialize(set, opts = {})
7
11
  @set = set
8
12
  @redis = Redis.new
9
- @disallowed_chars = disallowed_chars
10
- @terminal = terminal
13
+ @disallowed_chars = opts[:disallowed_chars] || DEFAULT_DISALLOWED_CHARS
14
+ @terminal = opts[:terminal] || DEFAULT_TERMINAL
15
+ @case_sensitive = opts[:case_sensitive].nil? ? DEFAULT_CASE_SENSITIVITY : opts[:case_sensitive]
11
16
  end
12
17
 
13
18
  def add_word(word)
14
19
  w = word.gsub(@disallowed_chars, '')
20
+ w.downcase! if !@case_sensitive
15
21
  (1..(w.length)).each { |i| @redis.zadd(@set, 0, w.slice(0, i)) }
16
22
  @redis.zadd(@set, 0, "#{w}#{@terminal}")
17
23
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{redis_autocomplete}
8
- s.version = "0.1.1"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joe Johnston (simple10)", "Ben Woosley (Empact)", "Salvatore Sanfilippo (antirez)"]
@@ -3,67 +3,118 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  #require 'redis_autocomplete'
4
4
 
5
5
  describe RedisAutocomplete do
6
- before do
6
+ before :all do
7
7
  @names = %w[
8
- ynes
9
- ynez
10
- yoko
11
- yolanda
12
- yolande
8
+ Ynes
9
+ Ynez
10
+ Yoko
11
+ Yolanda
12
+ Yolande
13
13
  yolane
14
14
  yolanthe
15
- aaren
16
- aarika
17
- abagael
18
- abagail
19
- catherine
20
- cathi
15
+ Aaren
16
+ Aarika
17
+ Abagael
18
+ Abagail
19
+ Catherine
20
+ Cathi
21
21
  cathie
22
22
  cathleen
23
23
  cathlene
24
- cathrin
25
- cathrine
26
- cathryn
27
- cathy
28
- cathyleen
29
- cati
30
- catie
31
- catina
32
- catlaina
33
- catlee
34
- catlin
24
+ Cathrin
25
+ Cathrine
26
+ Cathryn
27
+ Cathy
28
+ Cathyleen
29
+ Cati
30
+ Catie
31
+ Catina
32
+ Catlaina
33
+ Catlee
34
+ Catlin
35
35
  ]
36
36
  @set = :test_female_names
37
- @r = RedisAutocomplete.new(@set)
38
- #todo: drop female_names set
39
- @r.add_words(@names)
40
37
  end
41
38
 
42
- describe "#suggest" do
43
- it "should include words matching prefix" do
44
- @r.suggest('c').should == %w[
45
- catherine
46
- cathi
47
- cathie
48
- cathleen
49
- cathlene
50
- cathrin
51
- cathrine
52
- cathryn
53
- cathy
54
- cathyleen
55
- ]
39
+ context "with default case sensitivity" do
40
+ before do
41
+ @r = RedisAutocomplete.new(@set)
42
+ @r.redis.zremrangebyscore(@set, 0, 0)
43
+ @r.add_words(@names)
56
44
  end
57
- it "should not include words not matching prefix" do
58
- @r.suggest('cati').should_not include('cathy')
45
+
46
+ describe "#suggest" do
47
+ it "should include words matching prefix" do
48
+ @r.suggest('C').should == %w[
49
+ Catherine
50
+ Cathi
51
+ Cathrin
52
+ Cathrine
53
+ Cathryn
54
+ Cathy
55
+ Cathyleen
56
+ Cati
57
+ Catie
58
+ Catina
59
+ ]
60
+ end
61
+
62
+ it "should not include words not matching prefix" do
63
+ @r.suggest('Cati').should_not include('Cathy')
64
+ end
65
+
66
+ it "should not include uppercase when searching on lowercase" do
67
+ @r.suggest('Y').should_not include('yolane', 'yolanthe')
68
+ @r.suggest('Y').should == %w[Ynes Ynez Yoko Yolanda Yolande]
69
+ end
70
+
71
+ context "when a max count is supplied" do
72
+ it "should not include more than 10 matches" do
73
+ @r.suggest('C').length.should == 10
74
+ end
75
+
76
+ it "should not include more matches than the supplied count" do
77
+ @r.suggest('C', 4).length.should == 4
78
+ end
79
+ end
80
+ end
81
+ end
82
+
83
+ context "with :case_sensitive => false" do
84
+ before do
85
+ @r = RedisAutocomplete.new(@set, :case_sensitive => false)
86
+ @r.redis.zremrangebyscore(@set, 0, 0)
87
+ @r.add_words(@names)
59
88
  end
60
89
 
61
- context "when a max count is supplied" do
62
- it "should not include more than 10 matches" do
63
- @r.suggest('c').length.should == 10
90
+ describe "#suggest" do
91
+ it "should include words matching prefix" do
92
+ @r.suggest('c').should == %w[
93
+ catherine
94
+ cathi
95
+ cathie
96
+ cathleen
97
+ cathlene
98
+ cathrin
99
+ cathrine
100
+ cathryn
101
+ cathy
102
+ cathyleen
103
+ ]
64
104
  end
65
- it "should not include more matches than the supplied count" do
66
- @r.suggest('c', 4).length.should == 4
105
+
106
+ it "should not include words not matching prefix" do
107
+ @r.suggest('cati').should_not include('cathy')
108
+ end
109
+
110
+ context "when a max count is supplied" do
111
+ it "should not include more than 10 matches" do
112
+ @r.suggest('c').length.should == 10
113
+ end
114
+
115
+ it "should not include more matches than the supplied count" do
116
+ @r.suggest('c', 4).length.should == 4
117
+ end
67
118
  end
68
119
  end
69
120
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: redis_autocomplete
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.1
5
+ version: 0.1.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Joe Johnston (simple10)
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  requirements:
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- hash: -1355877188138504025
109
+ hash: 1956628192009044186
110
110
  segments:
111
111
  - 0
112
112
  version: "0"