Sha256: 940db5a81f92e164e424f8f7ad406ecfae9d0b195940bd03e8e01b6fe15b20e2

Contents?: true

Size: 800 Bytes

Versions: 6

Compression:

Stored size: 800 Bytes

Contents

# frozen_string_literal: true

require 'ffaker/identification_es'

module FFaker
  module IdentificationESCL
    extend IdentificationES
    extend ModuleUtils
    extend self

    # RUT is the Chilean ID, followed by format:  XX.XXX.XXX - Y
    # http://es.wikipedia.org/wiki/Rol_%C3%9Anico_Tributario
    #
    # The last Y is a modulo 11 validation code. In the case the result is 10, it will be
    # replaced by a 'K' character
    def rut
      # Rut is gonna be between 1.000.000 and 24.999.999
      n = rand(1_000_000...25_000_000)
      "#{n}-#{dv(n)}"
    end

    private

    def dv(rut)
      total = rut.to_s.rjust(8, '0').split(//).zip(%w[3 2 7 6 5 4 3 2]).sum do |a, b|
        a.to_i * b.to_i
      end
      (11 - total % 11).to_s.gsub(/10/, 'k').gsub(/11/, '0')
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ffaker-2.17.0 lib/ffaker/identification_es_cl.rb
ffaker-2.16.0 lib/ffaker/identification_es_cl.rb
ffaker-2.15.0 lib/ffaker/identification_es_cl.rb
ffaker-2.14.0 lib/ffaker/identification_es_cl.rb
ffaker-2.13.0 lib/ffaker/identification_es_cl.rb
ffaker-2.12.0 lib/ffaker/identification_es_cl.rb