fuzzy_record 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +8 -0
- data/fuzzy_record.gemspec +30 -0
- data/lib/fuzzy_record/fuzzy_searcher.rb +24 -0
- data/lib/fuzzy_record/version.rb +3 -0
- data/lib/fuzzy_record.rb +12 -0
- data/test/fuzzy_record_test.rb +53 -0
- data/test/test_helper.rb +11 -0
- metadata +183 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a99f00e5952ab300334f7484bdfdaca35aab18c9
|
4
|
+
data.tar.gz: da59bc75ff14e06de1b1564f79a3cd65acf887aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 50d161e9909a4d16b9bc4a138c0512f932bf9e9c9a98c8b41d98d8aa3db303c08183bdc04db2ca85a07f4f9b5f612206f3c3016e93d0773124c6d70b787d5787
|
7
|
+
data.tar.gz: e8c3ef7214bf2c9f5a303c77b555f11756ca299def56ef5c24ea7f30eb98843c71b50e54bc7130028ccb4996b88bfeb76eb765779464ceb9a42b838aea54e635
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Chris Moody
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# FuzzyRecord
|
2
|
+
|
3
|
+
FuzzyRecord searches through active records using fuzzy matchers and orders them based on FuzzyString.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'fuzzy_record'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install fuzzy_record
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To use for any active record model:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
$ Model.fuzzy_search('string')
|
27
|
+
```
|
28
|
+
|
29
|
+
It will search through your model for records where the first of these that exists
|
30
|
+
(:fuzzy_name, :ident, :name) fuzzy_matches and order it by best match
|
31
|
+
|
32
|
+
Alternatively you can use a hash where the field to be searched is the key:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
$ Model.fuzzy_search(title: 'string')
|
36
|
+
```
|
37
|
+
|
38
|
+
To make this the default behavior for your model simply add the definition:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
$ def self.fuzzy_search(str) super(title: str) end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
1. Fork it ( https://github.com/[my-github-username]/fuzzy_record/fork )
|
47
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
48
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
49
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
50
|
+
5. Create a new Pull Request
|
51
|
+
|
52
|
+
Author
|
53
|
+
-------
|
54
|
+
|
55
|
+
* Chris Moody
|
56
|
+
|
57
|
+
License
|
58
|
+
-------
|
59
|
+
|
60
|
+
This is free software released into the public domain.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'fuzzy_record/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "fuzzy_record"
|
8
|
+
spec.version = FuzzyRecord::VERSION
|
9
|
+
spec.authors = ["Chris Moody"]
|
10
|
+
spec.email = ["cmoody@transcon.com"]
|
11
|
+
spec.summary = %q{Active Record fuzzy search}
|
12
|
+
spec.homepage = "https://github.com/transcon/fuzzy_record"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "fuzzy_string"
|
21
|
+
spec.add_dependency "rails"
|
22
|
+
|
23
|
+
spec.add_development_dependency "flying_table"
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency 'minitest'
|
27
|
+
spec.add_development_dependency 'awesome_print'
|
28
|
+
spec.add_development_dependency 'minitest-reporters', '>= 1.0.1'
|
29
|
+
spec.add_development_dependency 'sqlite3'
|
30
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class FuzzyRecord::FuzzySearcher
|
2
|
+
def self.find(klass,args) new(klass,args).find end
|
3
|
+
def initialize(klass,args)
|
4
|
+
@class = klass
|
5
|
+
@args = args
|
6
|
+
@search = create_search
|
7
|
+
end
|
8
|
+
def find
|
9
|
+
@class.all.select{ |record| include_record(record)}.sort_by{|record| sorter(record)}
|
10
|
+
end
|
11
|
+
private
|
12
|
+
def generalize(str) ".*#{str.gsub(/[^a-zA-Z&0-9]/, "").chars.to_a.join(".*")}.*" end
|
13
|
+
def ave(arry) arry.sum.to_f / arry.length end
|
14
|
+
def matches?(record,k,v) !!(record.send(k) =~ /#{generalize(v)}/i) end
|
15
|
+
def sorter(record) ave(@search.map{|k,v| record.send(k) ^ v}) end
|
16
|
+
def include_record(record) @search.map{|k,v| matches?(record,k,v)}.inject(:&) end
|
17
|
+
|
18
|
+
def create_search
|
19
|
+
return @args if @args.is_a? Hash
|
20
|
+
[:fuzzy_name, :ident, :name].each do |field|
|
21
|
+
return {field => @args} if @class.instance_methods.include?(field)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/fuzzy_record.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "fuzzy_record/version"
|
2
|
+
require "fuzzy_record/fuzzy_searcher"
|
3
|
+
require "active_record"
|
4
|
+
require "fuzzy_string"
|
5
|
+
module FuzzyRecord
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
module ClassMethods
|
8
|
+
def fuzzy_search(args) FuzzySearcher.find(self,args) end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
ActiveRecord::Base.include(FuzzyRecord)
|
12
|
+
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
class FuzzyRecordTest < TestCase
|
3
|
+
def test_search_responds_to_name
|
4
|
+
FlyingTable.create(test_tables: {name: :string, title: :string})
|
5
|
+
first = TestTable.create(name: 'A very long string')
|
6
|
+
second = TestTable.create(name: 'A very longer string')
|
7
|
+
search = TestTable.fuzzy_search('a very long')
|
8
|
+
assert_equal 2, search.count
|
9
|
+
assert_equal first, search.first
|
10
|
+
FlyingTable.destroy(:test_tables)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_search_responds_to_ident
|
14
|
+
FlyingTable.create(ident_test_tables: {ident: :string, name: :string, title: :string})
|
15
|
+
first = IdentTestTable.create(name: 'A very long string', ident: 'a longer string')
|
16
|
+
second = IdentTestTable.create(name: 'A very longer string', ident: 'a long string')
|
17
|
+
search = IdentTestTable.fuzzy_search('a long string')
|
18
|
+
assert_equal 2, search.count
|
19
|
+
assert_equal second, search.first
|
20
|
+
FlyingTable.destroy(:ident_test_tables)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_search_responds_to_fuzzy_name
|
24
|
+
FlyingTable.create(fuzzy_test_tables: {fuzzy_name: :string, ident: :string, name: :string,title: :string})
|
25
|
+
first = FuzzyTestTable.create(ident: 'a longer string', fuzzy_name: 'a string')
|
26
|
+
second = FuzzyTestTable.create(ident: 'a long string', fuzzy_name: 'a stringer')
|
27
|
+
search = FuzzyTestTable.fuzzy_search('a string')
|
28
|
+
assert_equal 2, search.count
|
29
|
+
assert_equal first, search.first
|
30
|
+
FlyingTable.destroy(:fuzzy_test_tables)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_search_responds_to_any_name
|
34
|
+
FlyingTable.create(any_name_test_tables: {fuzzy_name: :string, ident: :string, name: :string,title: :string})
|
35
|
+
first = AnyNameTestTable.create(ident: 'a longer string', title: 'a string')
|
36
|
+
second = AnyNameTestTable.create(ident: 'a long string', title: 'a stringer')
|
37
|
+
search = AnyNameTestTable.fuzzy_search(title: 'a string')
|
38
|
+
assert_equal 2, search.count
|
39
|
+
assert_equal first, search.first
|
40
|
+
FlyingTable.destroy(:any_name_test_tables)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_search_responds_to_multiple_name
|
44
|
+
FlyingTable.create(multiple_name_test_tables: {fuzzy_name: :string, ident: :string, name: :string,title: :string})
|
45
|
+
first = MultipleNameTestTable.create(ident: 'a long string', title: 'a string')
|
46
|
+
second = MultipleNameTestTable.create(ident: 'a longer string', title: 'a stringer')
|
47
|
+
search = MultipleNameTestTable.fuzzy_search(title: 'a string', ident: 'a longer string')
|
48
|
+
assert_equal 1, search.count
|
49
|
+
assert_equal second, search.first
|
50
|
+
FlyingTable.destroy(:multiple_name_test_tables)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'fuzzy_record'
|
3
|
+
require 'minitest/reporters'
|
4
|
+
require 'flying_table'
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
6
|
+
|
7
|
+
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
|
8
|
+
|
9
|
+
|
10
|
+
class TestCase < MiniTest::Test
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,183 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fuzzy_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Chris Moody
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: fuzzy_string
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: flying_table
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: awesome_print
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: minitest-reporters
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 1.0.1
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 1.0.1
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: sqlite3
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description:
|
140
|
+
email:
|
141
|
+
- cmoody@transcon.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- Gemfile
|
148
|
+
- LICENSE.txt
|
149
|
+
- README.md
|
150
|
+
- Rakefile
|
151
|
+
- fuzzy_record.gemspec
|
152
|
+
- lib/fuzzy_record.rb
|
153
|
+
- lib/fuzzy_record/fuzzy_searcher.rb
|
154
|
+
- lib/fuzzy_record/version.rb
|
155
|
+
- test/fuzzy_record_test.rb
|
156
|
+
- test/test_helper.rb
|
157
|
+
homepage: https://github.com/transcon/fuzzy_record
|
158
|
+
licenses:
|
159
|
+
- MIT
|
160
|
+
metadata: {}
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 2.4.3
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: Active Record fuzzy search
|
181
|
+
test_files:
|
182
|
+
- test/fuzzy_record_test.rb
|
183
|
+
- test/test_helper.rb
|