Class: FuzzyMatch::Rule::Identity

Inherits:
FuzzyMatch::Rule show all
Defined in:
lib/fuzzy_match/rule/identity.rb

Overview

Identities take effect when needle and haystack both match a regexp Then the captured part of the regexp has to match exactly

Instance Attribute Summary collapse

Attributes inherited from FuzzyMatch::Rule

#regexp

Instance Method Summary collapse

Constructor Details

#initialize(regexp_or_proc) ⇒ Identity

Returns a new instance of Identity.



8
9
10
11
12
13
14
15
16
17
# File 'lib/fuzzy_match/rule/identity.rb', line 8

def initialize(regexp_or_proc)
  case regexp_or_proc
  when Regexp
    @regexp = regexp_or_proc
  when Proc
    @proc = regexp_or_proc
  else
    raise ArgumentError, "[FuzzyMatch] Identity must be set with either Regexp objects or Procs, but got #{regexp_or_proc.inspect} (#{regexp_or_proc.class.name})"
  end
end

Instance Attribute Details

#procObject (readonly)

Returns the value of attribute proc.



6
7
8
# File 'lib/fuzzy_match/rule/identity.rb', line 6

def proc
  @proc
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
# File 'lib/fuzzy_match/rule/identity.rb', line 19

def ==(other)
  other.class == self.class and (regexp ? regexp == other.regexp : proc == other.proc)
end

#identical?(record1, record2) ⇒ Boolean

Two strings are “identical” if they both match this identity and the captures are equal.

Only returns true/false if both strings match the regexp. Otherwise returns nil.

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fuzzy_match/rule/identity.rb', line 27

def identical?(record1, record2)
  if regexp
    if (str1_match_data = regexp.match(record1.whole)) and (str2_match_data = regexp.match(record2.whole))
      str1_match_data.captures.join.downcase == str2_match_data.captures.join.downcase
    else
      nil
    end
  else
    proc.call record1.original, record2.original
  end
end