faker 3.0.0 → 3.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +598 -1
  3. data/README.md +203 -107
  4. data/lib/faker/books/lovecraft.rb +2 -3
  5. data/lib/faker/default/address.rb +2 -2
  6. data/lib/faker/default/alphanumeric.rb +1 -1
  7. data/lib/faker/default/chile_rut.rb +23 -17
  8. data/lib/faker/default/code.rb +65 -19
  9. data/lib/faker/default/company.rb +112 -5
  10. data/lib/faker/default/crypto.rb +13 -4
  11. data/lib/faker/default/date.rb +61 -5
  12. data/lib/faker/default/driving_licence.rb +19 -8
  13. data/lib/faker/default/drone.rb +1 -1
  14. data/lib/faker/default/finance.rb +2 -2
  15. data/lib/faker/default/food.rb +14 -1
  16. data/lib/faker/default/hipster.rb +2 -3
  17. data/lib/faker/default/html.rb +230 -0
  18. data/lib/faker/default/id_number.rb +38 -24
  19. data/lib/faker/default/internet.rb +85 -78
  20. data/lib/faker/default/invoice.rb +4 -4
  21. data/lib/faker/default/json.rb +9 -4
  22. data/lib/faker/default/lorem.rb +24 -16
  23. data/lib/faker/default/marketing.rb +1 -1
  24. data/lib/faker/default/measurement.rb +8 -8
  25. data/lib/faker/default/{nhs.rb → national_health_service.rb} +8 -4
  26. data/lib/faker/default/number.rb +1 -1
  27. data/lib/faker/default/omniauth.rb +5 -5
  28. data/lib/faker/default/phone_number.rb +37 -18
  29. data/lib/faker/default/placeholdit.rb +1 -1
  30. data/lib/faker/default/south_africa.rb +3 -3
  31. data/lib/faker/{music/show.rb → default/theater.rb} +8 -8
  32. data/lib/faker/default/twitter.rb +4 -6
  33. data/lib/faker/default/types.rb +5 -6
  34. data/lib/faker/default/vehicle.rb +26 -36
  35. data/lib/faker/games/clash_of_clans.rb +1 -1
  36. data/lib/faker/games/dnd.rb +49 -7
  37. data/lib/faker/games/final_fantasy_xiv.rb +73 -0
  38. data/lib/faker/games/heroes_of_the_storm.rb +0 -11
  39. data/lib/faker/games/minecraft.rb +1 -1
  40. data/lib/faker/games/myst.rb +1 -1
  41. data/lib/faker/japanese_media/cowboy_bebop.rb +61 -0
  42. data/lib/faker/japanese_media/fullmetal_alchemist_brotherhood.rb +10 -10
  43. data/lib/faker/locations/australia.rb +40 -38
  44. data/lib/faker/movies/avatar.rb +49 -0
  45. data/lib/faker/movies/star_wars.rb +1 -1
  46. data/lib/faker/movies/{room.rb → the_room.rb} +4 -4
  47. data/lib/faker/music/smashing_pumpkins.rb +64 -0
  48. data/lib/faker/quotes/quote.rb +13 -0
  49. data/lib/faker/sports/chess.rb +90 -0
  50. data/lib/faker/sports/sport.rb +9 -3
  51. data/lib/faker/travel/airport.rb +43 -0
  52. data/lib/faker/travel/train_station.rb +54 -0
  53. data/lib/faker/tv_shows/archer.rb +51 -0
  54. data/lib/faker/tv_shows/buffy.rb +0 -13
  55. data/lib/faker/tv_shows/dr_who.rb +0 -13
  56. data/lib/faker/tv_shows/south_park.rb +15 -0
  57. data/lib/faker/tv_shows/spongebob.rb +50 -0
  58. data/lib/faker/tv_shows/the_fresh_prince_of_bel_air.rb +0 -13
  59. data/lib/faker/version.rb +1 -1
  60. data/lib/faker.rb +20 -9
  61. data/lib/helpers/deprecator.rb +118 -0
  62. data/lib/helpers/positional_generator.rb +480 -0
  63. data/lib/locales/README.md +18 -2
  64. data/lib/locales/ar.yml +1 -0
  65. data/lib/locales/bg.yml +1 -2
  66. data/lib/locales/da-DK.yml +529 -35
  67. data/lib/locales/de-AT.yml +3487 -27
  68. data/lib/locales/de-CH.yml +4359 -12
  69. data/lib/locales/de.yml +7182 -100
  70. data/lib/locales/ee.yml +0 -1
  71. data/lib/locales/en/address.yml +1826 -561
  72. data/lib/locales/en/airport.yml +381 -0
  73. data/lib/locales/en/archer.yml +75 -0
  74. data/lib/locales/en/australia.yml +106 -105
  75. data/lib/locales/en/avatar.yml +31 -0
  76. data/lib/locales/en/bank.yml +1 -1
  77. data/lib/locales/en/chess.yml +103 -0
  78. data/lib/locales/en/company.yml +1 -0
  79. data/lib/locales/en/cosmere.yml +1 -1
  80. data/lib/locales/en/cowboy_bebop.yml +163 -0
  81. data/lib/locales/en/dnd.yml +186 -1
  82. data/lib/locales/en/dog.yml +262 -7
  83. data/lib/locales/en/dota.yml +120 -67
  84. data/lib/locales/en/final_fantasy_xiv.yml +754 -0
  85. data/lib/locales/en/finance.yml +4 -4
  86. data/lib/locales/en/food.yml +14 -1
  87. data/lib/locales/en/{fma_brotherhood.yml → fullmetal_alchemist_brotherhood.yml} +3 -3
  88. data/lib/locales/en/harry_potter.yml +1 -1
  89. data/lib/locales/en/heroes.yml +0 -0
  90. data/lib/locales/en/id_number.yml +1 -1
  91. data/lib/locales/en/internet.yml +3 -4
  92. data/lib/locales/en/minecraft.yml +4 -4
  93. data/lib/locales/en/mitch_hedberg.yml +46 -0
  94. data/lib/locales/en/music.yml +25 -25
  95. data/lib/locales/en/opera.yml +1 -1
  96. data/lib/locales/en/phone_number.yml +78 -3
  97. data/lib/locales/en/restaurant.yml +1 -1
  98. data/lib/locales/en/smashing_pumpkins.yml +382 -0
  99. data/lib/locales/en/south_park.yml +360 -2
  100. data/lib/locales/en/spongebob.yml +489 -0
  101. data/lib/locales/en/star_wars.yml +1 -1
  102. data/lib/locales/en/{room.yml → the_room.yml} +1 -1
  103. data/lib/locales/en/{show.yml → theater.yml} +1 -1
  104. data/lib/locales/en/train_station.yml +280 -0
  105. data/lib/locales/en/vehicle.yml +1 -1
  106. data/lib/locales/en-AU.yml +718 -47
  107. data/lib/locales/en-CA.yml +373 -19
  108. data/lib/locales/en-GB.yml +116 -8
  109. data/lib/locales/en-IND.yml +1259 -17
  110. data/lib/locales/en-KE.yml +212 -0
  111. data/lib/locales/en-MS.yml +364 -17
  112. data/lib/locales/en-NEP.yml +212 -38
  113. data/lib/locales/en-NG.yml +1 -0
  114. data/lib/locales/en-NZ.yml +1153 -123
  115. data/lib/locales/en-PAK.yml +392 -10
  116. data/lib/locales/en-SG.yml +581 -16
  117. data/lib/locales/en-UG.yml +1 -0
  118. data/lib/locales/en-US.yml +6914 -74
  119. data/lib/locales/en-ZA.yml +1 -1
  120. data/lib/locales/en-au-ocker.yml +266 -22
  121. data/lib/locales/es-AR.yml +4569 -4599
  122. data/lib/locales/es-MX.yml +1 -2
  123. data/lib/locales/es.yml +2 -3
  124. data/lib/locales/fi-FI.yml +1 -0
  125. data/lib/locales/fr/address.yml +761 -9
  126. data/lib/locales/fr/internet.yml +0 -1
  127. data/lib/locales/fr/name.yml +2 -1
  128. data/lib/locales/fr-CA.yml +2733 -44
  129. data/lib/locales/fr-CH.yml +1 -2
  130. data/lib/locales/fr.yml +3 -1
  131. data/lib/locales/hy.yml +3624 -119
  132. data/lib/locales/id.yml +839 -12
  133. data/lib/locales/it.yml +1304 -32
  134. data/lib/locales/ja/book.yml +492 -3
  135. data/lib/locales/ja/football.yml +115 -0
  136. data/lib/locales/ja/lorem.yml +1 -1
  137. data/lib/locales/ja/sport.yml +130 -0
  138. data/lib/locales/ja/super_mario.yml +1 -1
  139. data/lib/locales/ja/touhou.yml +466 -0
  140. data/lib/locales/ko.yml +1361 -20
  141. data/lib/locales/lt.yml +297 -17
  142. data/lib/locales/lv.yml +0 -1
  143. data/lib/locales/mi-NZ.yml +2 -0
  144. data/lib/locales/nb-NO.yml +518 -29
  145. data/lib/locales/nl.yml +10 -2
  146. data/lib/locales/pl.yml +1 -2
  147. data/lib/locales/pt-BR.yml +8205 -664
  148. data/lib/locales/pt.yml +844 -45
  149. data/lib/locales/ru.yml +1353 -69
  150. data/lib/locales/sk.yml +4410 -43
  151. data/lib/locales/sv.yml +100 -0
  152. data/lib/locales/tr.yml +1 -2
  153. data/lib/locales/uk.yml +1808 -44
  154. data/lib/locales/vi.yml +1 -0
  155. data/lib/locales/zh-CN/bank.yml +17 -0
  156. data/lib/locales/zh-CN.yml +1 -0
  157. data/lib/locales/zh-TW.yml +1 -0
  158. metadata +37 -156
  159. data/History.md +0 -176
  160. data/lib/faker/default/fillmurray.rb +0 -36
  161. data/lib/faker/default/lorem_pixel.rb +0 -64
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Sports
5
+ class Chess < Base
6
+ class << self
7
+ ##
8
+ # Produces the name of a chess player name.
9
+ #
10
+ # @return [String]
11
+ #
12
+ # @example
13
+ # Faker::Sports::Chess.player #=> "Golden State Warriors"
14
+ #
15
+ # @faker.version next
16
+ def player
17
+ fetch('chess.players')
18
+ end
19
+
20
+ ##
21
+ # Produces a long (alpha-3) ISO 3166 country code.
22
+ #
23
+ # @return [String]
24
+ #
25
+ # @example
26
+ # Faker::Chess.federation #=> "COL"
27
+ #
28
+ # @faker.version next
29
+ def federation
30
+ Faker::Address.country_code_long
31
+ end
32
+
33
+ def tournament
34
+ ##
35
+ # Produces the name of a famous chess tournament name.
36
+ #
37
+ # @return [String]
38
+ #
39
+ # @example
40
+ # Faker::Chess.tournament #=> "Khanty-Mansisyk (Candidates Tournament)"
41
+ #
42
+ # @faker.version next
43
+ fetch('chess.tournaments')
44
+ end
45
+
46
+ def rating(from: 2000, to: 2900)
47
+ ##
48
+ # Produces a rating between two provided values. Boundaries are inclusive.
49
+ #
50
+ # @param from [Numeric] The lowest number to include.
51
+ # @param to [Numeric] The highest number to include.
52
+ # @return [Numeric]
53
+ #
54
+ # @example
55
+ # Faker::Sports::Chess.rating #=> 2734
56
+ # Faker::Sports::Chess.rating(from: 2400, to: 2700) #=> 2580
57
+ #
58
+ # @faker.version next
59
+ Faker::Base.rand_in_range(from, to)
60
+ end
61
+
62
+ ##
63
+ # Produces the name of a chess opening.
64
+ #
65
+ # @return [String]
66
+ #
67
+ # @example
68
+ # Faker::Sports::Chess.opening #=> "Giuoco Piano"
69
+ #
70
+ # @faker.version next
71
+ def opening
72
+ fetch('chess.openings')
73
+ end
74
+
75
+ ##
76
+ # Produces a chess title.
77
+ #
78
+ # @return [String]
79
+ #
80
+ # @example
81
+ # Faker::Sports::Chess.title #=> "GM"
82
+ #
83
+ # @faker.version next
84
+ def title
85
+ fetch('chess.titles')
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -22,9 +22,15 @@ module Faker
22
22
  #
