From e1572dda44b29c77077184b561904448b37c154d Mon Sep 17 00:00:00 2001 From: Sebastian Date: Thu, 1 Aug 2013 03:26:55 +0200 Subject: [PATCH] Added Checksum::AlgorythmHelper#sum_of_figues_for_pt_si --- lib/valvat/checksum.rb | 6 ++++++ lib/valvat/checksum/pt.rb | 6 +++--- lib/valvat/checksum/si.rb | 6 +++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/valvat/checksum.rb b/lib/valvat/checksum.rb index e7ce014..6766beb 100644 --- a/lib/valvat/checksum.rb +++ b/lib/valvat/checksum.rb @@ -64,6 +64,12 @@ def sum_of_figures(reverse_ints=false) (fig*(i.modulo(2) == 0 ? ints[0] : ints[1])).to_s.split("").inject(0) { |sum, n| sum + n.to_i } end.inject(:+) end + + def sum_of_figues_for_pt_si + 11 - figures.reverse.each_with_index.map do |fig, i| + fig*(i+2) + end.inject(:+).modulo(11) + end end end diff --git a/lib/valvat/checksum/pt.rb b/lib/valvat/checksum/pt.rb index 35dbb9e..c19ced5 100644 --- a/lib/valvat/checksum/pt.rb +++ b/lib/valvat/checksum/pt.rb @@ -3,10 +3,10 @@ class Valvat module Checksum class PT < Base + include AlgorythmHelper + def check_digit - chk = 11 - figures.reverse.each_with_index.map do |fig, i| - fig*(i+2) - end.inject(:+).modulo(11) + chk = sum_of_figues_for_pt_si chk > 9 ? 0 : chk end end diff --git a/lib/valvat/checksum/si.rb b/lib/valvat/checksum/si.rb index 4a3f061..755e18d 100644 --- a/lib/valvat/checksum/si.rb +++ b/lib/valvat/checksum/si.rb @@ -3,15 +3,15 @@ class Valvat module Checksum class SI < Base + include AlgorythmHelper + def validate figures_str.to_i > 999999 && super end def check_digit - chk = 11 - figures.reverse.each_with_index.map do |fig, i| - fig*(i+2) - end.inject(:+).modulo(11) + chk = sum_of_figues_for_pt_si chk == 1 ? 0 : chk end end