File: identification_ec.rb

package info (click to toggle)
ruby-ffaker 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,776 kB
  • sloc: ruby: 12,788; makefile: 8; sh: 1
file content (39 lines) | stat: -rw-r--r-- 857 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

module FFaker
  module IdentificationEC
    extend ModuleUtils
    extend self

    # https://medium.com/@bryansuarez/c%C3%B3mo-validar-c%C3%A9dula-y-ruc-en-ecuador-b62c5666186f
    # Cedula de Identificacion
    def ci
      first_digits = ci_digits
      "#{first_digits}#{last_digit(first_digits)}"
    end

    private

    def ci_digits
      "#{FFaker.numerify('##')}#{rand(0..5)}#{FFaker.numerify('######')}"
    end

    def last_digit(digits)
      mod = digits_sum(digits) % 10
      10 - mod if mod.positive?
    end

    def digits_sum(digits)
      sum = 0
      digits.chars.each_with_index do |char, index|
        if index.even?
          multiple = char.to_i * 2
          sum += multiple > 9 ? multiple - 9 : multiple
        else
          sum += char.to_i
        end
      end
      sum
    end
  end
end