class MatchMate::AddressMatcher

Public Class Methods

attributes() click to toggle source
# File lib/match-mate/address_matcher.rb, line 3
def self.attributes
  @attributes ||= (MatchMate.config.address_weights || {}).deep_merge DEFAULTS[:address_weights]
end
new(address, other_address) click to toggle source
# File lib/match-mate/address_matcher.rb, line 15
def initialize(address, other_address)
  @address = address.is_a?(MatchMate::Address) ? address : Address.new(address)
  @other_address = other_address.is_a?(MatchMate::Address) ? other_address : Address.new(other_address)
end
threshold() click to toggle source
# File lib/match-mate/address_matcher.rb, line 7
def self.threshold
  @threshold ||= MatchMate.config.address_threshold || DEFAULTS[:address_threshold]
end
total_weight() click to toggle source
# File lib/match-mate/address_matcher.rb, line 11
def self.total_weight
  @total_weight ||= attributes.values.sum { |config| config[:weight] }
end

Public Instance Methods

match?() click to toggle source
# File lib/match-mate/address_matcher.rb, line 24
def match?
  score >= self.class.threshold
end
score() click to toggle source
# File lib/match-mate/address_matcher.rb, line 20
def score
  @score ||= calculate_score.round
end

Private Instance Methods

adjusted_weight_for(weight) click to toggle source
# File lib/match-mate/address_matcher.rb, line 44
def adjusted_weight_for(weight)
  ((self.class.total_weight.to_f / 100) * weight) / 100
end
calculate_score() click to toggle source
# File lib/match-mate/address_matcher.rb, line 30
def calculate_score
  self.class.attributes.keys.sum do |attribute|
    weighted_score_for attribute
  end
end
score_for(attribute) click to toggle source
# File lib/match-mate/address_matcher.rb, line 48
def score_for(attribute)
  FuzzyWuzzy.new(@address.send(attribute), @other_address.send(attribute)).token_set_ratio
end
weighted_score_for(attribute) click to toggle source
# File lib/match-mate/address_matcher.rb, line 36
def weighted_score_for(attribute)
  value = score_for(attribute)
  config = self.class.attributes[attribute]
  return 0 if value < config[:threshold]

  value * adjusted_weight_for(config[:weight])
end