Chrononaut-no_fuzz 0.0.1 → 0.0.2

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/CHANGELOG CHANGED
@@ -1 +1,2 @@
1
- v0.0.1 Test release
1
+ v0.0.2. Basic normalization, bug fixes and documentation. First patch from a contributor - thanks sabman!
2
+ v0.0.1. Test release.
data/Manifest CHANGED
@@ -1,21 +1,20 @@
1
1
  Rakefile
2
2
  README.markdown
3
+ no_fuzz.gemspec
3
4
  tasks/no_fuzz_tasks.rake
4
- uninstall.rb
5
5
  init.rb
6
- generators/no_fuzz/no_fuzz_generator.rb
7
- generators/no_fuzz/templates/model.rb
8
- generators/no_fuzz/templates/migration.rb
9
- generators/no_fuzz/USAGE
10
- rails/init.rb
11
- CHANGELOG
12
6
  lib/no_fuzz.rb
7
+ generators/no_fuzz/USAGE
8
+ generators/no_fuzz/templates/migration.rb
9
+ generators/no_fuzz/templates/model.rb
10
+ generators/no_fuzz/no_fuzz_generator.rb
13
11
  MIT-LICENSE
14
- no_fuzz.gemspec
15
12
  install.rb
16
13
  test/no_fuzz_test.rb
17
- test/test_helper.rb
18
14
  test/database.yml
19
15
  test/schema.rb
20
- test/test.sqlite3
16
+ test/test_helper.rb
17
+ rails/init.rb
18
+ CHANGELOG
19
+ uninstall.rb
21
20
  Manifest
data/README.markdown CHANGED
@@ -1,21 +1,34 @@
1
- # NoFuzz
1
+ # No Fuzz
2
+ ## Simple Rails-plugin that provides offline fuzzy-search for ActiveRecord
2
3
 
3
4
  Simple as can be fuzzy search. Works with any database supported by Active
4
- Record. No dependencies.
5
-
6
- Based on code and ideas in Steven Ruttenberg's nice blog entry "Live fuzzy
7
- search using n-grams in Rails" [1].
5
+ Record. No dependencies. The notion of being offline means that it creates an
6
+ index which the search algorithm leverages. Note that this index has to be
7
+ updated to reflect changes in the database content. The plugin creates a
8
+ separate database model for the index. This means that no changes will be made
9
+ to your existing schema.
8
10
 
9
11
  Note that this family of fuzzy search techniques work best on dictionary-type