23
23
  # @faker.version next
24
24
  def sport(include_ancient: false, include_unusual: false)
25
- sports = fetch_all('sport.summer_olympics') + fetch_all('sport.winter_olympics') + fetch_all('sport.summer_paralympics') + fetch_all('sport.winter_paralympics')
26
- sports << fetch_all('sport.ancient_olympics') if include_ancient
27
- sports << fetch_all('sport.unusual') if include_unusual
25
+ sports = []
26
+ sports.concat(
27
+ fetch_all('sport.summer_olympics'),
28
+ fetch_all('sport.summer_paralympics'),
29
+ fetch_all('sport.winter_olympics'),
30
+ fetch_all('sport.winter_paralympics')
31
+ )
32
+ sports.concat(fetch_all('sport.ancient_olympics')) if include_ancient
33
+ sports.concat(fetch_all('sport.unusual')) if include_unusual
28
34
  sample(sports)
29
35
  end
30
36
 
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Travel
5
+ class Airport < Base
6
+ class << self
7
+ ##
8
+ # Produces random Airport by name and takes arguments for size and region
9
+ #
10
+ # @param size [String] airport size, united_states has large, or medium, or small, european_union has large, or medium
11
+ #
12
+ # @param region [String] airport region, currently available -> united_states or european_union
13
+ #
14
+ # @return [String]
15
+ #
16
+ # @example
17
+ # Faker::Travel::Airport.name(size: 'large', region: 'united_states') => "Los Angeles International Airport"
18
+ #
19
+ # @faker.version next
20
+ def name(size:, region:)
21
+ fetch("airport.#{region}.#{size}")
22
+ end
23
+
24
+ ##
25
+ # Produces random Airport by IATA code and takes arguments for size and region
26
+ #
27
+ # @param size [String] airport size, united_states has large, or medium, or small, european_union has large, or medium
28
+ #
29
+ # @param region [String] airport region, currently available -> united_states or european_union
30
+ #
31
+ # @return [String]
32
+ #
33
+ # @example
34
+ # Faker::Travel::Airport.iata(size: 'large', region: 'united_states') => "LAX"
35
+ #
36
+ # @faker.version next
37
+ def iata(size:, region:)
38
+ fetch("airport.#{region}.iata_code.#{size}")
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Travel
5
+ class TrainStation < Base
6
+ class << self
7
+ ##
8
+ # Produces random Train Station by name and takes optional arguments for region and type
9
+ #
10
+ # @param region [String] Train station region: germany, spain, united_kingdom, united_states
11
+ #
12
+ # @param type [String] Train station type: metro, railway
13
+ #
14
+ # @return [String]
15
+ #
16
+ # @examples
17
+ # Faker::Travel::TrainStation.name(region: 'united_kingdom', type: 'metro') => "Brockley"
18
+ # Faker::Travel::TrainStation.name(type: 'railway') => "Düsseldorf Hauptbahnhof"
19
+ # Faker::Travel::TrainStation.name(region: 'spain') => "Santa Eulàlia"
20
+ #
21
+ # @faker.version next
22
+ def name(region: nil, type: nil)
23
+ region, type = fill_missing_inputs_with_samples(region, type)
24
+ fetch("train_station.#{region}.#{type}")
25
+ end
26
+
27
+ private
28
+
29
+ def fill_missing_inputs_with_samples(region, type)
30
+ regions = %w[germany spain united_kingdom united_states]
31
+ types = %w[metro railway]
32
+
33
+ if region.nil? && type.nil?
34
+ region = sample(regions)
35
+ type = sample(types)
36
+ elsif region.nil?
37
+ validate_arguments(type, types, 'type')
38
+ region = sample(regions)
39
+ elsif type.nil?
40
+ validate_arguments(region, regions, 'region')
41
+ type = sample(types)
42
+ end
43
+ [region, type]
44
+ end
45
+
46
+ def validate_arguments(argument, correct_values, argument_name)
47
+ return if correct_values.include?(argument)
48
+
49
+ raise ArgumentError, "'#{argument}' not found, #{argument_name} can be blank, or one of the following, as strings: #{correct_values.join(', ')}"
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class TvShows
5
+ class Archer < Base
6
+ flexible :archer
7
+
8
+ class << self
9
+ ##
10
+ # Produces a character from Archer.
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @example
15
+ # Faker::TvShows::Archer.character #=> "Sterling Archer"
16
+ #
17
+ # @faker.version next
18
+ def character
19
+ fetch('archer.characters')
20
+ end
21
+
22
+ ##
23
+ # Produces a location from Archer.
24
+ #
25
+ # @return [String]
26
+ #
27
+ # @example
28
+ # Faker::TvShows::Archer.location #=> "The Tuntmore Towers"
29
+ #
30
+ # @faker.version next
31
+ def location
32
+ fetch('archer.locations')
33
+ end
34
+
35
+ ##
36
+ # Produces a quote from Archer.
37
+ #
38
+ # @return [String]
39
+ #
40
+ # @example
41
+ # Faker::TvShows::Archer.quote
42
+ # #=> "You're not my supervisor!"
43
+ #
44
+ # @faker.version next
45
+ def quote
46
+ fetch('archer.quotes')
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -45,19 +45,6 @@ module Faker
45
45
  fetch('buffy.actors')
