Class: BankCard::Validations::LuhnValidator
- Inherits:
-
ActiveModel::EachValidator
- Object
- ActiveModel::EachValidator
- BankCard::Validations::LuhnValidator
- Defined in:
- lib/bank_card/validations/luhn_validator.rb
Instance Method Summary collapse
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/bank_card/validations/luhn_validator.rb', line 4 def validate_each(record, attribute, value) relative_number = {'0' => 0, '1' => 2, '2' => 4, '3' => 6, '4' => 8, '5' => 1, '6' => 3, '7' => 5, '8' => 7, '9' => 9} sum = 0 value.to_s.reverse.split("").each_with_index do |n, i| sum += (i % 2 == 0) ? n.to_i : relative_number[n] end unless sum % 10 == 0 record.errors.add(attribute, :invalid, ) end end |