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 Faker::ChileRut #893

Merged
merged 11 commits into from
Jul 22, 2018
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Contents
- [Faker::Boolean](doc/boolean.md)
- [Faker::Business](doc/business.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 @@
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

def last_RUT
@@last_RUT
end
end
end
end
26 changes: 26 additions & 0 deletions test/test_faker_chile_rut.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.length > 0
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) == 30686957
assert @tester.dv == "4"
end
end