46
46
  end
47
47
 
48
- ##
49
- # Produces a actor from Buffy the Vampire Slayer.
50
- #
51
- # @return [String]
52
- #
53
- # @example
54
- # Faker::TvShows::Buffy.celebrity #=> "John Ritter"
55
- #
56
- # @deprecated Use the `actor` method instead.
57
- #
58
- # @faker.version 1.9.2
59
- alias celebrity actor
60
-
61
48
  ##
62
49
  # Produces a big bad from Buffy the Vampire Slayer.
63
50
  #
@@ -84,19 +84,6 @@ module Faker
84
84
  fetch('dr_who.villains')
85
85
  end
86
86
 
87
- ##
88
- # Produces a villain from Doctor Who.
89
- #
90
- # @return [String]
91
- #
92
- # @example
93
- # Faker::TvShows::DrWho.villian #=> "The Master"
94
- #
95
- # @deprecated Use the correctly-spelled `villain` method instead.
96
- #
97
- # @faker.version 1.8.0
98
- alias villian villain
99
-
100
87
  ##
101
88
  # Produces a species from Doctor Who.
102
89
  #
@@ -32,6 +32,21 @@ module Faker
32
32
  def quote
33
33
  fetch('south_park.quotes')
34
34
  end
35
+
36
+ ##
37
+ # Produces an episode name from South Park.
38
+ #
39
+ # @return [String]
40
+ #
41
+ # @example
42
+ # Faker::TvShows::SouthPark.episode_name
43
+ # #=> "Make Love, Not Warcraft"
44
+ #
45
+ # @faker.version next
46
+
47
+ def episode_name
48
+ fetch('south_park.episodes')
49
+ end
35
50
  end
