Skip to content

Commit

Permalink
Added Checksum::AlgorythmHelper#sum_of_figues_for_pt_si
Browse files Browse the repository at this point in the history
  • Loading branch information
yolk committed Aug 1, 2013
1 parent b8d0168 commit e1572dd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/valvat/checksum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lib/valvat/checksum/pt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/valvat/checksum/si.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1572dd

Please sign in to comment.