Skip to content

Commit

Permalink
Add max_rut option to Faker::ChileRut.rut (#2778)
Browse files Browse the repository at this point in the history
* Allow max_rut option on ChileRut
* ChileRut: format_rut method
* ChileRut: test formatted min/max RUT
  • Loading branch information
hacktivista committed Jul 15, 2023
1 parent 576ca87 commit 56fc3c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
40 changes: 23 additions & 17 deletions lib/faker/default/chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class << self
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [Number]
#
# @example
# Faker::ChileRut.rut #=> 11235813
# Faker::ChileRut.rut(min_rut: 20890156) #=> 31853211
# Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156
# Faker::ChileRut.rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> 21853211
# Faker::ChileRut.rut(min_rut: 20_890_156, fixed: true) #=> 20890156
#
# @faker.version 1.9.2
def rut(min_rut: 1, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
# @faker.version next
def rut(min_rut: 1, max_rut: 99_999_999, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, max_rut)
end

##
Expand Down Expand Up @@ -68,25 +69,30 @@ def check_digit
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits) with a dv (digito verificador, check-digit).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [String]
#
# @example
# 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"
# Faker::ChileRut.full_rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> "20686957-4"
# Faker::ChileRut.full_rut(min_rut: 30_686_957, fixed: true) #=> "30686957-4"
#
# @faker.version next
def full_rut(min_rut: 0, fixed: false, formatted: false)
if formatted
"#{rut(min_rut: min_rut, fixed: fixed).to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse}-#{dv}"
else
"#{rut(min_rut: min_rut, fixed: fixed)}-#{dv}"
end
def full_rut(min_rut: 1, max_rut: 99_999_999, fixed: false, formatted: false)
this_rut = rut(min_rut: min_rut, max_rut: max_rut, fixed: fixed)
this_rut = format_rut(this_rut) if formatted
"#{this_rut}-#{dv}"
end

attr_reader :last_rut

private

def format_rut(rut)
rut.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse
end
end
end
end
6 changes: 6 additions & 0 deletions test/faker/default/test_faker_chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def setup

def test_full_rut
assert_equal('6-k', @tester.full_rut(min_rut: 6, fixed: true))
assert_equal('8-6', @tester.full_rut(min_rut: 8, max_rut: 8))
assert_equal('11.111.111-1', @tester.full_rut(min_rut: 11_111_111, max_rut: 11_111_111, formatted: true))
assert_equal('30686957-4', @tester.full_rut(min_rut: 30_686_957, fixed: true))
end

Expand All @@ -17,6 +19,10 @@ def test_rut_length
assert @tester.rut.to_s.length <= 8
end

def test_rut_min_max
assert_equal(7, @tester.rut(min_rut: 7, max_rut: 7))
end

# We need to set specific rut before testing the check digit
# since the whole idea of the method revolves around calculating
# the check digit for that specific rut.
Expand Down

0 comments on commit 56fc3c8

Please sign in to comment.