Skip to content

Commit

Permalink
Merge branch 'main' into add-html-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
ruban-thilak authored Jul 22, 2023
2 parents 845a12f + 0839df2 commit 9240e0b
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ gemspec
gem 'minitest', '5.18.1'
gem 'pry', '0.14.2'
gem 'rake', '13.0.6'
gem 'rubocop', '1.54.1'
gem 'rubocop', '1.54.2'
gem 'rubocop-minitest', '0.31.0'
gem 'rubocop-rake', '0.6.0'
gem 'simplecov', '0.22.0'
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ GEM
rake (13.0.6)
regexp_parser (2.8.1)
rexml (3.2.5)
rubocop (1.54.1)
rubocop (1.54.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
Expand Down Expand Up @@ -68,7 +68,7 @@ DEPENDENCIES
minitest (= 5.18.1)
pry (= 0.14.2)
rake (= 13.0.6)
rubocop (= 1.54.1)
rubocop (= 1.54.2)
rubocop-minitest (= 0.31.0)
rubocop-rake (= 0.6.0)
simplecov (= 0.22.0)
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,7 @@ Faker::Config.locale = 'es'
Faker::Config.locale = :es
```

Note: Overriding the default locale might not be thread-safe. See [Locale setting can be ignored #2563](https://github.com/faker-ruby/faker/issues/2563) for more details.

To override Faker's locales,
To override Faker's locales, and set it on threaded server environments
check out the [locales README](lib/locales/README.md).

### Minitest and Faker >= 2.22
Expand Down
6 changes: 5 additions & 1 deletion lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@

module Faker
module Config
@default_locale = nil

class << self
attr_writer :default_locale

def locale=(new_locale)
Thread.current[:faker_config_locale] = new_locale
end

def locale
# Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
Thread.current[:faker_config_locale] || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
Thread.current[:faker_config_locale] || @default_locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
end

def own_locale
Expand Down
40 changes: 23 additions & 17 deletions lib/faker/default/chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ class << self
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [Number]
#
# @example
# Faker::ChileRut.rut #=> 11235813
# Faker::ChileRut.rut(min_rut: 20890156) #=> 31853211
# Faker::ChileRut.rut(min_rut: 20890156, fixed: true) #=> 20890156
# Faker::ChileRut.rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> 21853211
# Faker::ChileRut.rut(min_rut: 20_890_156, fixed: true) #=> 20890156
#
# @faker.version 1.9.2
def rut(min_rut: 1, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, 99_999_999)
# @faker.version next
def rut(min_rut: 1, max_rut: 99_999_999, fixed: false)
@last_rut = fixed ? min_rut : rand_in_range(min_rut, max_rut)
end

##
Expand Down Expand Up @@ -68,25 +69,30 @@ def check_digit
##
# Produces a random Chilean RUT (Rol Unico Tributario, ID with 8 digits) with a dv (digito verificador, check-digit).
#
# @param min_rut [Integer] Specifies the minimum value of the rut.
# @param fixed [Boolean] Determines if the rut is fixed (returns the min_rut value).
# @param min_rut [Integer] Specifies the minimum value of the RUT.
# @param max_rut [Integer] Specifies the maximum value of the RUT.
# @param fixed [Boolean] Determines if the RUT is fixed (returns the min_rut value).
# @return [String]
#
# @example
# Faker::ChileRut.full_rut #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 20890156) #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 30686957, fixed: true) #=> "30686957-4"
# Faker::ChileRut.full_rut(min_rut: 10_000_000, max_rut: 30_000_000) #=> "20686957-4"
# Faker::ChileRut.full_rut(min_rut: 30_686_957, fixed: true) #=> "30686957-4"
#
# @faker.version next
def full_rut(min_rut: 0, fixed: false, formatted: false)
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
def full_rut(min_rut: 1, max_rut: 99_999_999, fixed: false, formatted: false)
this_rut = rut(min_rut: min_rut, max_rut: max_rut, fixed: fixed)
this_rut = format_rut(this_rut) if formatted
"#{this_rut}-#{dv}"
end

attr_reader :last_rut

private

def format_rut(rut)
rut.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1.').reverse
end
end
end
end
20 changes: 18 additions & 2 deletions lib/locales/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Here's how to set it:
Faker::Config.locale = 'zh-CN'
```

It works so that once the Faker locale is set to a different location, the translate method will check that .yml file for an equivalent and use that data. If it doesn't exist, it defaults back to English. It uses the "i18n" gem to do this.
It works so that once the Faker locale is set to a different location, the translate method will check that `.yml` file for an equivalent and use that data. If it doesn't exist, it defaults back to English. It uses the [I18n](https://github.com/ruby-i18n/i18n) gem to do this.

Using Chinese as an example, when the locale is set to Chinese and you attempt to call for hipster ipsem (which doesn't exist at the time of this writing), you will get English back. It checks the "zh-CH.yml" file, does not find "hipster" and then checks the "en.yml" file and returns a word from that array.

Expand All @@ -18,7 +18,9 @@ Faker::Config.locale = 'zh-CN'
Faker::Hipster.word #=> "kogi"
```

In order to update a locale with more translation features, simply add a new field to the .yml file that corresponds to an existing piece of functionality in the "en.yml" file. In this example, that would mean providing Chinese hipster words.
## How to update a locale with more translations

T update a locale with more translation features, simply add a new field to the .yml file that corresponds to an existing piece of functionality in the "en.yml" file. In this example, that would mean providing Chinese hipster words.

```yaml
# /lib/locales/zh-CN.yml
Expand All @@ -38,3 +40,17 @@ In our hypothetical example here, one would add something like this to the "test
```ruby
assert Faker::Hipster.word.is_a? String
```

## How to set the default locale for in threaded server environments

If you want to modify the default locale that will be used in new threads, set it in your configuration:

```ruby
Faker::Config.default_locale = :pt
```

In threaded server environments, e.g., Puma, locale per thread can be set as the following:

```ruby
Faker::Config.locale = :es
```
3 changes: 2 additions & 1 deletion lib/locales/fr/name.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fr:
- "#{female_first_name}"
- "#{male_first_name}"
last_name: [Adam, Andre, André, Antoine, Arnaud, Aubert, Aubry, Bailly, Barbier, Baron, Barre, Barthelemy, Benard, Benoit, Berger, Bernard, Bertin, Bertrand, Besson, Blanc, Blanchard, Bonnet, Boucher, Bouchet, Boulanger, Bourgeois, Bouvier, Boyer, Breton, Brun, Brunet, Carlier, Caron, Carpentier, Carre, Charles, Charpentier, Chauvin, Chevalier, Chevallier, Clement, Clément, Colin, Collet, Collin, Cordier, Cousin, Da Silva, Da silva, Daniel, David, Delaunay, Denis, Deschamps, Dubois, Dufour, Dumas, Dumont, Dupont, Dupuis, Dupuy, Durand, Duval, Dvnis, Etienne, Fabre, Faure, Fernandez, Fleury, Fontaine, Fournier, Francois, Gaillard, Garcia, Garnier, Gauthier, Gautier, Gay, Gerard, Gérard, Germain, Gilbert, Gillet, Girard, Giraud, Gonzalez, Grondin, Guerin, Guérin, Guichard, Guillaume, Guillot, Guyot, Hamon, Henry, Herve, Hoarau, Hubert, Huet, Humbert, Jacob, Jacquet, Jean, Joly, Julien, Klein, Lacroix, Laine, Lambert, Lamy, Langlois, Laporte, Laurent, Le Gall, Le Goff, Le Roux, Le gall, Le roux, Leblanc, Lebrun, Leclerc, Leclercq, Lecomte, Lefevre, Lefèvre, Leger, Legrand, Lejeune, Lemaire, Lemaitre, Lemoine, Leroux, Leroy, Leveque, Lévêque, Lopez, Lopéz, Louis, Lucas, Maillard, Mallet, Marchal, Marchand, Marechal, Marie, Martin, Martinez, Marty, Masson, Mathieu, Menard, Ménard, Mercier, Meunier, Méunier, Meyer, Michaud, Michel, Millet, Monnier, Moreau, Morel, Morin, Moulin, Muller, Nguyen, Nicolas, Noel, Noël, Olivier, Paris, Pasquier, Paul, Payet, Pelletier, Perez, Perret, Perrier, Perrin, Perrot, Petit, Philippe, Picard, Pichon, Pierre, Poirier, Pons, Poulain, Prevost, Prévost, Remy, Rémy, Renard, Renaud, Renault, Rey, Reynaud, Richard, Riviere, Rivière, Robert, Robin, Roche, Rodriguez, Roger, Rolland, Rousseau, Roussel, Roux, Roy, Royer, Sanchez, Schmitt, Schneider, Simon, Tessier, Thomas, Vasseur, Vidal, Vincent, Weber]
prefix: [M., Mme, Dr., Prof., Me, Ing., Capt., Sgt., Adm., Cdt., Lt., Col., Gen., Sr., Fr., Rev.]
title:
job: [Superviseur, Executif, Manager, Ingenieur, Specialiste, Directeur, Coordinateur, Administrateur, Architecte, Analyste, Designer, Technicien, Developpeur, Producteur, Consultant, Assistant, Agent, Stagiaire]
name:
Expand All @@ -18,4 +19,4 @@ fr:
- "#{first_name} #{last_name} #{last_name}"
- "#{first_name} #{last_name} #{last_name}"
- "#{first_name} #{last_name} #{last_name}"
- "#{first_name} #{last_name} #{last_name}"
- "#{first_name} #{last_name} #{last_name}"
6 changes: 6 additions & 0 deletions test/faker/default/test_faker_chile_rut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def setup

def test_full_rut
assert_equal('6-k', @tester.full_rut(min_rut: 6, fixed: true))
assert_equal('8-6', @tester.full_rut(min_rut: 8, max_rut: 8))
assert_equal('11.111.111-1', @tester.full_rut(min_rut: 11_111_111, max_rut: 11_111_111, formatted: true))
assert_equal('30686957-4', @tester.full_rut(min_rut: 30_686_957, fixed: true))
end

Expand All @@ -17,6 +19,10 @@ def test_rut_length
assert @tester.rut.to_s.length <= 8
end

def test_rut_min_max
assert_equal(7, @tester.rut(min_rut: 7, max_rut: 7))
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.
Expand Down
4 changes: 2 additions & 2 deletions test/faker/default/test_faker_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_between
random_number = @tester.between(from: -50, to: 50)

assert random_number >= -50, "Expected >= -50, but got #{random_number}"
assert random_number <= 50, "Expected <= 50, but got #{random_number}"
assert random_number <= 50, "Expected <= 50, but got #{random_number}"
end
end

Expand All @@ -90,7 +90,7 @@ def test_within
random_number = @tester.within(range: -50..50)

assert random_number >= -50, "Expected >= -50, but got #{random_number}"
assert random_number <= 50, "Expected <= 50, but got #{random_number}"
assert random_number <= 50, "Expected <= 50, but got #{random_number}"
end
end

Expand Down
99 changes: 99 additions & 0 deletions test/test_default_locale.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# frozen_string_literal: true

require_relative 'test_helper'

class TestDefaultLocale < Test::Unit::TestCase
def test_setting_default_locale
# if locale is not set, fallback is :en
assert_equal :en, Faker::Config.locale

# locale can be updated initially
# and it becomes the default value
# for new threads
Faker::Config.default_locale = :pt

assert_equal :pt, Faker::Config.locale

t1 = Thread.new do
# child thread has initial locale equal to
# latest locale set on main thread
# instead of the fallback value
assert_equal :pt, Faker::Config.locale

# child thread can set its own locale
Faker::Config.locale = :es

assert_equal :es, Faker::Config.locale
end

t1.join

# child thread won't change locale of other threads
assert_equal :pt, Faker::Config.locale

t2 = Thread.new do
# initial default locale is copied over to new thread
assert_equal :pt, Faker::Config.locale

Faker::Config.locale = :it

assert_equal :it, Faker::Config.locale
end

t2.join

assert_equal :pt, Faker::Config.locale

# setting this to reset the default locale for all tests
Faker::Config.default_locale = nil

assert_equal :en, Faker::Config.locale
end

def test_setting_default_locale_on_child_thread
# if locale is not set, fallback is :en
assert_equal :en, Faker::Config.locale

# locale can be updated initially
# and it becomes the default value
# for new threads
Faker::Config.default_locale = :pt

assert_equal :pt, Faker::Config.locale

t1 = Thread.new do
# child thread has initial locale equal to
# latest locale set on main thread
# instead of the fallback value
assert_equal :pt, Faker::Config.locale

# child thread can set the default locale
Faker::Config.default_locale = :es

assert_equal :es, Faker::Config.locale
end

t1.join

# all threads now will have the same default
assert_equal :es, Faker::Config.locale

t2 = Thread.new do
# initial default locale is copied over to new thread
assert_equal :es, Faker::Config.locale

Faker::Config.locale = :it

assert_equal :it, Faker::Config.locale
end

t2.join

assert_equal :es, Faker::Config.locale

# setting this to reset the default locale for all tests
Faker::Config.default_locale = nil

assert_equal :en, Faker::Config.locale
end
end

0 comments on commit 9240e0b

Please sign in to comment.