fuzzy_match 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,9 @@
1
+ 2.0.2 / 2013-08-11
2
+
3
+ * Enhancements
4
+
5
+ * You can pass identity rules as procs that take record1 and record2 in their original forms (note they should return nil if not sure)
6
+
1
7
  2.0.1 / 2013-06-06
2
8
 
3
9
  * Bug fixes
@@ -3,15 +3,36 @@ class FuzzyMatch
3
3
  # Identities take effect when needle and haystack both match a regexp
4
4
  # Then the captured part of the regexp has to match exactly
5
5
  class Identity < Rule
6
+ attr_reader :proc
7
+
8
+ def initialize(regexp_or_proc)
9
+ case regexp_or_proc
10
+ when Regexp
11
+ @regexp = regexp_or_proc
12
+ when Proc
13
+ @proc = regexp_or_proc
14
+ else
15
+ raise ArgumentError, "[FuzzyMatch] Identity must be set with either Regexp objects or Procs, but got #{regexp_or_proc.inspect} (#{regexp_or_proc.class.name})"
16
+ end
17
+ end
18
+
19
+ def ==(other)
20
+ other.class == self.class and (regexp ? regexp == other.regexp : proc == other.proc)
21
+ end
22
+
6
23
  # Two strings are "identical" if they both match this identity and the captures are equal.
7
24
  #
8
25
  # Only returns true/false if both strings match the regexp.
9
26
  # Otherwise returns nil.
10
27
  def identical?(record1, record2)
11
- if str1_match_data = regexp.match(record1.whole) and str2_match_data = regexp.match(record2.whole)
12
- str1_match_data.captures.join.downcase == str2_match_data.captures.join.downcase
28
+ if regexp
29
+ if (str1_match_data = regexp.match(record1.whole)) and (str2_match_data = regexp.match(record2.whole))
30
+ str1_match_data.captures.join.downcase == str2_match_data.captures.join.downcase
31
+ else
32
+ nil
33
+ end
13
34
  else
14
- nil
35
+ proc.call record1.original, record2.original
15
36
  end
16
37
  end
17
38
  end
@@ -1,3 +1,3 @@
1
1
  class FuzzyMatch
2
- VERSION = '2.0.1'
2
+ VERSION = '2.0.2'
3
3
  end
@@ -96,12 +96,17 @@ describe FuzzyMatch do
96
96
  it %{sometimes gets false results without them} do
97
97
  # false positive without identity
98
98
  d = FuzzyMatch.new %w{ foo bar }
99
+ d.find('bar').should == 'bar'
100
+ d.find('bare').should == 'bar'
99
101
  d.find('baz').should == 'bar'
100
102
  end
101
103
 
102
104
  it %{can be used to improve results} do
103
105
  d = FuzzyMatch.new %w{ foo bar }, :identities => [ /ba(.)/ ]
106
+ d.find('bar').should == 'bar'
107
+ d.find('bare').should == 'bar'
104
108
  d.find('baz').should be_nil
109
+ d.find('baze').should be_nil
105
110
  end
106
111
 
107
112
  it %{is sort of like backreferences} do
@@ -112,6 +117,14 @@ describe FuzzyMatch do
112
117
  d = FuzzyMatch.new([one, two], :identities => [/\A(\d+)\s+(\w+)/])
113
118
  d.find('1 sauk TWOTWOTWOTWO').should == one # correct
114
119
  end
120
+
121
+ it %{has a proc form} do
122
+ d = FuzzyMatch.new %w{ foo bar }, :identities => [ lambda { |a, b| (a.start_with?('ba') and b.start_with?('ba') ? a[2] == b[2] : nil) } ]
123
+ d.find('bar').should == 'bar'
124
+ d.find('bare').should == 'bar'
125
+ d.find('baz').should be_nil
126
+ d.find('baze').should be_nil
127
+ end
115
128
  end
116
129
 
117
130
  describe 'groupings' do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fuzzy_match
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-06 00:00:00.000000000 Z
12
+ date: 2013-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_record_inline_schema