36
51
  end
37
52
  end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class TvShows
5
+ class Spongebob < Base
6
+ flexible :spongebob
7
+
8
+ class << self
9
+ ##
10
+ # Produces a character from Spongebob.
11
+ #
12
+ # @return [String]
13
+ #
14
+ # @example
15
+ # Faker::TvShows::Spongebob.character #=> "Patrick"
16
+ #
17
+ # @faker.version next
18
+ def character
19
+ fetch('spongebob.characters')
20
+ end
21
+
22
+ ##
23
+ # Produces a quote from Spongebob.
24
+ #
25
+ # @return [String]
26
+ #
27
+ # @example
28
+ # Faker::TvShows::Spongebob.quote #=> "I'm ready! I'm ready!"
29
+ #
30
+ # @faker.version next
31
+ def quote
32
+ fetch('spongebob.quotes')
33
+ end
34
+
35
+ ##
36
+ # Produces an episode from Spongebob.
37
+ #
38
+ # @return [String]
39
+ #
40
+ # @example
41
+ # Faker::TvShows::Spongebob.episode #=> "Reef Blower"
42
+ #
43
+ # @faker.version next
44
+ def episode
45
+ fetch('spongebob.episodes')
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -32,19 +32,6 @@ module Faker
32
32
  fetch('the_fresh_prince_of_bel_air.actors')
