Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
module Jancode | ||
class GTIN13 < GTIN | ||
def check_digit | ||
code = [@company_prefix, @item_code].join.chars.map{|n| n.to_i} | ||
even = (1..(code.size - 1)).step(2).inject(0){|i,n| i += code[n] * 3} | ||
odd = (0..(code.size - 1)).step(2).inject(0){|i,n| i += code[n]} | ||
val = (even + odd).to_s[-1].to_i | ||
val == 0 ? 0 : 10 - val | ||
def validation | ||
valid_company_prefix @company_prefix | ||
valid_item_code @item_code | ||
raise "Is not 12 characters Item Code and GS1 Company Prefix to suit" if code_array.size != 12 | ||
end | ||
|
||
private | ||
def valid_company_prefix(company_prefix) | ||
raise "GS1 Company Prefix is not String" unless company_prefix.is_a? String | ||
raise "GS1 Company Prefix is not 7 or 9 characters" if company_prefix.size != 7 && company_prefix.size != 9 | ||
end | ||
|
||
def valid_item_code(item_code) | ||
raise "Item Code is not String" unless item_code.is_a? String | ||
raise "Item Code is not 5 or 3 characters" if item_code.size != 3 && item_code.size != 5 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
module Jancode | ||
class GTIN8 < GTIN | ||
def check_digit | ||
code = [@company_prefix, @item_code].join.chars.map{|n| n.to_i} | ||
even = (0..(code.size - 1)).step(2).inject(0){|i,n| i += code[n] * 3} | ||
odd = (1..(code.size - 1)).step(2).inject(0){|i,n| i += code[n]} | ||
val = (even + odd).to_s[-1].to_i | ||
val == 0 ? 0 : 10 - val | ||
def validation | ||
valid_company_prefix @company_prefix | ||
valid_item_code @item_code | ||
raise "Is not 7 characters Item Code and GS1 Company Prefix to suit" if code_array.size != 7 | ||
end | ||
|
||
private | ||
def valid_company_prefix(company_prefix) | ||
raise "GS1 Company Prefix is not String" unless company_prefix.is_a? String | ||
raise "GS1 Company Prefix is not 6 characters" if company_prefix.size != 6 | ||
end | ||
|
||
def valid_item_code(item_code) | ||
raise "Item Code is not String" unless item_code.is_a? String | ||
raise "Item Code is not 1 characters" if item_code.size != 1 | ||
end | ||
end | ||
end |