username_suggester 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,30 @@
1
+ = UsernameSuggester
2
+
3
+ It generates username suggestions for users.
4
+ It works with MySQL and any DB that supports "RLIKE". If enough people request, I will add the support for not
5
+ using RLIKE for those DB does not support it. (although it will be slower)
6
+
7
+ ==Usage
8
+
9
+ Assume you have an User model with attributes :username, :firstname, :lastname
10
+
11
+ class User < ActiveRecord::Base
12
+ suggestions_for :username, :num_suggestions => 5,
13
+ :first_name_attribute => :firstname, :last_name_attribute => lastname
14
+
15
+ ...
16
+
17
+ end
18
+
19
+ And now you can call the suggestion function like the following:
20
+
21
+ user = User.new(:firstname => "Jerry", :lastname => "Luk")
22
+ user.username_suggestions
23
+
24
+ You can also filter the suggestions, let's say you want the username contains at least 4 characters:
25
+
26
+ suggestions_for :username, :num_suggestions => 10,
27
+ :validate => Proc.new { |username| username.length >= 4 }
28
+
29
+
30
+ Copyright (c) 2010 Presdo, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,30 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+ require 'spec/rake/spectask'
5
+
6
+ desc 'Default: run specs.'
7
+ task :default => :spec
8
+
9
+ desc 'Run the specs'
10
+ Spec::Rake::SpecTask.new(:spec) do |t|
11
+ t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
12
+ t.spec_files = FileList['spec/**/*_spec.rb']
13
+ end
14
+
15
+ desc 'Test the username_suggester plugin.'
16
+ Rake::TestTask.new(:test) do |t|
17
+ t.libs << 'lib'
18
+ t.libs << 'test'
19
+ t.pattern = 'test/**/*_test.rb'
20
+ t.verbose = true
21
+ end
22
+
23
+ desc 'Generate documentation for the username_suggester plugin.'
24
+ Rake::RDocTask.new(:rdoc) do |rdoc|
25
+ rdoc.rdoc_dir = 'rdoc'
26
+ rdoc.title = 'UsernameSuggester'
27
+ rdoc.options << '--line-numbers' << '--inline-source'
28
+ rdoc.rdoc_files.include('README')
29
+ rdoc.rdoc_files.include('lib/**/*.rb')
30
+ end
@@ -0,0 +1,63 @@
1
+ require 'set'
2
+
3
+ module UsernameSuggester
4
+ class Suggester
5
+ attr_reader :first_name
6
+ attr_reader :last_name
7
+
8
+ # Creates a UsernameSuggester to suggest usernames
9
+ #
10
+ # ==== Parameters
11
+ #
12
+ # * <tt>:first_name</tt> - Required.
13
+ # * <tt>:last_name</tt> - Required.
14
+ # * <tt>:options</tt> - There is one option -- :validate. You can pass in a Proc object and the suggestion will
15
+ # be returned if involving the Proc with the suggestion returns true
16
+ #
17
+ def initialize(first_name, last_name, options = {})
18
+ @first_name = (first_name || "").downcase
19
+ @last_name = (last_name || "").downcase
20
+ @options = options
21
+ end
22
+
23
+ # Generates the combinations without the knowledge of what names are available
24
+ def name_combinations
25
+ @name_combinations ||= [
26
+ "#{@first_name}",
27
+ "#{@last_name}",
28
+ "#{@first_name.first}#{@last_name}",
29
+ "#{@first_name}#{@last_name.first}",
30
+ "#{@first_name}#{@last_name}",
31
+ "#{@last_name.first}#{@first_name}",
32
+ "#{@last_name}#{@first_name.first}",
33
+ "#{@last_name}#{@first_name}"
34
+ ].uniq.reject { |s| s.blank? }
35
+ end
36
+
37
+ # Generates suggestions and making sure they are not in unavailable_suggestions
38
+ def suggest(max_num_suggestion, unavailable_suggestions = [])
39
+ unavailable_set = unavailable_suggestions.to_set
40
+ results = []
41
+ candidates = name_combinations.clone
42
+ while results.size < max_num_suggestion and !candidates.blank?
43
+ candidate = candidates.shift
44
+ if @options[:validate] and !@options[:validate].call(candidate)
45
+ # Do nothing
46
+ elsif unavailable_set.include? candidate
47
+ candidates << find_extended_candidate(candidate, unavailable_set)
48
+ else
49
+ results << candidate
50
+ end
51
+ end
52
+ results
53
+ end
54
+
55
+ private
56
+ # Generates a candidate with "candidate<number>" which is not included in unavailable_set
57
+ def find_extended_candidate(candidate, unavailable_set)
58
+ i = 1
59
+ i+=1 while unavailable_set.include? "#{candidate}#{i}"
60
+ "#{candidate}#{i}"
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,39 @@
1
+ module UsernameSuggester
2
+ module UsernameSuggestions
3
+ def self.included(base)
4
+ base.send :extend, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ # Creates method to generate suggestions for an username attributes. Example:
9
+ #
10
+ # suggestions_for :username, :num_suggestions => 5,
11
+ # :first_name_attribute => :first, :last_name_attribute => last
12
+ #
13
+ # will creates a "username_suggestions" method which generates suggestions of usernames based on first name
14
+ # and last name
15
+ #
16
+ # Available options are:
17
+ # <tt>:attribute</tt>:: The name of the attribute for storing username. Default is <tt>:username</tt>
18
+ # <tt>:first_name_attribute</tt>:: The attribute which stores the first name. Default is <tt>:first_name</tt>
19
+ # <tt>:last_name_attribute</tt>:: The attribute which stores the last name. Default is <tt>:last_name</tt>
20
+ # <tt>:num_suggestions</tt>:: Maximum suggestions generated. Default is <tt>10</tt>
21
+ # <tt>:validate</tt>: An Proc object which takes in an username and return true if this is an validate username
22
+ #
23
+ def suggestions_for(attribute = :username, options = {})
24
+ first_name_attribute = options[:first_name_attribute] || :first_name
25
+ last_name_attribute = options[:last_name_attribute] || :last_name
26
+ num_suggestions = options[:num_suggestions] || 10
27
+
28
+ send :define_method, "#{attribute}_suggestions".to_sym do
29
+ suggester = Suggester.new(read_attribute(first_name_attribute), read_attribute(last_name_attribute), options)
30
+ name_combinations_with_regex = suggester.name_combinations.map { |s| "^#{s}[0-9]*$" }
31
+ sql_conditions = Array.new(name_combinations_with_regex.size, "#{attribute} RLIKE ?").join(" OR ")
32
+ unavailable_choices = self.class.all(:select => attribute,
33
+ :conditions => [sql_conditions].concat(name_combinations_with_regex)).map{ |c| c.read_attribute(attribute) }
34
+ suggester.suggest(num_suggestions, unavailable_choices)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ # UsernameSuggester
2
+ require "username_suggester/suggester"
3
+
4
+ if defined?(ActiveRecord)
5
+ require "username_suggester/suggestions_for"
6
+ ActiveRecord::Base.send :include, UsernameSuggester::UsernameSuggestions
7
+ end
@@ -0,0 +1,10 @@
1
+ sqlite3:
2
+ :adapter: sqlite3
3
+ :database: vendor/plugins/username_suggester/spec/db/username_suggester.sqlite3.db
4
+
5
+ # mysql:
6
+ # :adapter: mysql
7
+ # :database: username_suggester_test
8
+ # :username: root
9
+ # :password:
10
+ # :host: localhost
data/spec/db/schema.rb ADDED
@@ -0,0 +1,7 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :users, :force => true do |t|
3
+ t.string :first_name
4
+ t.string :last_name
5
+ t.string :username
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ begin
2
+ require File.dirname(__FILE__) + '/../../../../spec/spec_helper'
3
+ rescue LoadError
4
+ puts "You need to install rspec in your base app"
5
+ exit
6
+ end
7
+
8
+ plugin_spec_dir = File.dirname(__FILE__)
9
+ ActiveRecord::Base.logger = Logger.new(plugin_spec_dir + "/debug.log")
10
+
11
+ databases = YAML::load(IO.read(plugin_spec_dir + "/db/database.yml"))
12
+ ActiveRecord::Base.establish_connection(databases[ENV["DB"] || "mysql"])
13
+ load(File.join(plugin_spec_dir, "db", "schema.rb"))
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe UsernameSuggester::Suggester do
4
+ describe "name combinations" do
5
+ it "returns combinations of first name and last name" do
6
+ UsernameSuggester::Suggester.new("Jerry", "Luk").name_combinations.should == [
7
+ "jerry",
8
+ "luk",
9
+ "jluk",
10
+ "jerryl",
11
+ "jerryluk",
12
+ "ljerry",
13
+ "lukj",
14
+ "lukjerry"
15
+ ]
16
+ end
17
+
18
+ it "returns first name and first name initial if last name is blank" do
19
+ UsernameSuggester::Suggester.new("Jerry", "").name_combinations.should == [
20
+ "jerry",
21
+ "j"
22
+ ]
23
+ end
24
+
25
+ it "returns last name and last name initial if first name is blank" do
26
+ UsernameSuggester::Suggester.new("", "Luk").name_combinations.should == [
27
+ "luk",
28
+ "l"
29
+ ]
30
+ end
31
+
32
+ it "returns empty array for blank first name and last name" do
33
+ UsernameSuggester::Suggester.new("", "").name_combinations.should == []
34
+ end
35
+ end
36
+
37
+ describe "name suggestions" do
38
+ before(:each) do
39
+ @suggester = UsernameSuggester::Suggester.new("Jerry", "Luk")
40
+ end
41
+
42
+ it "returns suggestions for names that are not in the unavailable suggestions" do
43
+ @suggester.suggest(5, []).should == ["jerry", "luk", "jluk", "jerryl", "jerryluk"]
44
+ end
45
+
46
+ it "returns extended suggestions for names that are in the unavailable suggestions" do
47
+ @suggester.suggest(10, ["jerry"]).should include "jerry1"
48
+ @suggester.suggest(10, ["jerry", "jerry1"]).should include "jerry2"
49
+ @suggester.suggest(10, ["jerry", "jerry1031"]).should include "jerry1"
50
+ end
51
+
52
+ it "should only returns suggestions passing the validate proc" do
53
+ suggestions = UsernameSuggester::Suggester.new("Jerry", "Luk",
54
+ :validate => Proc.new { |s| s.length <= 5 }).suggest(10, [])
55
+ suggestions.should include "jerry"
56
+ suggestions.should_not include "jerryl"
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ class User < ActiveRecord::Base
4
+ attr_accessible :first_name, :last_name, :username
5
+ suggestions_for :username
6
+ end
7
+
8
+ describe UsernameSuggester::UsernameSuggestions do
9
+ before(:each) do
10
+ @user = User.new(:first_name => "Jerry", :last_name => "Luk")
11
+ end
12
+
13
+ it "should able to suggest usernames" do
14
+ suggestions = @user.username_suggestions
15
+ suggestions.should_not be_blank
16
+ suggestions.should include "jerry"
17
+ end
18
+
19
+ it "should able to suggest usernames that are not taken" do
20
+ User.create!(:username => "jerry")
21
+ 1.upto(10) do |i|
22
+ User.create!(:username => "jerry#{i}")
23
+ end
24
+ suggestions = @user.username_suggestions
25
+ suggestions.should_not be_blank
26
+ suggestions.should_not include "jerry"
27
+ suggestions.should include "jerry11"
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = %q{username_suggester}
3
+ s.version = "0.1.0"
4
+
5
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
+ s.authors = ["jerryluk"]
7
+ s.date = %q{2010-06-30}
8
+ s.description = %q{Generates username suggestions for users}
9
+ s.email = %q{jerry@presdo.com}
10
+ s.extra_rdoc_files = [
11
+ "MIT-LICENSE",
12
+ "README.rdoc"
13
+ ]
14
+ s.files = [
15
+ "MIT-LICENSE",
16
+ "README.rdoc",
17
+ "Rakefile",
18
+ "username_suggester.gemspec",
19
+ "lib/username_suggester.rb",
20
+ "lib/username_suggester/suggester.rb",
21
+ "lib/username_suggester/suggestions_for.rb",
22
+ "spec/suggestions_for_spec.rb",
23
+ "spec/suggester_spec.rb",
24
+ "spec/spec_helper.rb",
25
+ "spec/db/database.yml",
26
+ "spec/db/schema.rb"
27
+ ]
28
+ s.homepage = %q{http://github.com/jerryluk/username_suggester}
29
+ s.rdoc_options = ["--charset=UTF-8"]
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.6}
32
+ s.summary = %q{Generates username suggestions for users}
33
+ s.test_files = [
34
+ "spec/suggestions_for_spec.rb",
35
+ "spec/suggester_spec.rb",
36
+ "spec/spec_helper.rb",
37
+ "spec/db/database.yml",
38
+ "spec/db/schema.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
47
+ else
48
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
+ end
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: username_suggester
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - jerryluk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-30 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ description: Generates username suggestions for users
38
+ email: jerry@presdo.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - MIT-LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - MIT-LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - username_suggester.gemspec
51
+ - lib/username_suggester.rb
52
+ - lib/username_suggester/suggester.rb
53
+ - lib/username_suggester/suggestions_for.rb
54
+ - spec/suggestions_for_spec.rb
55
+ - spec/suggester_spec.rb
56
+ - spec/spec_helper.rb
57
+ - spec/db/database.yml
58
+ - spec/db/schema.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/jerryluk/username_suggester
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Generates username suggestions for users
93
+ test_files:
94
+ - spec/suggestions_for_spec.rb
95
+ - spec/suggester_spec.rb
96
+ - spec/spec_helper.rb
97
+ - spec/db/database.yml
98
+ - spec/db/schema.rb