Skip to content

Commit

Permalink
Add Faker::ChileRut (#893)
Browse files Browse the repository at this point in the history
* 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
oxfist authored and vbrazo committed Jul 22, 2018
1 parent 8376005 commit 12dbcda
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions doc/chile_rut.md
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"
```
44 changes: 44 additions & 0 deletions lib/faker/chile_rut.rb
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
2 changes: 1 addition & 1 deletion test/test_determinism.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 27 additions & 0 deletions test/test_faker_chile_rut.rb
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

0 comments on commit 12dbcda

Please sign in to comment.