diff --git a/CHANGELOG.md b/CHANGELOG.md index 522a41d42a..6ddfdb4173 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [PR #372](https://github.com/stympy/faker/pull/372) Add test_password_could_achieve_max_length [@oleksii-ti](https://github.com/oleksii-ti) ### Feature Request +- [PR #893](https://github.com/stympy/faker/pull/893) Add Faker::ChileRut [@oxfist](https://github.com/oxfist) - [PR #1315](https://github.com/stympy/faker/pull/1315) Add Faker::GratefulDead [@wileybaba](https://github.com/wileybaba) - [PR #1314](https://github.com/stympy/faker/pull/1314) Add Faker::SouthPark [@saurabhudaniya200](https://github.com/saurabhudaniya200) - [PR #1313](https://github.com/stympy/faker/pull/1313) Add Faker::Restaurant [@dwhitlow](https://github.com/dwhitlow) diff --git a/README.md b/README.md index deb5f0707c..525954015c 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ Contents - [Faker::Business](doc/business.md) - [Faker::Cannabis](doc/cannabis.md) - [Faker::Cat](doc/cat.md) + - [Faker::ChileRut](doc/chile_rut.md) - [Faker::ChuckNorris](doc/chuck_norris.md) - [Faker::Code](doc/code.md) - [Faker::Coffee](doc/coffee.md) diff --git a/doc/chile_rut.md b/doc/chile_rut.md new file mode 100644 index 0000000000..9e96d54744 --- /dev/null +++ b/doc/chile_rut.md @@ -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" +``` diff --git a/lib/faker/chile_rut.rb b/lib/faker/chile_rut.rb new file mode 100644 index 0000000000..d8f6eccd1b --- /dev/null +++ b/lib/faker/chile_rut.rb @@ -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 diff --git a/test/test_determinism.rb b/test/test_determinism.rb index a216ba7d0d..6f8c47fa89 100644 --- a/test/test_determinism.rb +++ b/test/test_determinism.rb @@ -45,7 +45,7 @@ def all_methods def subclasses Faker.constants.delete_if do |subclass| - %i[Base Bank Char Config Date Internet Time VERSION].include?(subclass) + %i[Base Bank Char ChileRut Config Date Internet Time VERSION].include?(subclass) end.sort end diff --git a/test/test_faker_chile_rut.rb b/test/test_faker_chile_rut.rb new file mode 100644 index 0000000000..c9b54a9200 --- /dev/null +++ b/test/test_faker_chile_rut.rb @@ -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