Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom GTINs #29

Merged
merged 7 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor: GTIN::Base.prioritize_before does not touch GTIN internals
Manipulating `GTIN.prioritized_gtin_classes` now only happens through
helper methods on GTIN, theoretically allowing the list itself to become
private.
  • Loading branch information
Narnach committed Sep 20, 2022
commit a3241440e18e9578c3493ff7b225101d3c0c0ae8
17 changes: 16 additions & 1 deletion lib/barcodevalidation/gtin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def prioritized_gtin_classes

# Adds the provided class to the back of the list of prioritized GTIN classes.
def append_gtin_class(gtin_class)
return if prioritized_gtin_classes.include?(gtin_class)
return if gtin_class?(gtin_class)

prioritized_gtin_classes.push(gtin_class)
nil
Expand All @@ -34,6 +34,21 @@ def remove_gtin_class(gtin_class)
nil
end

# Is this a registered prioritized GTIN class?
# @return [true, false]
def gtin_class?(gtin_class)
prioritized_gtin_classes.include?(gtin_class)
end

# @param [Class] high_priority_class The higher priority GTIN class you want to move before the low priority class
# @param [Class] low_priority_class The low priority GTIN class that the high priority one is moved before
def reprioritize_before(high_priority_class, low_priority_class)
low_priority_index = prioritized_gtin_classes.index(low_priority_class)
remove_gtin_class(high_priority_class)
prioritized_gtin_classes.insert(low_priority_index, high_priority_class)
nil
end

private

def class_for_input(input)
Expand Down
8 changes: 2 additions & 6 deletions lib/barcodevalidation/gtin/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,9 @@ def self.inherited(subclass)

# Ensure this class is earlier in the GTIN classes list than +other_gtin_class+ and thus will get asked earlier if it handles a GTIN.
def self.prioritize_before(other_gtin_class)
other_index = BarcodeValidation::GTIN.prioritized_gtin_classes.index(other_gtin_class)
raise ArgumentError, "The class you want to prioritize before is not a registered prioritized GTIN class." if other_index.nil?
raise ArgumentError, "The class you want to prioritize before is not a registered prioritized GTIN class." unless GTIN.gtin_class?(other_gtin_class)

BarcodeValidation::GTIN.remove_gtin_class(self)
BarcodeValidation::GTIN.prioritized_gtin_classes.insert(other_index, self)

nil
GTIN.reprioritize_before(self, other_gtin_class)
end

# This class is abstract and should not be included in the list of GTIN classes that actually implement a GTIN.
Expand Down