Class: AtlasEngine::AddressValidation::Token::Comparator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Gem::Text
Defined in:
app/models/atlas_engine/address_validation/token/comparator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(left_token, right_token) ⇒ Comparator

Returns a new instance of Comparator.



17
18
19
20
# File 'app/models/atlas_engine/address_validation/token/comparator.rb', line 17

def initialize(left_token, right_token)
  @left = T.let(left_token, Token)
  @right = T.let(right_token, Token)
end

Instance Attribute Details

#leftObject (readonly)

Returns the value of attribute left.



14
15
16
# File 'app/models/atlas_engine/address_validation/token/comparator.rb', line 14

def left
  @left
end

#rightObject (readonly)

Returns the value of attribute right.



14
15
16
# File 'app/models/atlas_engine/address_validation/token/comparator.rb', line 14

def right
  @right
end

Instance Method Details

#compareObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/atlas_engine/address_validation/token/comparator.rb', line 23

def compare
  left_value = left.value
  right_value = right.value

  if left_value == right_value
    Comparison.new(left: left, right: right, qualifier: :equal, edit_distance: 0)
  else
    edit = levenshtein_distance(left_value, right_value)

    if right_value.start_with?(left_value) || left_value.start_with?(right_value)
      Comparison.new(left: left, right: right, qualifier: :prefix, edit_distance: edit)
    elsif right_value.end_with?(left_value) || left_value.end_with?(right_value)
      Comparison.new(left: left, right: right, qualifier: :suffix, edit_distance: edit)
    else
      Comparison.new(left: left, right: right, qualifier: :comp, edit_distance: edit)
    end
  end
end