-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ComparisonHelper into FieldComparison classes for each fields
- Loading branch information
Showing
16 changed files
with
538 additions
and
356 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/models/atlas_engine/address_validation/validators/full_address/building_comparison.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module AtlasEngine | ||
module AddressValidation | ||
module Validators | ||
module FullAddress | ||
class BuildingComparison < FieldComparisonBase | ||
extend T::Sig | ||
|
||
sig { override.returns(T.nilable(NumberComparison)) } | ||
def compare | ||
@building_comparison ||= NumberComparison.new( | ||
numbers: datastore.parsings.potential_building_numbers, | ||
candidate_ranges: building_ranges_from_candidate(candidate), | ||
) | ||
end | ||
|
||
private | ||
|
||
sig { params(candidate: Candidate).returns(T::Array[AddressNumberRange]) } | ||
def building_ranges_from_candidate(candidate) | ||
building_and_unit_ranges = candidate.component(:building_and_unit_ranges)&.value | ||
return [] if building_and_unit_ranges.blank? | ||
|
||
building_ranges = JSON.parse(building_and_unit_ranges).keys | ||
building_ranges.map { |building_range| AddressNumberRange.new(range_string: building_range) } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
25 changes: 25 additions & 0 deletions
25
app/models/atlas_engine/address_validation/validators/full_address/city_comparison.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module AtlasEngine | ||
module AddressValidation | ||
module Validators | ||
module FullAddress | ||
class CityComparison < FieldComparisonBase | ||
extend T::Sig | ||
|
||
sig { override.returns(T.nilable(Token::Sequence::Comparison)) } | ||
def compare | ||
return @city_comparison if defined?(@city_comparison) | ||
|
||
@city_comparison = best_comparison( | ||
datastore.fetch_city_sequence, | ||
T.must(candidate.component(:city)).sequences, | ||
field_policy(:city), | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
147 changes: 0 additions & 147 deletions
147
app/models/atlas_engine/address_validation/validators/full_address/comparison_helper.rb
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
app/models/atlas_engine/address_validation/validators/full_address/field_comparison_base.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module AtlasEngine | ||
module AddressValidation | ||
module Validators | ||
module FullAddress | ||
class FieldComparisonBase | ||
extend T::Sig | ||
extend T::Helpers | ||
|
||
abstract! | ||
|
||
sig { params(address: AbstractAddress, candidate: Candidate, datastore: DatastoreBase).void } | ||
def initialize(address:, candidate:, datastore:) | ||
@address = address | ||
@datastore = datastore | ||
@candidate = candidate | ||
end | ||
|
||
sig { abstract.returns(T.any(T.nilable(Token::Sequence::Comparison), T.nilable(NumberComparison))) } | ||
def compare; end | ||
|
||
private | ||
|
||
sig { returns(AbstractAddress) } | ||
attr_reader :address | ||
|
||
sig { returns(DatastoreBase) } | ||
attr_reader :datastore | ||
|
||
sig { returns(Candidate) } | ||
attr_reader :candidate | ||
|
||
sig do | ||
params( | ||
sequence: Token::Sequence, | ||
component_sequences: T::Array[Token::Sequence], | ||
comparison_policy: Token::Sequence::ComparisonPolicy, | ||
).returns(T.nilable(Token::Sequence::Comparison)) | ||
end | ||
def best_comparison( | ||
sequence, | ||
component_sequences, | ||
comparison_policy = Token::Sequence::ComparisonPolicy::DEFAULT_POLICY | ||
) | ||
component_sequences.map do |component_sequence| | ||
Token::Sequence::Comparator.new( | ||
left_sequence: sequence, | ||
right_sequence: component_sequence, | ||
comparison_policy:, | ||
).compare | ||
end.min_by.with_index do |comparison, index| | ||
# ruby's `min` and `sort` methods are not stable | ||
# so we need to prefer the leftmost comparison when two comparisons are equivalent | ||
[comparison, index] | ||
end | ||
end | ||
|
||
sig { params(field: Symbol).returns(Token::Sequence::ComparisonPolicy) } | ||
def field_policy(field) | ||
datastore.country_profile.validation.comparison_policy(field) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
...odels/atlas_engine/address_validation/validators/full_address/province_code_comparison.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
module AtlasEngine | ||
module AddressValidation | ||
module Validators | ||
module FullAddress | ||
class ProvinceCodeComparison < FieldComparisonBase | ||
extend T::Sig | ||
|
||
sig { override.returns(T.nilable(Token::Sequence::Comparison)) } | ||
def compare | ||
return @province_code_comparison if defined?(@province_code_comparison) | ||
|
||
normalized_session_province_code = ValidationTranscriber::ProvinceCodeNormalizer.normalize( | ||
country_code: address.country_code, | ||
province_code: address.province_code, | ||
) | ||
normalized_candidate_province_code = ValidationTranscriber::ProvinceCodeNormalizer.normalize( | ||
country_code: T.must(candidate.component(:country_code)).value, | ||
province_code: T.must(candidate.component(:province_code)).value, | ||
) | ||
|
||
@province_code_comparison = best_comparison( | ||
Token::Sequence.from_string(normalized_session_province_code), | ||
[Token::Sequence.from_string(normalized_candidate_province_code)], | ||
field_policy(:province_code), | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.