33
33
  end
34
34
 
35
- ##
36
- # Produces a actor from The Fresh Prince of Bel-Air.
37
- #
38
- # @return [String]
39
- #
40
- # @example
41
- # Faker::TvShows::TheFreshPrinceOfBelAir.celebrity #=> "Quincy Jones"
42
- #
43
- # @deprecated Use the `actor` method instead.
44
- #
45
- # @faker.version 1.8.3
46
- alias celebrity actor
47
-
48
35
  ##
49
36
  # Produces a quote from The Fresh Prince of Bel-Air.
50
37
  #
data/lib/faker/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Faker # :nodoc:
4
- VERSION = '3.0.0'
4
+ VERSION = '3.5.2'
5
5
  end
data/lib/faker.rb CHANGED
@@ -5,21 +5,24 @@ mydir = __dir__
5
5
  require 'psych'
6
6
  require 'i18n'
7
7
 
8
- Dir.glob(File.join(mydir, 'helpers', '*.rb')).sort.each { |file| require file }
8
+ Dir.glob(File.join(mydir, 'helpers', '*.rb')).each { |file| require file }
9
9
 
10
10
  I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')]
11
- I18n.reload! if I18n.backend.initialized?
12
11
 
13
12
  module Faker
14
13
  module Config
14
+ @default_locale = nil
15
+
15
16
  class << self
