Created
January 26, 2014 15:26
-
-
Save maksar/8634396 to your computer and use it in GitHub Desktop.
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
class IdentificationStrategy | |
def initialize origin, priorities | |
@origin = origin | |
@priorities = [priorities].flatten | |
end | |
def branch candidates | |
case candidates.length | |
when 0 | |
zero | |
when 1 | |
return candidates.first | |
when 2...Float::INFINITY | |
many candidates | |
end | |
end | |
def search company | |
database_results = query(:PAYINDEX, extract_attributes(company).collect(&extract_from(company))) | |
candidates = database_results.select(&confidently_identified(company)) | |
branch candidates | |
end | |
def extract_attributes company | |
@origin.attributes.select(&by_priority_from(company)) | |
end | |
def classify weight | |
case weight | |
when 0...@origin.suspicious_threshold | |
NegativeIdentification | |
when @origin.suspicious_threshold...@origin.identification_threshold | |
SuspiciousIdentification | |
when @origin.identification_threshold...Float::INFINITY | |
ConfidentIdentification | |
end | |
end | |
private | |
def extract_from company | |
->(attribute) { | |
[attribute.name, company.send(attribute.name)] | |
} | |
end | |
def matching candidate, company | |
->(attribute) { | |
company.send(attribute.name) == candidate.send(attribute.name) | |
} | |
end | |
def by_priority_from candidate | |
->(attribute) { | |
@priorities.include?(attribute.priority) && candidate.send(attribute.name) | |
} | |
end | |
def confidently_identified company | |
-> (candidate) { | |
equal_attributes = extract_attributes(candidate).select(&matching(candidate, company)) | |
ConfidentIdentification == classify(equal_attributes.sum(&:weight)) | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment