-
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.
- Loading branch information
1 parent
88e3dd1
commit eef1195
Showing
4 changed files
with
74 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Faker::CNPJ | ||
|
||
Brazilian CNPJ valid random generator. | ||
|
||
```ruby | ||
Faker::CNPJ.number | ||
#=> "00000000000" | ||
|
||
Faker::CNPJ.formatted | ||
#=> "000.000.000-00" | ||
``` |
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,45 @@ | ||
# frozen_string_literal: true | ||
|
||
module Faker | ||
class CNPJ < Faker::Base | ||
attr_accessor :digits | ||
|
||
def initialize | ||
@digits = Array.new(8).map { Faker::Config.random.rand(0..9) }.concat([0, 0, 0, 1]) | ||
end | ||
|
||
def number | ||
@number ||= [*digits, first_digit, last_digit].join | ||
end | ||
|
||
def formatted | ||
@formatted ||= '%s.%s.%s/%s-%s' % number.scan(/(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})/).flatten | ||
end | ||
|
||
private | ||
|
||
def first_digit | ||
values = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | ||
sum = digits.map.with_index { |v, i| v * values[i] }.inject(&:+) | ||
result = sum % 11 | ||
result < 2 ? 0 : 11 - result | ||
end | ||
|
||
def last_digit | ||
values = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2] | ||
sum = [*digits, first_digit].map.with_index { |v, i| v * values[i] }.inject(&:+) | ||
result = sum % 11 | ||
result < 2 ? 0 : 11 - result | ||
end | ||
|
||
class << self | ||
def number | ||
new.number | ||
end | ||
|
||
def formatted | ||
new.formatted | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../test_helper' | ||
|
||
class TestFakerCNPJ < Test::Unit::TestCase | ||
def setup | ||
@tester = Faker::CNPJ.new | ||
end | ||
|
||
def test_number | ||
assert_equal(14, @tester.number.length) | ||
end | ||
|
||
def test_formatted | ||
assert @tester.formatted.match(/\d{2}\.\d{3}\.\d{3}\/\d{4}-\d{2}/) | ||
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