Sha256: 9c1f990c5170b7226274dfa6daff4fc2a065723bb41808925e7ebbcd25dac489

Contents?: true

Size: 746 Bytes

Versions: 3

Compression:

Stored size: 746 Bytes

Contents

module ValidatesCpf
  module Cpf
    @@invalid_cpfs = %w{12345678909 11111111111 22222222222 33333333333 44444444444 55555555555 66666666666 77777777777 88888888888 99999999999 00000000000}

    def self.valid?(value)
      value.gsub!(/[^0-9]/, '')

      return false if @@invalid_cpfs.member?(value)

      digit = value.slice(-2, 2)
      control = ''
      if value.size == 11
        factor = 0
        2.times do |i|
          sum = 0
          9.times do |j|
            sum += value.slice(j, 1).to_i * (10 + i - j)
          end
          sum += (factor * 2) if i == 1
          factor = (sum * 10) % 11
          factor = 0 if factor == 10
          control << factor.to_s
        end
      end
      control == digit
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cpf_validation-0.3.0 lib/validates_cpf/cpf.rb
cpf_validation-0.2.0 lib/validates_cpf/cpf.rb
cpf_validation-0.1.0 lib/validates_cpf/cpf.rb