Fix: re-expose GTIN#check_digit as public method - #28
Conversation
PR marketplacer#3 changed the internals of BarcodeValidation::GTIN to support different GTIN formats and to allow conversion between them. While this is a _very_ nice feature, it unfortunately made the previously public method `GTIN#check_digit` (which was added via BarcodeValidation::Mixin) private. In v2.2.0 we relied on this in our tests to fix the check digit of randomly generated barcodes. v2.3.0 makes both `check_digit` and `expected_check_digit` private, so it does not publicly expose any of the tools we'd need for our purposes. The simplest fix seemed to make `check_digit` public, which restores the API that was available in v2.2.0. I've added a spec that calls the method and its validation as a means to ensure it remains public. Ref: marketplacer#3
|
Thanks for the PR! I'm bumping into this as well. @marketplacer if you find the time to merge this, very welcome :) |
|
Making the check digit private was a conscious decision to treat it as an internal implementation detail of the GTIN objects' def valid?
valid_length == length && check_digit.valid?
endCould you walk us through how you're using the check digit directly? |
|
I can see how hiding implementation details is a good reason to shrink your public API. In this case, though, check digits are part of the barcode domain and hiding it means you're hiding a useful domain object. In our case we relied on it, so we did not have to re-implement it ourselves (which seems wasteful). In addition to reading and validating existing barcodes, we have two reasons for generating them:
One example of how we use the public check digit would be this: # Takes a barcode string and if needed corrects the check digit to make it valid.
#
# @param [String] bc A barcode that may or may not have a valid check digit.
# @return [String] A barcode that has a valid check digit.
def repair_check_digit(bc)
bc = bc.dup
bc[-1] = BarcodeValidation.scan(bc).check_digit.expected.to_s
bc
end |
|
@Narnach Nice one, thanks for the explanation, makes perfect sense. |
|
Ah great! Can we merge and do a release? @beet :) |
|
Just released Release v2.5.0 · marketplacer/barcodevalidation |
|
Nice, thank you @beet! |
Restore
BarcodeValiation::GTIN#check_digitas public method (broken in v2.3.0).PR #3 changed the internals of BarcodeValidation::GTIN to support different GTIN formats and to allow conversion between them. While this is a very nice feature, it unfortunately made the previously public method
GTIN#check_digit(which was added via BarcodeValidation::Mixin) private.In v2.2.0 we relied on this in our tests to fix the check digit of randomly generated barcodes. v2.3.0 makes both
check_digitandexpected_check_digitprivate, so it does not publicly expose any of the tools we'd need for our purposes.The simplest fix seemed to make
check_digitpublic, which restores the API that was available in v2.2.0. I've added a spec that calls the method and its validation as a means to ensure it remains public.The readme still references
check_digitas a public method, which reinforces that it was probably made private by accident.(Note that I read the commit template, but it seems copy/pasted from another project that has interface tests and authentication. I did try to keep the first paragraph of my PR suitable for a changelog, because I'm not sure if that is relevant or not.)