username_suggester 0.1.0 → 0.2.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/.gitignore +3 -0
- data/README.rdoc +4 -1
- data/lib/username_suggester/suggester.rb +2 -2
- data/spec/db/database.yml +6 -6
- data/spec/suggester_spec.rb +2 -1
- data/spec/suggestions_for_spec.rb +2 -0
- data/username_suggester.gemspec +2 -1
- metadata +4 -3
data/.gitignore
ADDED
data/README.rdoc
CHANGED
@@ -6,6 +6,9 @@ using RLIKE for those DB does not support it. (although it will be slower)
|
|
6
6
|
|
7
7
|
==Usage
|
8
8
|
|
9
|
+
In your environment.rb, add:
|
10
|
+
config.gem 'username_suggester'
|
11
|
+
|
9
12
|
Assume you have an User model with attributes :username, :firstname, :lastname
|
10
13
|
|
11
14
|
class User < ActiveRecord::Base
|
@@ -21,7 +24,7 @@ And now you can call the suggestion function like the following:
|
|
21
24
|
user = User.new(:firstname => "Jerry", :lastname => "Luk")
|
22
25
|
user.username_suggestions
|
23
26
|
|
24
|
-
You can also filter the suggestions, let's say you want the username
|
27
|
+
You can also filter the suggestions, let's say you want the username to contain at least 4 characters:
|
25
28
|
|
26
29
|
suggestions_for :username, :num_suggestions => 10,
|
27
30
|
:validate => Proc.new { |username| username.length >= 4 }
|
@@ -42,7 +42,7 @@ module UsernameSuggester
|
|
42
42
|
while results.size < max_num_suggestion and !candidates.blank?
|
43
43
|
candidate = candidates.shift
|
44
44
|
if @options[:validate] and !@options[:validate].call(candidate)
|
45
|
-
#
|
45
|
+
# Don't add the candidate to result
|
46
46
|
elsif unavailable_set.include? candidate
|
47
47
|
candidates << find_extended_candidate(candidate, unavailable_set)
|
48
48
|
else
|
@@ -56,7 +56,7 @@ module UsernameSuggester
|
|
56
56
|
# Generates a candidate with "candidate<number>" which is not included in unavailable_set
|
57
57
|
def find_extended_candidate(candidate, unavailable_set)
|
58
58
|
i = 1
|
59
|
-
i+=
|
59
|
+
i+=rand(10) while unavailable_set.include? "#{candidate}#{i}"
|
60
60
|
"#{candidate}#{i}"
|
61
61
|
end
|
62
62
|
end
|
data/spec/db/database.yml
CHANGED
@@ -2,9 +2,9 @@ sqlite3:
|
|
2
2
|
:adapter: sqlite3
|
3
3
|
:database: vendor/plugins/username_suggester/spec/db/username_suggester.sqlite3.db
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
mysql:
|
6
|
+
:adapter: mysql
|
7
|
+
:database: username_suggester_test
|
8
|
+
:username: root
|
9
|
+
:password:
|
10
|
+
:host: localhost
|
data/spec/suggester_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/spec_helper'
|
2
2
|
|
3
|
-
describe UsernameSuggester::Suggester do
|
3
|
+
describe UsernameSuggester::Suggester do
|
4
4
|
describe "name combinations" do
|
5
5
|
it "returns combinations of first name and last name" do
|
6
6
|
UsernameSuggester::Suggester.new("Jerry", "Luk").name_combinations.should == [
|
@@ -44,6 +44,7 @@ describe UsernameSuggester::Suggester do
|
|
44
44
|
end
|
45
45
|
|
46
46
|
it "returns extended suggestions for names that are in the unavailable suggestions" do
|
47
|
+
UsernameSuggester::Suggester.send(:define_method, :rand) { 1 }
|
47
48
|
@suggester.suggest(10, ["jerry"]).should include "jerry1"
|
48
49
|
@suggester.suggest(10, ["jerry", "jerry1"]).should include "jerry2"
|
49
50
|
@suggester.suggest(10, ["jerry", "jerry1031"]).should include "jerry1"
|
@@ -17,6 +17,8 @@ describe UsernameSuggester::UsernameSuggestions do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
it "should able to suggest usernames that are not taken" do
|
20
|
+
UsernameSuggester::Suggester.send(:define_method, :rand) { 1 }
|
21
|
+
|
20
22
|
User.create!(:username => "jerry")
|
21
23
|
1.upto(10) do |i|
|
22
24
|
User.create!(:username => "jerry#{i}")
|
data/username_suggester.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = %q{username_suggester}
|
3
|
-
s.version = "0.
|
3
|
+
s.version = "0.2.0"
|
4
4
|
|
5
5
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
6
6
|
s.authors = ["jerryluk"]
|
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
"README.rdoc"
|
13
13
|
]
|
14
14
|
s.files = [
|
15
|
+
".gitignore",
|
15
16
|
"MIT-LICENSE",
|
16
17
|
"README.rdoc",
|
17
18
|
"Rakefile",
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: username_suggester
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 2
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- jerryluk
|
@@ -44,6 +44,7 @@ extra_rdoc_files:
|
|
44
44
|
- MIT-LICENSE
|
45
45
|
- README.rdoc
|
46
46
|
files:
|
47
|
+
- .gitignore
|
47
48
|
- MIT-LICENSE
|
48
49
|
- README.rdoc
|
49
50
|
- Rakefile
|