-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts common Detector logic to module
Why are these changes being introduced: * We were starting to accumulate technical debt each time we added a new detector with a lot of similar (or duplicate) code Relevant ticket(s): * https://mitlibraries.atlassian.net/browse/TCO-104 How does this address that need: * A new module was created `pattern_checker` and included into the detectors that could use it with minimal changes. Document any side effects to this change: * Two options where considered, and ultimately modules were chosen * The other option was model inheritance which felt appropriate until considering that the parent model `Detector` has absolutely nothing in common with the individual detectors that are namespaced under it. Forcing this commonality was noticed when linters suggested that it is appropriate to call `super` when initializing a child class. We could of course skip that linter, and I considered it. But as I pondered whether I actually wanted to suggest that path, I tried the alternative path of extracting the common logic in a module, which doesn't have the side effect of potentially having inheritance for convenience rather than allowing `Detector` to only focus on what it is supposed to be managing for us.
- Loading branch information
Showing
7 changed files
with
62 additions
and
62 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class Detector | ||
# PatternChecker is intended to be added to Detectors via `include Detector::PatternChecker` to make | ||
# these methods available to instances of the class | ||
module PatternChecker | ||
def term_pattern_checker(term) | ||
term_patterns.each_pair do |type, pattern| | ||
@detections[type.to_sym] = match(pattern, term) if match(pattern, term).present? | ||
end | ||
end | ||
|
||
# Note on the limitations of this implementation | ||
# We only detect the first match of each pattern, so a search of "1234-5678 5678-1234" will not return two ISSNs as | ||
# might be expected, but just "1234-5678". Using ruby's string.scan(pattern) may be worthwhile if we want to detect | ||
# all possible matches instead of just the first. That may require a larger refactor though as initial tests of doing | ||
# that change did result in unintended results so it was backed out for now. | ||
def match(pattern, term) | ||
pattern.match(term).to_s.strip | ||
end | ||
end | ||
end |
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
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