17
+ attr_writer :default_locale
18
+
16
19
  def locale=(new_locale)
17
20
  Thread.current[:faker_config_locale] = new_locale
18
21
  end
19
22
 
20
23
  def locale
21
24
  # Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
22
- Thread.current[:faker_config_locale] || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
25
+ Thread.current[:faker_config_locale] || @default_locale || (I18n.available_locales.include?(I18n.locale) ? I18n.locale : I18n.available_locales.first)
23
26
  end
24
27
 
25
28
  def own_locale
@@ -49,19 +52,23 @@ module Faker
49
52
 
50
53
  ## by default numerify results do not start with a zero
51
54
  def numerify(number_string, leading_zero: false)
52
- return number_string.gsub(/#/) { rand(10).to_s } if leading_zero
55
+ return number_string.gsub('#') { rand(10).to_s } if leading_zero
53
56
 
54
- number_string.sub(/#/) { rand(1..9).to_s }.gsub(/#/) { rand(10).to_s }
57
+ number_string.sub('#') { rand(1..9).to_s }.gsub('#') { rand(10).to_s }
55
58
  end
56
59
 
57
60
  def letterify(letter_string)
58
- letter_string.gsub(/\?/) { sample(ULetters) }
61
+ letter_string.gsub('?') { sample(ULetters) }
59
62
  end
60
63
 
61
64
  def bothify(string)
62
65
  letterify(numerify(string))
63
66
  end
64
67
 
68
+ def generate(as_type, &block)
69
+ PositionalGenerator.new(as_type, &block).generate
70
+ end
71
+
65
72
  # Given a regular expression, attempt to generate a string
66
73
  # that would match it. This is a rather simple implementation,
67
74
  # so don't be shocked if it blows up on you in a spectacular fashion.
@@ -85,7 +92,7 @@ module Faker
85
92
  reg = reg.source if reg.respond_to?(:source) # Handle either a Regexp or a String that looks like a Regexp
86
93
  reg
87
94
  .gsub(%r{^/?\^?}, '').gsub(%r{\$?/?$}, '') # Ditch the anchors
88
- .gsub(/\{(\d+)\}/, '{\1,\1}').gsub(/\?/, '{0,1}') # All {2} become {2,2} and ? become {0,1}
95
+ .gsub(/\{(\d+)\}/, '{\1,\1}').gsub('?', '{0,1}') # All {2} become {2,2} and ? become {0,1}
89
96
  .gsub(/(\[[^\]]+\])\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # [12]{1,2} becomes [12] or [12][12]
90
97
  .gsub(/(\([^)]+\))\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # (12|34){1,2} becomes (12|34) or (12|34)(12|34)
91
98
  .gsub(/(\\?.)\{(\d+),(\d+)\}/) { |_match| Regexp.last_match(1) * sample(Array(Range.new(Regexp.last_match(2).to_i, Regexp.last_match(3).to_i))) } # A{1,2} becomes A or AA or \d{3} becomes \d\d\d
@@ -100,7 +107,7 @@ module Faker
100
107
  # with an array of values and selecting one of them.
101
108
  def fetch(key)
102
109
  fetched = sample(translate("faker.#{key}"))
103
- if fetched&.match(%r{^/}) && fetched&.match(%r{/$}) # A regex
110
+ if fetched&.match(%r{^/}) && fetched.match(%r{/$}) # A regex
104
111
  regexify(fetched)
105
112
  else
106
113
  fetched
@@ -242,6 +249,10 @@ module Faker
242
249
  list.shuffle(random: Faker::Config.random)
243
250
  end
244
251
 
252
+ def shuffle!(list)
253
+ list.shuffle!(random: Faker::Config.random)
254
+ end
255
+
245
256
  def rand(max = nil)
246
257
  if max.nil?
247
258
  Faker::Config.random.rand
@@ -264,4 +275,4 @@ module Faker
264
275
  end
265
276
 
266
277
  # require faker objects
267
- Dir.glob(File.join(mydir, 'faker', '/**/*.rb')).sort.each { |file| require file }
278
+ Dir.glob(File.join(mydir, 'faker', '/**/*.rb')).each { |file| require file }
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Based on Rails ActiveSupport Deprecator
4
+ # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/deprecation/constant_accessor.rb
5
+
6
+ # rubocop:disable Style/ClassVars
7
+ module Faker
8
+ # Provides a way to rename generators, including their namespaces, with a deprecation cycle in which
9
+ # both the old and new names work, but using the old one prints a deprecation message.
10
+ #
11
+ # Deprecator provides a deprecate_generator method to be used when
12
+ # renaming a generator. For example, let's say we want to change the following Generator's
13
+ # name to <tt>Faker::NewGenerator</tt>:
14
+ #
15
+ # module Faker
16
+ # class Generator
17
+ # def self.generate
18
+ # "be kind"
19
+ # end
20
+ # end
21
+ # end
22
+ #
23
+ # To rename it, you need to do the update the name and declare the deprecation by
24
+ # including the <tt>Deprecator</tt> module and using the deprecate_generator method:
25
+ #
26
+ # module Faker
27
+ # class NewGenerator
28
+ # def self.generate
29
+ # "be kind"
30
+ # end
31
+ # end
32
+ #
33
+ # include Deprecator
34
+ # deprecate_generator('DeprecatedGenerator', NewGenerator)
35
+ # end
36
+ #
37
+ # The first argument is a constant name (no colons) as a string. It is the name of
38
+ # the constant you want to deprecate.
39
+ #
40
+ # The second argument is the constant path of the replacement (no colons) as a constant.
41
+ #
42
+ # For this to work, a +const_missing+ hook is installed. When users
43
+ # reference the deprecated constant, the callback prints the
44
+ # message and constantizes the replacement.
45
+ #
46
+ # With that in place, references to <tt>Faker::Deprecator</tt> still work, they
47
+ # evaluate to <tt>Faker::NewGenerator</tt> now, and trigger a deprecation warning:
48
+ #
49
+ # Faker::Generator.generate
50
+ # # DEPRECATION WARNING: Faker::Generator is deprecated. Use Faker::NewGenerator instead
51
+ # # "be kind"
52
+ #
53
+ # For testing the deprecations, we provide <tt>assert_deprecated</tt>
54
+ # and <tt>assert_not_deprecated</tt> matchers.
55
+ #
56
+ # There's also a <tt>Faker::Deprecator.skip_warning</tt> helper to silence
57
+ # the deprecation messages in the *test* output. Use it for generators that have lots of tests
58
+ # to avoid too many noise when running the tests.
59
+ module Deprecator
60
+ def self.included(base)
61
+ extension = Module.new do
62
+ def const_missing(missing_const_name)
63
+ if class_variable_defined?(:@@_deprecated_constants) && (replacement = class_variable_get(:@@_deprecated_constants)[missing_const_name.to_s])
64
+ unless Faker::Deprecator.skip_warning?
65
+ deprecated_message = "#{name}::#{replacement[:old_generator]} is deprecated."
66
+ replacement_message = "Use #{replacement[:new_constant]} instead."
67
+ $stdout.puts("DEPRECATION WARNING: #{deprecated_message} #{replacement_message}")
68
+ end
69
+
70
+ return replacement[:new_constant]
71
+ end
72
+
73
+ super
74
+ end
75
+
76
+ def deprecate_generator(old_generator_name, new_generator_constant)
77
+ class_variable_set(:@@_deprecated_constants, {}) unless class_variable_defined?(:@@_deprecated_constants)
78
+ class_variable_get(:@@_deprecated_constants)[old_generator_name] = {
79
+ new_constant: new_generator_constant,
80
+ old_generator: old_generator_name
81
+ }
82
+ end
83
+ end
84
+
85
+ base.singleton_class.prepend extension
86
+ end
87
+
88
+ # Silence deprecation warnings within the block.
89
+ #
90
+ # Faker::Generator.generate
91
+ # # => DEPRECATION WARNING: Faker::Generator is deprecated. Use Faker::NewGenerator instead.
92
+ #
93
+ # Faker::Deprecator.skip_warning do
94
+ # Faker::Generator.generate
95
+ # end
96
+ # # => nil
97
+ def self.skip_warning
98
+ original = Faker::Deprecator.skip
99
+ Faker::Deprecator.skip = true
100
+ yield
101
+ ensure
102
+ Faker::Deprecator.skip = original
103
+ end
104
+
105
+ def self.skip_warning?
106
+ @skip == true
107
+ end
108
+
109
+ def self.skip
110
+ @skip ||= false
111
+ end
112
+
113
+ def self.skip=(value)
114
+ @skip = value
115
+ end
116
+ end
117
+ end
118
+ # rubocop:enable Style/ClassVars