Sha256: 33dc3cabb633b300442230b6b033385545872c9d1689504bf8abeb795a70d380

Contents?: true

Size: 406 Bytes

Versions: 14

Compression:

Stored size: 406 Bytes

Contents

module LuhnChecker
  # This method implements Luhn algorythm.
  # Details about it's work may be founded here: http://en.wikipedia.org/wiki/Luhn_Algorithm
  def self.valid?(s)
    value = s.gsub(/\D/, '').reverse

    sum = i = 0

    value.each_char do |ch|
      n = ch.to_i

      n *= 2 if i.odd?

      n = 1 + (n - 10) if n >= 10

      sum += n
      i   += 1
    end

    (sum % 10).zero?
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
activevalidators-6.0.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-5.1.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-5.0.1 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-5.0.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-4.1.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-4.0.3 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-4.0.2 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-4.0.1 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-4.0.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-3.3.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-3.2.0 lib/active_validators/active_model/validations/shared/luhn_checker.rb
activevalidators-3.1.2 lib/active_model/validations/shared/luhn_checker.rb
activevalidators-3.1.1 lib/active_model/validations/shared/luhn_checker.rb
activevalidators-3.0.0 lib/active_model/validations/shared/luhn_checker.rb