Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ChileRut.full_formatted_rut #2460

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions doc/default/chile_rut.md
Expand Up @@ -19,4 +19,12 @@ Faker::ChileRut.dv #=> "k"
# check_digit is an alias for dv, for English speaking devs
Faker::ChileRut.rut #=> 30528772
Faker::ChileRut.check_digit #=> "5"

# Returns full rut
# Keyword arguments: min_rut
# Keyword arguments: fixed
Faker::ChileRut.full_rut #=> "30686957-4"
Faker::ChileRut.full_rut(min_rut: 20890156) #=> "20890156-4"
Faker::ChileRut.full_rut(min_rut: 20890156, formatted: true) #=> "20.890.156-4"
Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
```
10 changes: 7 additions & 3 deletions lib/faker/default/chile_rut.rb
Expand Up @@ -82,14 +82,18 @@ def check_digit
# Faker::ChileRut.full_rut(min_rut: 20890156) #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
#
# @faker.version 1.9.2
def full_rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 0, fixed: false)
# @faker.version next
def full_rut(legacy_min_rut = NOT_GIVEN, legacy_fixed = NOT_GIVEN, min_rut: 0, fixed: false, formatted: 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}"
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
end

attr_reader :last_rut
Expand Down
5 changes: 5 additions & 0 deletions test/faker/default/test_faker_chile_rut.rb
Expand Up @@ -24,4 +24,9 @@ def test_check_digit
assert @tester.rut(min_rut: 30_686_957, fixed: true) == 30_686_957
assert @tester.dv == '4'
end

def test_full_formatted_rut
assert @tester.full_rut(min_rut: 30_686_957, fixed: true, formatted: true).split('-')[0] == '30.686.957'
assert @tester.dv == '4'
end
end