10
- lookups, i.e not for large amounts of text.
12
+ lookups (names, words, etc).
13
+
14
+ The plugin is based on code and ideas in Steven Ruttenberg's nice blog entry
15
+ ["Live fuzzy search using n-grams in Rails"](http://unirec.blogspot.com/2007/12/live-fuzzy-search-using-n-grams-in.html).
11
16
 
12
- kristian's acts_as_fuzzy_search [2] is a similar plugin, but it targets DataMapper.
17
+ kristian's
18
+ [acts_as_fuzzy_search](http://github.com/mkristian/kristians_rails_plugins/tree/master/act_as_fuzzy_search)
19
+ is a similar plugin, but it targets DataMapper.
13
20
 
14
- 1: http://unirec.blogspot.com/2007/12/live-fuzzy-search-using-n-grams-in.html
21
+ ## Installation and Setup
15
22
 
16
- 2: http://github.com/mkristian/kristians_rails_plugins/tree/master/act_as_fuzzy_search
23
+ cd your_rails_project
24
+ script/plugin install git://github.com/Chrononaut/no_fuzz.git
25
+ # Now we create a trigram migration for the model we want to add fuzzy search to:
26
+ script/generate no_fuzz Model
27
+ rake db:migrate
17
28
 
18
- # Basic Usage
29
+ (Note that the plugin is available as a gem as well - you don't have to use script/plugin)
30
+
31
+ ## Basic Usage
19
32
 
20
33
  Add the following code in the model you'd like to index:
21
34
 
@@ -31,4 +44,16 @@ search fuzzily with the fuzzy_find method:
31
44
  Model.fuzzy_find("query")
32
45
  Model.fuzzy_find("query", 10) # find maximum 10 rows
33
46
 
47
+ A concrete example from a real app can look like this:
48
+
49
+ >> Contractor.fuzzy_find('johm')
50
+ => [#<Contractor id: 1, full_name: "John Doe", created_at: "2009-04-30 10:05:02", updated_at: "2009-04-30 10:05:02">]
51
+
52
+ ## Contributors
53
+
54
+ The following people have submitted changes which have been applied to the core:
55
+
56
+ * [Bjørn Arild Mæland](http://github.com/Chrononaut)
57
+ * [Shoaib Burq](http://github.com/sabman)
58
+
34
59
  Copyright (c) 2009 Bjørn Arild Mæland, released under the MIT license
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ Echoe.new('no_fuzz') do |p|
13
13
  p.author = "Bjørn Arild Mæland"
14
14
  p.email = "bjorn.maeland@gmail.com"
15
15
  p.summary = "No Fuzz"
16
- p.url = "http://www.github.com/Chrononaut/no_fuzz/"
16
+ p.url = "http://github.com/Chrononaut/no_fuzz"
17
17
  p.ignore_pattern = FileList[".gitignore"]
18
18
  p.include_rakefile = true
19
19
  end
@@ -13,21 +13,20 @@ class NoFuzzGenerator < Rails::Generator::NamedBase
13
13
  end
14
14
 
15
15
  private
16
- def local_custom_name
17
- class_name.underscore.downcase
18
- end
19
-
20
- def gracefully_pluralize(str)
21
- str.pluralize! if ActiveRecord::Base.pluralize_table_names
22
- str
23
- end
24
-
25
16
  def local_assigns
26
17
  returning(assigns = {}) do
27
- assigns[:class_name] = local_custom_name.classify
18
+ assigns[:class_name] = class_name.underscore.downcase.classify
28
19
  assigns[:migration_class_name] = "CreateTrigramsTableFor#{assigns[:class_name]}"
29
- assigns[:table_name] = gracefully_pluralize(local_custom_name + "_trigram")
20
+ assigns[:table_name] = gracefully_pluralize(class_name.underscore.downcase + "_trigram")
30
21
  assigns[:foreign_key] = (class_name.underscore.downcase + "_id")
31
22
  end
32
23
  end
33
24
  end
25
+
26
+ module GeneratorHelpers
27
+ def gracefully_pluralize(str)
28
+ ActiveRecord::Base.pluralize_table_names ? str.pluralize : str
29
+ end
30
+ end
31
+
32
+ NoFuzzGenerator.send(:include, GeneratorHelpers)
@@ -1,4 +1,4 @@
1
- class <%= migration_name -%> < ActiveRecord::Migration
1
+ class <%= migration_class_name -%> < ActiveRecord::Migration
2
2
  def self.up
3
3
  create_table :<%= table_name -%>, :force => true do |t|
4
4
  t.integer :<%= foreign_key -%>, :null => false
data/lib/no_fuzz.rb CHANGED
@@ -2,7 +2,6 @@
2
2
  # - scores
3
3
  # I.e fuzzy {:first_name => 1, :last_name => 2}, last_name gives double score
4
4
  # Currently everything gets scored with 1
5
- # - normalization
6
5
  # - weighting of fuzzy_find results
7
6
 
8
7
  module NoFuzz
@@ -18,7 +17,7 @@ module NoFuzz
18
17
  def fuzzy(*fields)
19
18
  # put the parameters as instance variable of the model
20
19
  @@model.instance_variable_set(:@fuzzy_fields, fields)
21
- @@model.instance_variable_set(:@fuzzy_ref_id, "#{@@model}_id".downcase)
20
+ @@model.instance_variable_set(:@fuzzy_ref_id, "#{@@model.to_s.demodulize.underscore}_id")
22
21
  @@model.instance_variable_set(:@fuzzy_trigram_model, "#{@@model}Trigram".constantize)
23
22
  end
24
23
 
@@ -33,7 +32,8 @@ module NoFuzz
33
32
  self.all.each do |i|
34
33
  word = ' ' + i.send(f)
35
34
  (0..word.length-3).each do |idx|
36
- tg = word[idx,3]
35
+ tg = word[idx,3].downcase # Force normalization by downcasing for
36
+ # now - should be overridable by the user
37
37
  trigram_model.create(:tg => tg, fuzzy_ref_id => i.id)
38
38
  end
39
39
  end
data/no_fuzz.gemspec CHANGED
@@ -2,17 +2,17 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{no_fuzz}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Bj\303\270rn Arild M\303\246land"]
9
- s.date = %q{2009-03-30}
9
+ s.date = %q{2009-04-30}
10
10
  s.description = %q{No Fuzz}
11
11
  s.email = %q{bjorn.maeland@gmail.com}
12
- s.extra_rdoc_files = ["README.markdown", "tasks/no_fuzz_tasks.rake", "CHANGELOG", "lib/no_fuzz.rb"]
13
- s.files = ["Rakefile", "README.markdown", "tasks/no_fuzz_tasks.rake", "uninstall.rb", "init.rb", "generators/no_fuzz/no_fuzz_generator.rb", "generators/no_fuzz/templates/model.rb", "generators/no_fuzz/templates/migration.rb", "generators/no_fuzz/USAGE", "rails/init.rb", "CHANGELOG", "lib/no_fuzz.rb", "MIT-LICENSE", "install.rb", "test/no_fuzz_test.rb", "test/test_helper.rb", "test/database.yml", "test/schema.rb", "test/test.sqlite3", "Manifest", "no_fuzz.gemspec"]
12
+ s.extra_rdoc_files = ["README.markdown", "tasks/no_fuzz_tasks.rake", "lib/no_fuzz.rb", "CHANGELOG"]
13
+ s.files = ["Rakefile", "README.markdown", "no_fuzz.gemspec", "tasks/no_fuzz_tasks.rake", "init.rb", "lib/no_fuzz.rb", "generators/no_fuzz/USAGE", "generators/no_fuzz/templates/migration.rb", "generators/no_fuzz/templates/model.rb", "generators/no_fuzz/no_fuzz_generator.rb", "MIT-LICENSE", "install.rb", "test/no_fuzz_test.rb", "test/database.yml", "test/schema.rb", "test/test_helper.rb", "rails/init.rb", "CHANGELOG", "uninstall.rb", "Manifest"]
14
14
  s.has_rdoc = true
15
- s.homepage = %q{http://www.github.com/Chrononaut/no_fuzz/}
15
+ s.homepage = %q{http://github.com/Chrononaut/no_fuzz}
16
16
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "No_fuzz", "--main", "README.markdown"]
17
17
  s.require_paths = ["lib"]
18
18
  s.rubyforge_project = %q{no_fuzz}
@@ -25,11 +25,8 @@ Gem::Specification.new do |s|
25
25
  s.specification_version = 2
26
26
 
27
27
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
28
- s.add_development_dependency(%q<echoe>, [">= 0"])
29
28
  else
30
- s.add_dependency(%q<echoe>, [">= 0"])
31
29
  end
32
30
  else
33
- s.add_dependency(%q<echoe>, [">= 0"])
34
31
  end
35
32
  end
data/test/no_fuzz_test.rb CHANGED
@@ -1,18 +1,38 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'rails_generator'
3
+ require 'rails_generator/scripts/generate'
4
+ require 'generators/no_fuzz/no_fuzz_generator'
2
5
 
3
6
  class NoFuzzTest < ActiveSupport::TestCase
4
7
 
5
8
  load_schema
6
9
 
7
- class Package < ActiveRecord::Base
10
+ class PackageTrigram < ActiveRecord::Base
8
11
  end
9
12
 
10
- class PackageTrigram < ActiveRecord::Base
13
+ class Package < ActiveRecord::Base
14
+ include NoFuzz
15
+
16
+ fuzzy :name
11
17
  end
12
18
 
13
- def test_schema_has_loaded_correctly
14
- assert_equal [], Package.all
15
- assert_equal [], PackageTrigram.all
19
+ def test_populating_and_deleting_trigram_index
20
+ Package.create!(:name => "abcdef")
21
+ Package.populate_trigram_index
22
+ assert_equal 5, PackageTrigram.count
23
+ Package.clear_trigram_index
24
+ assert_equal 0, PackageTrigram.count
16
25
  end
17
26
 
18
27
  end
28
+
29
+ class GeneratorHelpersTest < Test::Unit::TestCase
30
+ include GeneratorHelpers
31
+
32
+ def test_graceful_pluralization
33
+ ActiveRecord::Base.pluralize_table_names = false
34
+ assert_equal "chicken", gracefully_pluralize("chicken")
35
+ ActiveRecord::Base.pluralize_table_names = true
36
+ assert_equal "chickens", gracefully_pluralize("chicken")
37
+ end
38
+ end
data/test/test_helper.rb CHANGED
@@ -29,6 +29,6 @@ def load_schema
29
29
  end
30
30
 
31
31
  ActiveRecord::Base.establish_connection(config[db_adapter])
32
- #load(File.dirname(__FILE__) + "/schema.rb")
32
+ load(File.dirname(__FILE__) + "/schema.rb")
33
33
  require File.dirname(__FILE__) + '/../rails/init.rb'
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: Chrononaut-no_fuzz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Bj\xC3\xB8rn Arild M\xC3\xA6land"
@@ -9,19 +9,10 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-30 00:00:00 -07:00
12
+ date: 2009-04-30 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: echoe
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "0"
24
- version:
14
+ dependencies: []
15
+
25
16
  description: No Fuzz
26
17
  email: bjorn.maeland@gmail.com
27
18
  executables: []
@@ -31,32 +22,31 @@ extensions: []
31
22
  extra_rdoc_files:
32
23
  - README.markdown
33
24
  - tasks/no_fuzz_tasks.rake
34
- - CHANGELOG
35
25
  - lib/no_fuzz.rb
26
+ - CHANGELOG
36
27
  files:
37
28
  - Rakefile
38
29
  - README.markdown
30
+ - no_fuzz.gemspec
39
31
  - tasks/no_fuzz_tasks.rake
40
- - uninstall.rb
41
32
  - init.rb
42
- - generators/no_fuzz/no_fuzz_generator.rb
43
- - generators/no_fuzz/templates/model.rb
44
- - generators/no_fuzz/templates/migration.rb
45
- - generators/no_fuzz/USAGE
46
- - rails/init.rb
47
- - CHANGELOG
48
33
  - lib/no_fuzz.rb
34
+ - generators/no_fuzz/USAGE
35
+ - generators/no_fuzz/templates/migration.rb
36
+ - generators/no_fuzz/templates/model.rb
37
+ - generators/no_fuzz/no_fuzz_generator.rb
49
38
  - MIT-LICENSE
50
39
  - install.rb
51
40
  - test/no_fuzz_test.rb
52
- - test/test_helper.rb
53
41
  - test/database.yml
54
42
  - test/schema.rb
55
- - test/test.sqlite3
43
+ - test/test_helper.rb
44
+ - rails/init.rb
45
+ - CHANGELOG
46
+ - uninstall.rb
56
47
  - Manifest
57
- - no_fuzz.gemspec
58
48
  has_rdoc: true
59
- homepage: http://www.github.com/Chrononaut/no_fuzz/
49
+ homepage: http://github.com/Chrononaut/no_fuzz
60
50
  post_install_message:
61
51
  rdoc_options:
62
52
  - --line-numbers
data/test/test.sqlite3 DELETED
Binary file