pg_scope_by_soundex 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +48 -0
- data/init.rb +1 -0
- data/lib/generators/pg_scope_by_soundex_install/USAGE +3 -0
- data/lib/generators/pg_scope_by_soundex_install/pg_scope_by_soundex_install_generator.rb +18 -0
- data/lib/generators/pg_scope_by_soundex_install/templates/migration.rb +42 -0
- data/lib/pg_scope_by_soundex.rb +12 -0
- data/lib/pg_scope_by_soundex/named_scope.rb +50 -0
- data/spec/pg_scope_by_soundex_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +71 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Alejandro Juarez
|
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,48 @@
|
|
1
|
+
= Description
|
2
|
+
|
3
|
+
The pg_scope_by_soundex gem provides a simple interface to use the set of functions of contrib/fuzzystringmatching for PostgreSQL.
|
4
|
+
|
5
|
+
== How to install It
|
6
|
+
|
7
|
+
Edit your Gemfile and insert the next line:
|
8
|
+
|
9
|
+
gem "pg_scope_by_soundex"
|
10
|
+
|
11
|
+
Then use the bundler to install It:
|
12
|
+
|
13
|
+
$ bundle install
|
14
|
+
|
15
|
+
Add the next line into your file config/initializers/my_gems.rb:
|
16
|
+
|
17
|
+
require 'pg_scope_by_soundex'
|
18
|
+
|
19
|
+
Then execute the rails migration generator:
|
20
|
+
|
21
|
+
$ ./script/rails generate pg_scope_by_soundex_install
|
22
|
+
|
23
|
+
And finally run the rails migrations to add the functions for PostgreSQL:
|
24
|
+
|
25
|
+
$ rake db:migrate
|
26
|
+
|
27
|
+
== How to use It
|
28
|
+
|
29
|
+
# scope_by_soundex_matcher
|
30
|
+
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
scope_by_soundex_matcher :find_by_fullname, :fields => [:firstname, :lastname]
|
33
|
+
end
|
34
|
+
|
35
|
+
Person.find_by_fullname 'Alejandro' => Returns a collection of active_record objects
|
36
|
+
|
37
|
+
|
38
|
+
# scope_by_difference_matcher
|
39
|
+
|
40
|
+
class Person < ActiveRecord::Base
|
41
|
+
scope_by_difference_matcher :find_by_fullname, :fields => [:firstname, :lastname]
|
42
|
+
end
|
43
|
+
|
44
|
+
Person.find_by_fullname 'Alejandro' => Returns a collection of active_record objects
|
45
|
+
|
46
|
+
== Copyright
|
47
|
+
|
48
|
+
Copyright (c) 2010 Alejandro Juarez. See LICENSE for details.
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'pg_scope_by_soundex'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators/migration'
|
2
|
+
class PgScopeBySoundexInstallGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
|
5
|
+
desc "Generates a migration file to add Fuzzy String Matching functions to your PostgreSQL database."
|
6
|
+
|
7
|
+
def self.source_root
|
8
|
+
@_source_root = File.expand_path('../templates', __FILE__)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.next_migration_number(path)
|
12
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
13
|
+
end
|
14
|
+
|
15
|
+
def copy_migration_template
|
16
|
+
migration_template "migration.rb", "db/migrate/add_pg_scope_by_soundex"
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
class AddPgScopeBySoundexMatcher < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
execute "SET search_path = public;"
|
5
|
+
|
6
|
+
execute "CREATE OR REPLACE FUNCTION levenshtein (text,text) RETURNS int AS '$libdir/fuzzystrmatch','levenshtein' LANGUAGE C IMMUTABLE STRICT;"
|
7
|
+
|
8
|
+
execute "CREATE OR REPLACE FUNCTION levenshtein (text,text,int,int,int) RETURNS int AS '$libdir/fuzzystrmatch','levenshtein_with_costs' LANGUAGE C IMMUTABLE STRICT;"
|
9
|
+
|
10
|
+
execute "CREATE OR REPLACE FUNCTION metaphone (text,int) RETURNS text AS '$libdir/fuzzystrmatch','metaphone'LANGUAGE C IMMUTABLE STRICT;"
|
11
|
+
|
12
|
+
execute "CREATE OR REPLACE FUNCTION soundex(text) RETURNS text AS '$libdir/fuzzystrmatch', 'soundex' LANGUAGE C IMMUTABLE STRICT;"
|
13
|
+
|
14
|
+
execute "CREATE OR REPLACE FUNCTION text_soundex(text) RETURNS text AS '$libdir/fuzzystrmatch', 'soundex' LANGUAGE C IMMUTABLE STRICT;"
|
15
|
+
|
16
|
+
execute "CREATE OR REPLACE FUNCTION difference(text,text) RETURNS int AS '$libdir/fuzzystrmatch', 'difference' LANGUAGE C IMMUTABLE STRICT;"
|
17
|
+
|
18
|
+
execute "CREATE OR REPLACE FUNCTION dmetaphone (text) RETURNS text AS '$libdir/fuzzystrmatch', 'dmetaphone' LANGUAGE C IMMUTABLE STRICT;"
|
19
|
+
|
20
|
+
execute "CREATE OR REPLACE FUNCTION dmetaphone_alt (text) RETURNS text AS '$libdir/fuzzystrmatch', 'dmetaphone_alt' LANGUAGE C IMMUTABLE STRICT;"
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.down
|
24
|
+
execute "SET search_path = public;"
|
25
|
+
|
26
|
+
execute "DROP FUNCTION dmetaphone_alt (text);"
|
27
|
+
|
28
|
+
execute "DROP FUNCTION dmetaphone (text);"
|
29
|
+
|
30
|
+
execute "DROP FUNCTION difference(text,text);"
|
31
|
+
|
32
|
+
execute "DROP FUNCTION text_soundex(text);"
|
33
|
+
|
34
|
+
execute "DROP FUNCTION soundex(text);"
|
35
|
+
|
36
|
+
execute "DROP FUNCTION metaphone (text,int);"
|
37
|
+
|
38
|
+
execute "DROP FUNCTION levenshtein (text,text,int,int,int);"
|
39
|
+
|
40
|
+
execute "DROP FUNCTION levenshtein (text,text);"
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module PgScopeBySoundex
|
2
|
+
def enable_activerecord
|
3
|
+
return if ActiveRecord::Base.respond_to? :scope_by_soundex_matcher and ActiveRecord::Base.respond_to? :scope_by_difference_matcher
|
4
|
+
require 'pg_scope_by_soundex/named_scope'
|
5
|
+
::ActiveRecord::Base.send :include, PgScopeBySoundex::NamedScope
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
if defined? Rails
|
10
|
+
include PgScopeBySoundex
|
11
|
+
PgScopeBySoundex.enable_activerecord if defined? ActiveRecord
|
12
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module PgScopeBySoundex
|
2
|
+
module NamedScope
|
3
|
+
def self.included(base)
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# scope_by_soundex_matcher
|
10
|
+
#
|
11
|
+
# Example:
|
12
|
+
# class Person < ActiveRecord::Base
|
13
|
+
# scope_by_soundex_matcher find_by_fullname, :fields => [:firstname, :lastname]
|
14
|
+
# end
|
15
|
+
# Person.find_by_fullname 'Alejandro'
|
16
|
+
def scope_by_soundex_matcher(method_name, options={})
|
17
|
+
class_eval do
|
18
|
+
sql = options[:fields].collect { |field| "soundex(#{field}) = soundex(:q)"}.join(' OR ')
|
19
|
+
scope method_name.to_sym, lambda {|string| where(sql, :q => string) }
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# scope_by_difference_matcher
|
24
|
+
#
|
25
|
+
# Example:
|
26
|
+
# class Person < ActiveRecord::Base
|
27
|
+
# scope_by_difference_matcher find_by_fullname, :fields => [:firstname, :lastname]
|
28
|
+
# OR
|
29
|
+
# scope_by_difference_matcher find_by_fullname, :fields => [:firstname, :lastname], :position => 4, :operator => '>'
|
30
|
+
# OR
|
31
|
+
# scope_by_difference_matcher find_by_fullname, :by_sql => 'difference(firstname, :q) > 3 OR difference(:lastname, :q) > 4"
|
32
|
+
# end
|
33
|
+
# Person.find_by_fullname 'Alejandro'
|
34
|
+
def scope_by_difference_matcher(method_name, options={})
|
35
|
+
class_eval do
|
36
|
+
if options[:by_sql].nil?
|
37
|
+
options[:position] ||= 3
|
38
|
+
options[:operator] = '>'
|
39
|
+
sql = options[:fields].collect { |field|"difference(#{field}, :q) #{options[:operator]} #{options[:position]}" }.join(' OR ')
|
40
|
+
else
|
41
|
+
sql = options[:by_sql]
|
42
|
+
end
|
43
|
+
scope method_name.to_sym, lambda {|string| where(sql, :q => string) }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# TODO: The following PostgreSQL functions are not implemented: dmetaphone_alt, dmetaphone, text_soundex, metaphone and levenshtein.
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_scope_by_soundex
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Alejandro Juarez
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-01 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: The pg_scope_by_soundex gem uses the set of functions of contrib/fuzzystringmatching for PostgreSQL.
|
22
|
+
email: alex@monsterlabs.com.mx
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- LICENSE
|
29
|
+
- README.rdoc
|
30
|
+
files:
|
31
|
+
- LICENSE
|
32
|
+
- init.rb
|
33
|
+
- lib/generators/pg_scope_by_soundex_install/USAGE
|
34
|
+
- lib/generators/pg_scope_by_soundex_install/pg_scope_by_soundex_install_generator.rb
|
35
|
+
- lib/generators/pg_scope_by_soundex_install/templates/migration.rb
|
36
|
+
- lib/pg_scope_by_soundex.rb
|
37
|
+
- lib/pg_scope_by_soundex/named_scope.rb
|
38
|
+
- README.rdoc
|
39
|
+
has_rdoc: true
|
40
|
+
homepage: http://github.com/alecz/pg_scope_by_soundex
|
41
|
+
licenses: []
|
42
|
+
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --charset=UTF-8
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.3.6
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: This gem provides the soundex and difference postgresql functions to determine similarities and distance between strings
|
69
|
+
test_files:
|
70
|
+
- spec/pg_scope_by_soundex_spec.rb
|
71
|
+
- spec/spec_helper.rb
|