Sha256: c691771580c634a559a72d6b09d917f7a261c6ef2d93cd1c345f5a17f2af0b5d
Contents?: true
Size: 824 Bytes
Versions: 3
Compression:
Stored size: 824 Bytes
Contents
require 'jan/code' module Jan class CodeBody < String # @param code_body [String] def initialize(code_body) @code_body = code_body super(code_body) end # @return [boolean] def valid? @code_body.match?(/\A(\d{12}|\d{7})\z/) end # @return [Jan::CheckDigit] def calculate_check_digit # http://www.dsri.jp/jan/check_digit.html even_position_digits, odd_position_digits = @code_body.reverse.each_char.partition.with_index(2) {|_chr, idx| idx.even? } sum = even_position_digits.map{|d| Integer(d) }.sum * 3 + odd_position_digits.map{|d| Integer(d) }.sum digit = (10 - sum % 10).to_s[-1] Jan::CheckDigit.new(digit) end # @return [Jan::Code] def generate_code Jan::Code.new(@code_body + calculate_check_digit) end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
jan-0.2.4 | lib/jan/code_body.rb |
jan-0.2.3 | lib/jan/code_body.rb |
jan-0.2.2 | lib/jan/code_body.rb |