5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/kangal/tckn.rb', line 5
def validate_each(record, attribute, value)
return if options[:allow_nil] && value.nil?
return if options[:allow_blank] && value.blank?
digits = value[0..-3].each_char.map(&:to_i).each_with_index
first, last =
digits.reduce([0, 0]) do |memo, (digit, idx)|
add = digit * (idx.even? ? 7 : -1)
[
(memo.first + add) % 10,
(memo.last + digit + add) % 10
]
end
valid = (!invalid_value?(value, 11) && value[-2] == first.to_s && value[-1] == last.to_s)
record.errors.add attribute, (options[:message] || I18n.t(:invalid, :scope => 'kangal.validations.tckn')) unless valid
end
|