Class: Faker::ChileRut

Inherits:
Base
  • Object
show all
Defined in:
lib/faker/default/chile_rut.rb

Constant Summary

Constants inherited from Base

Base::LLetters, Base::Letters, Base::NOT_GIVEN, Base::Numbers, Base::ULetters

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Base

bothify, disable_enforce_available_locales, fetch, fetch_all, flexible, letterify, method_missing, numerify, parse, rand, rand_in_range, regexify, resolve, respond_to_missing?, sample, shuffle, translate, unique, with_locale

Class Attribute Details

.last_rutObject (readonly)

Returns the value of attribute last_rut


94
95
96
# File 'lib/faker/default/chile_rut.rb', line 94

def last_rut
  @last_rut
end

Class Method Details

.check_digitString

Produces a random Chilean digito verificador (check-digit). Alias for english speaking devs.

Examples:

Faker::ChileRut.check_digit #=> "5"

Returns:

  • (String)

Available since:

  • 1.9.2


68
69
70
# File 'lib/faker/default/chile_rut.rb', line 68

def check_digit
  dv
end

.dvString

Produces a random Chilean digito verificador (check-digit).

Examples:

Faker::ChileRut.dv #=> "k"

Returns:

  • (String)

Available since:

  • 1.9.2


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/faker/default/chile_rut.rb', line 39

def dv
  split_reversed_rut = @last_rut.to_s.reverse.split('')
  seq = [2, 3, 4, 5, 6, 7]
  i = 0
  digit_sum = split_reversed_rut.reduce(0) do |sum, n|
    partial_result = sum.to_i + (n.to_i * seq[i])
    i = i == 5 ? 0 : i + 1
    partial_result
  end
  partial_check_digit = 11 - (digit_sum % 11)
  if partial_check_digit == 11
    '0'
  elsif partial_check_digit == 10
    'k'
  else
    partial_check_digit.to_s
  end
end

.full_rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 0, fixed: false) ⇒ String

Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits) with a dv (digito verificador, check-digit).

Examples:

Faker::ChileRut.full_rut #=> "30686957-4"
Faker::ChileRut.full_rut(min_rut: 20890156) #=> "30686957-4"
Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"

Parameters:

  • min_rut (Integer) (defaults to: 0)

    Specifies the minimum value of the rut.

  • fixed (Boolean) (defaults to: false)

    Determines if the rut is fixed (returns the min_rut value).

Returns:

  • (String)

Available since:

  • 1.9.2


85
86
87
88
89
90
91
92
# File 'lib/faker/default/chile_rut.rb', line 85

def full_rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 0, fixed: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :min_rut if legacy_min_rut != NOT_GIVEN
    keywords << :fixed if legacy_fixed != NOT_GIVEN
  end

  "#{rut(min_rut: min_rut, fixed: fixed)}-#{dv}"
end

.rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 1, fixed: false) ⇒ Number

Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits).

Examples:

Faker::ChileRut.rut #=> 11235813
Faker::ChileRut.rut(min_rut: 20890156) #=> 31853211
Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156

Parameters:

  • min_rut (Integer) (defaults to: 1)

    Specifies the minimum value of the rut.

  • fixed (Boolean) (defaults to: false)

    Determines if the rut is fixed (returns the min_rut value).

Returns:

Available since:

  • 1.9.2


21
22
23
24
25
26
27
28
# File 'lib/faker/default/chile_rut.rb', line 21

def rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 1, fixed: false)
  warn_for_deprecated_arguments do |keywords|
    keywords << :min_rut if legacy_min_rut != NOT_GIVEN
    keywords << :fixed if legacy_fixed != NOT_GIVEN
  end

  @last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
end