-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added ChileRUT class for generating Modulo 11 chilean RUTs and check digits. * Improved ChileRUT docs. * Update chile_rut.rb * Update test_determinism.rb * Update test_faker_chile_rut.rb * Update CHANGELOG.md * Update chile_rut.md * Update README.md * Update chile_rut.md * Update test_faker_chile_rut.rb
- Loading branch information
Showing
6 changed files
with
95 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Faker::ChileRut | ||
|
||
```ruby | ||
Faker::ChileRut.full_rut #=> "30686957-4" | ||
|
||
# Returns rut between 1 (default param) and 99999999 | ||
Faker::ChileRut.rut #=> 11235813 | ||
|
||
# Returns rut between passed minimum rut and 99999999 | ||
Faker::ChileRut.rut(20_890_156) #=> 31853211 | ||
|
||
# Every call to rut or full_rut generates a new random rut, so last_rut and dv | ||
# allows you to get the separated parts of the full rut without losing the already generated rut | ||
Faker::ChileRut.rut #=> 23567131 | ||
Faker::ChileRut.last_rut #=> 23567131 | ||
Faker::ChileRut.dv #=> "k" | ||
|
||
# check_digit is an alias for dv, for English speaking devs | ||
Faker::ChileRut.rut #=> 30528772 | ||
Faker::ChileRut.check_digit #=> "5" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
module Faker | ||
class ChileRut < Base | ||
class << self | ||
@last_rut = nil | ||
|
||
# Fixed param added for testing a specific RUT and check digit combination. | ||
def rut(min_rut = 1, fixed = false) | ||
@last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999) | ||
end | ||
|
||
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 | ||
|
||
# Alias for english speaking devs. | ||
def check_digit | ||
dv | ||
end | ||
|
||
def full_rut(min_rut = 0, fixed = false) | ||
"#{rut(min_rut, fixed)}-#{dv}" | ||
end | ||
|
||
attr_reader :last_rut | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
require File.expand_path(File.dirname(__FILE__) + '/test_helper.rb') | ||
|
||
class TestChileRut < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::ChileRut | ||
end | ||
|
||
def test_full_rut | ||
assert @tester.full_rut(6, true) == '6-k' | ||
assert @tester.full_rut(30_686_957, true) == '30686957-4' | ||
end | ||
|
||
def test_rut_length | ||
assert !@tester.rut.to_s.empty? | ||
assert @tester.rut.to_s.length <= 8 | ||
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. | ||
def test_check_digit | ||
assert @tester.rut(30_686_957, true) == 30_686_957 | ||
assert @tester.dv == '4' | ||
end | ||
end |