Class: Faker::ChileRut

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

Constant Summary

Constants inherited from Base

Base::Letters, 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


41
42
43
# File 'lib/faker/default/chile_rut.rb', line 41

def last_rut
  @last_rut
end

Class Method Details

.check_digitObject

Alias for english speaking devs.


33
34
35
# File 'lib/faker/default/chile_rut.rb', line 33

def check_digit
  dv
end

.dvObject


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/faker/default/chile_rut.rb', line 13

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(min_rut = 0, fixed = false) ⇒ Object


37
38
39
# File 'lib/faker/default/chile_rut.rb', line 37

def full_rut(min_rut = 0, fixed = false)
  "#{rut(min_rut, fixed)}-#{dv}"
end

.rut(min_rut = 1, fixed = false) ⇒ Object

Fixed param added for testing a specific RUT and check digit combination.


9
10
11
# File 'lib/faker/default/chile_rut.rb', line 9

def rut(min_rut = 1, fixed = false)
  @last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
end