faker 2.15.1 → 3.4.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (305) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +848 -34
  3. data/README.md +225 -103
  4. data/lib/faker/blockchain/aeternity.rb +1 -1
  5. data/lib/faker/blockchain/bitcoin.rb +2 -2
  6. data/lib/faker/blockchain/ethereum.rb +1 -1
  7. data/lib/faker/blockchain/tezos.rb +31 -3
  8. data/lib/faker/books/dune.rb +2 -10
  9. data/lib/faker/books/lovecraft.rb +9 -41
  10. data/lib/faker/books/the_kingkiller_chronicle.rb +61 -0
  11. data/lib/faker/creature/bird.rb +203 -0
  12. data/lib/faker/default/address.rb +7 -27
  13. data/lib/faker/default/alphanumeric.rb +2 -8
  14. data/lib/faker/default/app.rb +1 -10
  15. data/lib/faker/default/avatar.rb +1 -12
  16. data/lib/faker/default/bank.rb +25 -17
  17. data/lib/faker/default/barcode.rb +33 -22
  18. data/lib/faker/default/boolean.rb +1 -4
  19. data/lib/faker/default/cannabis.rb +10 -0
  20. data/lib/faker/default/chile_rut.rb +25 -25
  21. data/lib/faker/default/chuck_norris.rb +1 -0
  22. data/lib/faker/default/code.rb +71 -42
  23. data/lib/faker/default/color.rb +77 -6
  24. data/lib/faker/default/commerce.rb +31 -17
  25. data/lib/faker/default/company.rb +137 -22
  26. data/lib/faker/default/crypto.rb +26 -4
  27. data/lib/faker/default/crypto_coin.rb +3 -15
  28. data/lib/faker/default/date.rb +65 -42
  29. data/lib/faker/default/demographic.rb +1 -5
  30. data/lib/faker/default/driving_licence.rb +20 -18
  31. data/lib/faker/default/drone.rb +1 -1
  32. data/lib/faker/default/educator.rb +13 -0
  33. data/lib/faker/default/emotion.rb +33 -0
  34. data/lib/faker/default/file.rb +5 -22
  35. data/lib/faker/default/finance.rb +30 -7
  36. data/lib/faker/default/food.rb +27 -1
  37. data/lib/faker/default/hipster.rb +8 -48
  38. data/lib/faker/default/hobby.rb +22 -0
  39. data/lib/faker/default/html.rb +230 -0
  40. data/lib/faker/default/id_number.rb +175 -27
  41. data/lib/faker/default/internet.rb +333 -114
  42. data/lib/faker/default/invoice.rb +4 -17
  43. data/lib/faker/default/json.rb +4 -21
  44. data/lib/faker/default/lorem.rb +175 -73
  45. data/lib/faker/default/lorem_flickr.rb +4 -37
  46. data/lib/faker/default/markdown.rb +10 -13
  47. data/lib/faker/default/marketing.rb +1 -1
  48. data/lib/faker/default/measurement.rb +16 -48
  49. data/lib/faker/default/name.rb +1 -5
  50. data/lib/faker/default/nation.rb +0 -3
  51. data/lib/faker/default/{nhs.rb → national_health_service.rb} +9 -8
  52. data/lib/faker/default/number.rb +33 -61
  53. data/lib/faker/default/omniauth.rb +55 -52
  54. data/lib/faker/default/phone_number.rb +36 -21
  55. data/lib/faker/default/placeholdit.rb +10 -20
  56. data/lib/faker/default/relationship.rb +1 -5
  57. data/lib/faker/default/religion.rb +6 -0
  58. data/lib/faker/default/science.rb +108 -0
  59. data/lib/faker/default/source.rb +3 -16
  60. data/lib/faker/default/south_africa.rb +3 -3
  61. data/lib/faker/default/string.rb +1 -7
  62. data/lib/faker/default/stripe.rb +4 -20
  63. data/lib/faker/default/tea.rb +41 -0
  64. data/lib/faker/{music/show.rb → default/theater.rb} +11 -8
  65. data/lib/faker/default/time.rb +4 -34
  66. data/lib/faker/default/twitter.rb +7 -23
  67. data/lib/faker/default/types.rb +12 -34
  68. data/lib/faker/default/vehicle.rb +44 -54
  69. data/lib/faker/default/vulnerability_identifier.rb +23 -0
  70. data/lib/faker/default/world_cup.rb +2 -11
  71. data/lib/faker/games/clash_of_clans.rb +1 -1
  72. data/lib/faker/games/dnd.rb +49 -7
  73. data/lib/faker/games/dota.rb +14 -5
  74. data/lib/faker/games/final_fantasy_xiv.rb +73 -0
  75. data/lib/faker/games/minecraft.rb +1 -1
  76. data/lib/faker/games/myst.rb +1 -1
  77. data/lib/faker/games/tarkov.rb +205 -0
  78. data/lib/faker/games/touhou.rb +75 -0
  79. data/lib/faker/games/witcher.rb +39 -0
  80. data/lib/faker/japanese_media/cowboy_bebop.rb +61 -0
  81. data/lib/faker/japanese_media/fullmetal_alchemist_brotherhood.rb +51 -0
  82. data/lib/faker/japanese_media/kamen_rider.rb +102 -0
  83. data/lib/faker/japanese_media/one_piece.rb +1 -1
  84. data/lib/faker/locations/australia.rb +52 -0
  85. data/lib/faker/movies/avatar.rb +49 -0
  86. data/lib/faker/movies/hackers.rb +48 -0
  87. data/lib/faker/movies/star_wars.rb +73 -5
  88. data/lib/faker/movies/{room.rb → the_room.rb} +4 -4
  89. data/lib/faker/movies/tron.rb +161 -0
  90. data/lib/faker/music/hiphop.rb +3 -3
  91. data/lib/faker/music/music.rb +12 -0
  92. data/lib/faker/music/rock_band.rb +12 -0
  93. data/lib/faker/music/smashing_pumpkins.rb +64 -0
  94. data/lib/faker/quotes/quote.rb +13 -0
  95. data/lib/faker/quotes/rajnikanth.rb +1 -0
  96. data/lib/faker/quotes/shakespeare.rb +34 -0
  97. data/lib/faker/religion/bible.rb +50 -0
  98. data/lib/faker/sports/chess.rb +90 -0
  99. data/lib/faker/sports/mountaineering.rb +22 -0
  100. data/lib/faker/sports/sport.rb +116 -0
  101. data/lib/faker/travel/airport.rb +43 -0
  102. data/lib/faker/travel/train_station.rb +54 -0
  103. data/lib/faker/tv_shows/archer.rb +51 -0
  104. data/lib/faker/tv_shows/big_bang_theory.rb +1 -1
  105. data/lib/faker/tv_shows/brooklyn_nine_nine.rb +38 -0
  106. data/lib/faker/tv_shows/michael_scott.rb +0 -3
  107. data/lib/faker/tv_shows/south_park.rb +15 -0
  108. data/lib/faker/tv_shows/spongebob.rb +50 -0
  109. data/lib/faker/tv_shows/suits.rb +1 -1
  110. data/lib/faker/tv_shows/supernatural.rb +48 -0
  111. data/lib/faker/tv_shows/the_office.rb +37 -0
  112. data/lib/faker/version.rb +2 -2
  113. data/lib/faker.rb +26 -67
  114. data/lib/helpers/base58.rb +1 -1
  115. data/lib/helpers/deprecator.rb +53 -0
  116. data/lib/helpers/positional_generator.rb +480 -0
  117. data/lib/helpers/unique_generator.rb +13 -14
  118. data/lib/locales/README.md +18 -2
  119. data/lib/locales/ar.yml +6 -1
  120. data/lib/locales/bg.yml +1 -2
  121. data/lib/locales/da-DK.yml +529 -35
  122. data/lib/locales/de-AT.yml +3487 -27
  123. data/lib/locales/de-CH.yml +4328 -12
  124. data/lib/locales/de.yml +7185 -99
  125. data/lib/locales/ee.yml +0 -1
  126. data/lib/locales/en/address.yml +1826 -562
  127. data/lib/locales/en/airport.yml +381 -0
  128. data/lib/locales/en/animal.yml +1 -1
  129. data/lib/locales/en/archer.yml +75 -0
  130. data/lib/locales/en/australia.yml +108 -0
  131. data/lib/locales/en/avatar.yml +31 -0
  132. data/lib/locales/en/bank.yml +1 -1
  133. data/lib/locales/en/bible.yml +90 -0
  134. data/lib/locales/en/bird.yml +1281 -0
  135. data/lib/locales/en/book.yml +487 -3
  136. data/lib/locales/en/brooklyn_nine_nine.yml +35 -0
  137. data/lib/locales/en/cat.yml +1 -1
  138. data/lib/locales/en/chess.yml +103 -0
  139. data/lib/locales/en/coffee.yml +1 -1
  140. data/lib/locales/en/commerce.yml +17 -0
  141. data/lib/locales/en/community.yml +17 -17
  142. data/lib/locales/en/company.yml +1 -0
  143. data/lib/locales/en/computer.yml +23 -4
  144. data/lib/locales/en/cowboy_bebop.yml +163 -0
  145. data/lib/locales/en/device.yml +112 -4
  146. data/lib/locales/en/dnd.yml +186 -1
  147. data/lib/locales/en/dog.yml +262 -7
  148. data/lib/locales/en/dota.yml +173 -0
  149. data/lib/locales/en/dune.yml +266 -397
  150. data/lib/locales/en/educator.yml +6 -0
  151. data/lib/locales/en/emotion.yml +480 -0
  152. data/lib/locales/en/file.yml +9 -1
  153. data/lib/locales/en/final_fantasy_xiv.yml +754 -0
  154. data/lib/locales/en/finance.yml +27 -4
  155. data/lib/locales/en/food.yml +1067 -10
  156. data/lib/locales/en/football.yml +3 -3
  157. data/lib/locales/en/fullmetal_alchemist_brotherhood.yml +78 -0
  158. data/lib/locales/en/game.yml +11 -0
  159. data/lib/locales/en/hackers.yml +53 -0
  160. data/lib/locales/en/harry_potter.yml +1 -1
  161. data/lib/locales/en/heroes.yml +2 -2
  162. data/lib/locales/en/hobby.yml +171 -0
  163. data/lib/locales/en/horse.yml +2 -2
  164. data/lib/locales/en/id_number.yml +1 -1
  165. data/lib/locales/en/internet.yml +136 -3
  166. data/lib/locales/en/kamen_rider.yml +452 -0
  167. data/lib/locales/en/kpop.yml +7 -7
  168. data/lib/locales/en/lebowski.yml +1 -1
  169. data/lib/locales/en/lovecraft.yml +76 -6
  170. data/lib/locales/en/minecraft.yml +4 -4
  171. data/lib/locales/en/mitch_hedberg.yml +46 -0
  172. data/lib/locales/en/mountain.yml +14 -1
  173. data/lib/locales/en/mountaineering.yml +14 -0
  174. data/lib/locales/en/movie.yml +2 -1
  175. data/lib/locales/en/music.yml +152 -142
  176. data/lib/locales/en/naruto.yml +2 -3
  177. data/lib/locales/en/one_piece.yml +1 -1
  178. data/lib/locales/en/opera.yml +2 -2
  179. data/lib/locales/en/overwatch.yml +5 -7
  180. data/lib/locales/en/phone_number.yml +78 -3
  181. data/lib/locales/en/rock_band.yml +1 -0
  182. data/lib/locales/en/science.yml +481 -3
  183. data/lib/locales/en/shakespeare.yml +21 -24
  184. data/lib/locales/en/smashing_pumpkins.yml +382 -0
  185. data/lib/locales/en/source.yml +5 -0
  186. data/lib/locales/en/south_park.yml +360 -2
  187. data/lib/locales/en/space.yml +1 -1
  188. data/lib/locales/en/spongebob.yml +489 -0
  189. data/lib/locales/en/sport.yml +130 -0
  190. data/lib/locales/en/star_wars.yml +1 -1
  191. data/lib/locales/en/stranger_thing.yml +1 -1
  192. data/lib/locales/en/super_smash_bros.yml +7 -2
  193. data/lib/locales/en/supernatural.yml +141 -0
  194. data/lib/locales/en/tarkov.yml +593 -0
  195. data/lib/locales/en/tea.yml +172 -0
  196. data/lib/locales/en/the_kingkiller_chronicle.yml +56 -0
  197. data/lib/locales/en/the_office.yml +86 -0
  198. data/lib/locales/en/{room.yml → the_room.yml} +1 -1
  199. data/lib/locales/en/{show.yml → theater.yml} +1 -1
  200. data/lib/locales/en/touhou.yml +839 -0
  201. data/lib/locales/en/train_station.yml +280 -0
  202. data/lib/locales/en/tron.yml +227 -0
  203. data/lib/locales/en/vehicle.yml +2809 -75
  204. data/lib/locales/en/witcher.yml +37 -0
  205. data/lib/locales/en-AU.yml +718 -47
  206. data/lib/locales/en-CA.yml +373 -18
  207. data/lib/locales/en-GB.yml +116 -8
  208. data/lib/locales/en-IND.yml +1259 -17
  209. data/lib/locales/en-KE.yml +212 -0
  210. data/lib/locales/en-MS.yml +364 -17
  211. data/lib/locales/en-NEP.yml +212 -38
  212. data/lib/locales/en-NG.yml +1 -0
  213. data/lib/locales/en-NZ.yml +1153 -123
  214. data/lib/locales/en-PAK.yml +369 -10
  215. data/lib/locales/en-SG.yml +581 -16
  216. data/lib/locales/en-UG.yml +1 -0
  217. data/lib/locales/en-US.yml +6914 -74
  218. data/lib/locales/en-ZA.yml +1 -1
  219. data/lib/locales/en-au-ocker.yml +266 -22
  220. data/lib/locales/es-AR.yml +4570 -0
  221. data/lib/locales/es-MX.yml +1 -2
  222. data/lib/locales/es.yml +2 -3
  223. data/lib/locales/fi-FI.yml +3 -1
  224. data/lib/locales/fr/address.yml +20 -0
  225. data/lib/locales/fr/adjective.yml +266 -0
  226. data/lib/locales/fr/ancient.yml +141 -0
  227. data/lib/locales/fr/animal.yml +5 -0
  228. data/lib/locales/fr/appliance.yml +4 -0
  229. data/lib/locales/fr/book.yml +7 -0
  230. data/lib/locales/fr/color.yml +4 -0
  231. data/lib/locales/fr/company.yml +17 -0
  232. data/lib/locales/fr/compass.yml +23 -0
  233. data/lib/locales/fr/demographic.yml +4 -0
  234. data/lib/locales/fr/gender.yml +6 -0
  235. data/lib/locales/fr/internet.yml +4 -0
  236. data/lib/locales/fr/lorem.yml +5 -0
  237. data/lib/locales/fr/measurement.yml +7 -0
  238. data/lib/locales/fr/name.yml +22 -0
  239. data/lib/locales/fr/phone_number.yml +7 -0
  240. data/lib/locales/fr/pokemon.yml +7 -0
  241. data/lib/locales/fr-CA.yml +2733 -44
  242. data/lib/locales/fr-CH.yml +1 -2
  243. data/lib/locales/fr.yml +2 -118
  244. data/lib/locales/hy.yml +3624 -119
  245. data/lib/locales/id.yml +839 -12
  246. data/lib/locales/it.yml +1304 -32
  247. data/lib/locales/ja/README.md +13 -0
  248. data/lib/locales/ja/address.yml +120539 -0
  249. data/lib/locales/ja/adjective.yml +148 -0
  250. data/lib/locales/ja/ancient.yml +4 -0
  251. data/lib/locales/ja/animal.yml +5 -0
  252. data/lib/locales/ja/bank.yml +4 -0
  253. data/lib/locales/ja/book.yml +7 -0
  254. data/lib/locales/ja/cat.yml +5 -0
  255. data/lib/locales/ja/coffee.yml +4 -0
  256. data/lib/locales/ja/color.yml +4 -0
  257. data/lib/locales/ja/commerce.yml +11 -0
  258. data/lib/locales/ja/company.yml +8 -0
  259. data/lib/locales/ja/dog.yml +6 -0
  260. data/lib/locales/ja/emotion.yml +51 -0
  261. data/lib/locales/ja/food.yml +4 -0
  262. data/lib/locales/ja/football.yml +115 -0
  263. data/lib/locales/ja/gender.yml +4 -0
  264. data/lib/locales/ja/lorem.yml +9 -0
  265. data/lib/locales/ja/name.yml +13 -0
  266. data/lib/locales/ja/naruto.yml +230 -0
  267. data/lib/locales/ja/overwatch.yml +5 -0
  268. data/lib/locales/ja/phone_number.yml +7 -0
  269. data/lib/locales/ja/pokemon.yml +7 -0
  270. data/lib/locales/ja/relationship.yml +10 -0
  271. data/lib/locales/ja/restaurant.yml +11 -0
  272. data/lib/locales/ja/space.yml +5 -0
  273. data/lib/locales/ja/sport.yml +130 -0
  274. data/lib/locales/ja/studio_ghibli.yml +112 -0
  275. data/lib/locales/ja/subscription.yml +8 -0
  276. data/lib/locales/ja/super_mario.yml +9 -0
  277. data/lib/locales/ja/super_smash_bros.yml +8 -0
  278. data/lib/locales/ja/touhou.yml +466 -0
  279. data/lib/locales/ja/university.yml +9 -0
  280. data/lib/locales/ja/zelda.yml +5 -0
  281. data/lib/locales/ko.yml +1361 -20
  282. data/lib/locales/lt.yml +315 -0
  283. data/lib/locales/lv.yml +1 -2
  284. data/lib/locales/mi-NZ.yml +283 -0
  285. data/lib/locales/nb-NO.yml +518 -29
  286. data/lib/locales/nl.yml +1 -1
  287. data/lib/locales/pl.yml +1 -2
  288. data/lib/locales/pt-BR.yml +8157 -660
  289. data/lib/locales/pt.yml +844 -45
  290. data/lib/locales/ru.yml +1355 -32
  291. data/lib/locales/sk.yml +4410 -43
  292. data/lib/locales/sv.yml +100 -0
  293. data/lib/locales/th.yml +76 -76
  294. data/lib/locales/tr.yml +1 -2
  295. data/lib/locales/uk.yml +1808 -44
  296. data/lib/locales/vi.yml +1 -0
  297. data/lib/locales/zh-CN/bank.yml +17 -0
  298. data/lib/locales/zh-CN.yml +1 -0
  299. data/lib/locales/zh-TW.yml +1 -0
  300. metadata +134 -133
  301. data/History.md +0 -176
  302. data/lib/faker/default/fillmurray.rb +0 -45
  303. data/lib/faker/default/lorem_pixel.rb +0 -69
  304. data/lib/locales/ja.yml +0 -105
  305. /data/lib/faker/default/{faker_adjective.rb → adjective.rb} +0 -0
@@ -0,0 +1,480 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ # A high level way to generate a list of generated values that fit a specific
5
+ # format, such as an ID, postal code, or phone number.
6
+ #
7
+ # It provides generators for random digits and letters, hardcoded literal
8
+ # strings, computed values based on previously-generated values, union (one-of)
9
+ # selectors, and grouped generators.
10
+ #
11
+ # The generation allows for dependencies on previously generated values -- most
12
+ # useful for computations -- and this object knows how to build that dependency
13
+ # graph.
14
+ #
15
+ # See {PositionalGenerator::Builder} for more.
16
+ class PositionalGenerator
17
+ ##
18
+ # @param as_type [Symbol] +:string+ to generate a String
19
+ # @param block [Method] a function that interacts with the {Builder}
20
+ def initialize(as_type, &block)
21
+ @block = block
22
+ @generator_builder = Builder.new(as_type)
23
+ end
24
+
25
+ ##
26
+ # @return [String] if +as_type+ is +:string+
27
+ def generate
28
+ @block.call(@generator_builder)
29
+ @generator_builder.build
30
+ end
31
+
32
+ Component = Struct.new(:position, :name, :deps, :generator)
33
+
34
+ class Builder
35
+ attr_reader :as_type
36
+
37
+ def initialize(as_type)
38
+ @components = []
39
+ @as_type = as_type
40
+ end
41
+
42
+ ##
43
+ # Generate a value in the range of 0..9.
44
+ #
45
+ # @param name [Symbol] the name for this node in the group
46
+ # @param length [Integer] how many digits to generate
47
+ # @param ranges [Array<Range, Array, Set>] an array of limitations on the
48
+ # generation. Elements can be a Range to select from within that range,
49
+ # or an Array or Set to select an element from within the list.
50
+ # @return [void]
51
+ #
52
+ # @example a digit
53
+ # int
54
+ #
55
+ # @example five digits named :a
56
+ # int(name: :a, length: 5)
57
+ #
58
+ # @example digits of any length between 4 to 10
59
+ # int(ranges: [1_000 .. 9_999_999_999)
60
+ def int(name: nil, length: 1, ranges: nil)
61
+ @components << Component.new(@components.count, name, [], Int.new(length, ranges))
62
+ end
63
+
64
+ ##
65
+ # Generate a value in the range of 'a'..'Z'.
66
+ #
67
+ # @param name [Symbol] the name for this node in the group
68
+ # @param length [Integer, Range] how many letters to generate
69
+ # @param ranges [Array<Range, Array, Set>] an array of limitations on the
70
+ # generation. Elements can be a Range to select from within that range,
71
+ # or an Array or Set to select an element from within the list.
72
+ # @return [void]
73
+ #
74
+ # @example Generate a letter
75
+ # letter
76
+ #
77
+ # @example Generate five uppercase letters named :b
78
+ # letter(name: :b, length: 5, ranges: ['A'..'Z'])
79
+ #
80
+ # @example Generate three-letter strings from within specific values
81
+ # letter(ranges: ['700'..'799', '7A0'..'7F9'])
82
+ def letter(name: nil, length: 1, ranges: ['a'..'z', 'A'..'Z'])
83
+ @components << Component.new(@components.count, name, [], Letter.new(length, ranges))
84
+ end
85
+
86
+ ##
87
+ # Generate a literal String
88
+ #
89
+ # @param value [String]
90
+ # @param name [Symbol] the name for this node in the group
91
+ # @return [void]
92
+ # @example
93
+ # lit("-")
94
+ def lit(value, name: nil)
95
+ @components << Component.new(@components.count, name, [], Literal.new(value))
96
+ end
97
+
98
+ ##
99
+ # Fill the position with an arbitrary value.
100
+ #
101
+ # @param name [Symbol] the name for this node in the group
102
+ # @param deps [Array<Symbol>] the name of other fields that this one depends on
103
+ # @param block [Method] the block that yields the arbitrary value. Its
104
+ # arguments are the deps.
105
+ # @return [void]
106
+ #
107
+ # @example Today's date
108
+ # computed do
109
+ # Date.today
110
+ # end
111
+ #
112
+ # @example A check digit
113
+ # int(name: :a, length: 5)
114
+ # computed(deps: [:a]) do |a|
115
+ # a.to_s.bytes.sum % 10
116
+ # end
117
+ def computed(name: nil, deps: [], &block)
118
+ @components << Component.new(@components.count, name, deps, Computed.new(block))
119
+ end
120
+
121
+ ##
122
+ # Fill the position with one of the results from the given generators.
123
+ #
124
+ # @param name [Symbol] the name for this node in the group
125
+ # @param block [Method] subgenerator block
126
+ # @return [void]
127
+ #
128
+ # @example Either five digits, or two letters
129
+ # oneof do |or_else|
130
+ # or_else.int(length: 5)
131
+ # or_else.letter(length: 2)
132
+ # end
133
+ #
134
+ # @example Either one letter; or a slash, five digits, then a slash.
135
+ # oneof do |or_else|
136
+ # or_else.letter
137
+ # or_else.group do |g_|
138
+ # g_.lit("/")
139
+ # g_.digit(length: 5)
140
+ # g_.lit("/")
141
+ # end
142
+ # end
143
+ def oneof(name: nil, &block)
144
+ @components << Component.new(@components.count, name, [], Oneof.new(self, block))
145
+ end
146
+
147
+ ##
148
+ # A group of generators. Useful for {#oneof}.
149
+ #
150
+ # @param name [Symbol] the name for this node in the group
151
+ # @param block [Method] a subgenerator block
152
+ # @return [void]
153
+ def group(name: nil, &block)
154
+ @components << Component.new(@components.count, name, [], Group.new(@as_type, block))
155
+ end
156
+
157
+ ##
158
+ # Generate the value.
159
+ #
160
+ # @return [String] if +as_type+ is +:string+
161
+ def build
162
+ graph = build_graph
163
+ stack = build_stack(graph)
164
+ values = generate_values(stack)
165
+ convert(values)
166
+ end
167
+
168
+ private
169
+
170
+ ##
171
+ # Turn the components into a graph following dependencies.
172
+ #
173
+ # @return [Array<(Integer, Integer)>]
174
+ #
175
+ # Components can have dependencies. Here's one where a computation (b)
176
+ # depends on a value generated after it (c):
177
+ #
178
+ # @components = [
179
+ # Int.new(0, :a, 1, nil),
180
+ # Computed.new(1, :b, [:c]) { |c| c + 1 },
181
+ # Int.new(2, :c, 1, nil),
182
+ # ]
183
+ #
184
+ # We can think of a graph like so:
185
+ #
186
+ # (a) (c)
187
+ # | |
188
+ # | (b)
189
+ # \ /
190
+ # end
191
+ #
192
+ # Or in Mermaid:
193
+ #
194
+ # ```mermaid
195
+ # stateDiagram-v2
196
+ # a --> [*]
197
+ # c --> b
198
+ # b --> [*]
199
+ # ```
200
+ #
201
+ # This method builds that graph, using their positional locations as the
202
+ # ID. The end state is represented as +nil+. So continuing the example
203
+ # above, it will give this output:
204
+ #
205
+ # [
206
+ # [0, nil],
207
+ # [2, 1],
208
+ # [1, nil],
209
+ # ]
210
+ #
211
+ # Later we can look up the appropriate component by indexing into the
212
+ # +@components+ array.
213
+ def build_graph
214
+ graph = []
215
+
216
+ # rubocop:disable Style/CombinableLoops
217
+ @components.each do |component|
218
+ component.deps.each do |dep|
219
+ dep_component = @components.detect { |c| c.name == dep }
220
+ raise if dep_component.nil?
221
+
222
+ graph.push([dep_component.position, component.position])
223
+ end
224
+ end
225
+
226
+ @components.each do |component|
227
+ graph.push([component.position, nil]) if graph.none? { |(from, _to)| from == component.position }
228
+ end
229
+ # rubocop:enable Style/CombinableLoops
230
+
231
+ graph
232
+ end
233
+
234
+ ##
235
+ # Produce a stack of components to evaluate in sequence.
236
+ #
237
+ # @param graph [Array<(Integer, Integer)>]
238
+ # @return [Array<Array<Int>>]
239
+ #
240
+ # Now that we have a graph, we know enough to determine how to traverse the
241
+ # generators such that all dependencies are met.
242
+ #
243
+ # The initial stack is an array of all the free traversals to the goal
244
+ # (where the +to+ is +nil+).
245
+ #
246
+ # Loop over the top of the stack:
247
+ # - The next array is all the nodes that lead into the nodes atop the
248
+ # stack.
249
+ # - If the next array has values, push that onto the top of the stack.
250
+ # - If the next array is empty, we are done.
251
+ #
252
+ # For example, given the graph:
253
+ #
254
+ # [
255
+ # [0, nil],
256
+ # [2, 1],
257
+ # [1, nil],
258
+ # ]
259
+ #
260
+ # The initial stack is:
261
+ #
262
+ # [
263
+ # [0, 1]
264
+ # ]
265
+ #
266
+ # We loop over the top of the stack, +[0, 1]+, and find all the nodes of
267
+ # the graph that lead there. Nothing leads to +0+, and +2+ leads to +1+.
268
+ #
269
+ # Therefore, push +[2]+ to the top of the stack.
270
+ #
271
+ # Repeat for +[2]+. Nothing leads to +2+, so our new goal is +[]+. This is
272
+ # empty, so don't push it onto the stack. We are done.
273
+ #
274
+ # The final stack is:
275
+ #
276
+ # [
277
+ # [0, 1],
278
+ # [2]
279
+ # ]
280
+ def build_stack(graph)
281
+ require 'set'
282
+
283
+ terminals = graph.filter_map { |(from, to)| to.nil? && from }
284
+ stack = [terminals]
285
+ seen = Set.new(terminals)
286
+ deps = []
287
+
288
+ loop do
289
+ stack[-1].each do |e|
290
+ deps = graph.select { |(from, to)| to == e && !seen.include?(from) }.map do |from, _to|
291
+ seen << from
292
+ from
293
+ end
294
+ stack << deps if deps.any?
295
+ end
296
+
297
+ break if deps.empty?
298
+ end
299
+
300
+ stack
301
+ end
302
+
303
+ ##
304
+ # Turn a stack into a list of generated values.
305
+ #
306
+ # @param stack [Array<Array<Int>>]
307
+ # @return [Array<Object>] values sorted by desired order
308
+ #
309
+ # We start with a stack of components we need evaluated. We have been
310
+ # tracking these components by position, so first we need to look up the
311
+ # component in our list.
312
+ #
313
+ # From there we can get a list of all the dependencies for the component.
314
+ # These have already been evaluated, since +stack+ is sorted, so we fetch
315
+ # them.
316
+ #
317
+ # Since the stack was sorted by computation order, we must re-sort them
318
+ # into positional order at the end.
319
+ def generate_values(stack)
320
+ result = []
321
+
322
+ while (top = stack.pop)
323
+ top.each do |component_id|
324
+ component = @components[component_id]
325
+ raise if component.nil?
326
+
327
+ values = result.filter_map do |(_id, name, value)|
328
+ value if component.deps.include?(name)
329
+ end
330
+
331
+ result << [component.position, component.name, component.generator.generate(values)]
332
+ end
333
+ end
334
+
335
+ result.sort_by do |component_position, _, _|
336
+ component_position
337
+ end
338
+ end
339
+
340
+ ##
341
+ # @param values [Array<Object>]
342
+ # @return [String] if +@as_type+ is +:string+
343
+ # @raise [ArgumentError] if +@as_type+ is unsupported
344
+ def convert(values)
345
+ case @as_type
346
+ when :string
347
+ values.inject('') do |acc, (_, _, v)|
348
+ "#{acc}#{v}"
349
+ end
350
+ else
351
+ raise ArgumentError, "unknown return type: #{@as_type}"
352
+ end
353
+ end
354
+
355
+ class Group
356
+ def initialize(as_type, block)
357
+ @as_type = as_type
358
+ @block = block
359
+ end
360
+
361
+ def generate(_)
362
+ builder = Builder.new(@as_type)
363
+ @block.call(builder)
364
+ builder.build
365
+ end
366
+ end
367
+
368
+ class Oneof
369
+ def initialize(builder, block)
370
+ @block = block
371
+ @builder = builder
372
+ end
373
+
374
+ def generate(...)
375
+ subgens = OneofSelector.new(@builder)
376
+ @block.call(subgens)
377
+ subgens.sample
378
+ subgens.generate(...)
379
+ end
380
+
381
+ class OneofSelector
382
+ def initialize(builder)
383
+ @subgens = []
384
+ @builder = Builder.new(builder.as_type)
385
+ end
386
+
387
+ def method_missing(meth, *args, **kwargs, &block)
388
+ @subgens << [meth, args, kwargs, block]
389
+ end
390
+
391
+ def respond_to_missing?(method_name, include_private = false)
392
+ @builder.respond_to?(method_name, include_private)
393
+ end
394
+
395
+ def sample
396
+ (meth, args, kwargs, block) = Faker::Base.sample(@subgens)
397
+ @builder.send(meth, *args, **kwargs, &block)
398
+ end
399
+
400
+ def generate(...)
401
+ @builder.build
402
+ end
403
+ end
404
+ end
405
+
406
+ class Int
407
+ def initialize(length, ranges)
408
+ # Internally we store only an Enumerable of Range values. So if we are
409
+ # not given any Ranges but are given a length, we need to convert the
410
+ # length to a Range.
411
+ #
412
+ # If the length is `5`, that means we should compute the Range `10000..99999`.
413
+ # We can compute the lower end with a simple exponent: 10^4 = 10000.
414
+ # The upper end is one less than an exponent: 10^5 - 1 = 99999.
415
+ if ranges.nil?
416
+ lower = 10**(length - 1)
417
+ upper = (10**length) - 1
418
+ ranges = [lower..upper]
419
+ end
420
+
421
+ @ranges = ranges
422
+ end
423
+
424
+ def generate(_)
425
+ Faker::Base.rand(@ranges.sample(random: Faker::Config.random))
426
+ end
427
+ end
428
+
429
+ class Letter
430
+ def initialize(length, ranges)
431
+ @length = length
432
+ @ranges = ranges
433
+ end
434
+
435
+ def generate(_)
436
+ @length.times.inject('') do |acc, _index|
437
+ generated_character = char
438
+ "#{acc}#{generated_character}"
439
+ end
440
+ end
441
+
442
+ private
443
+
444
+ def char
445
+ if @ranges
446
+ case s = @ranges.sample(random: Faker::Config.random)
447
+ when Range
448
+ s.to_a.sample(random: Faker::Config.random)
449
+ when Array, Set
450
+ s.sample(random: Faker::Config.random)
451
+ else
452
+ raise ArgumentError, "unsupported range type: #{s.inspect}"
453
+ end
454
+ else
455
+ Faker::Base.sample(Faker::Base::Letters)
456
+ end
457
+ end
458
+ end
459
+
460
+ class Literal
461
+ def initialize(value)
462
+ @value = value
463
+ end
464
+
465
+ def generate(_)
466
+ @value
467
+ end
468
+ end
469
+
470
+ class Computed
471
+ def initialize(block)
472
+ @block = block
473
+ end
474
+
475
+ def generate(args)
476
+ @block.call(*args)
477
+ end
478
+ end
479
+ end
480
+ end
@@ -2,35 +2,25 @@
2
2
 
3
3
  module Faker
4
4
  class UniqueGenerator
5
- @marked_unique = Set.new # Holds names of generators with unique values
6
-
7
- class << self
8
- attr_reader :marked_unique
9
- end
10
-
11
5
  def initialize(generator, max_retries)
12
6
  @generator = generator
13
7
  @max_retries = max_retries
14
- @previous_results = Hash.new { |hash, key| hash[key] = Set.new }
15
8
  end
16
9
 
17
- # rubocop:disable Lint/MissingSuper
18
10
  def method_missing(name, *arguments)
19
11
  self.class.marked_unique.add(self)
20
12
 
21
13
  @max_retries.times do
22
14
  result = @generator.public_send(name, *arguments)
23
15
 
24
- next if @previous_results[[name, arguments]].include?(result)
16
+ next if previous_results[[name, arguments]].include?(result)
25
17
 
26
- @previous_results[[name, arguments]] << result
18
+ previous_results[[name, arguments]] << result
27
19
  return result
28
20
  end
29
21
 
30
22
  raise RetryLimitExceeded, "Retry limit exceeded for #{name}"
31
23
  end
32
- # rubocop:enable Lint/MissingSuper
33
-
34
24
  # Have method_missing use ruby 2.x keywords if the method exists.
35
25
  # This is necessary because the syntax for passing arguments (`...`)
36
26
  # is invalid on versions before Ruby 2.7, so it can't be used.
@@ -42,8 +32,17 @@ module Faker
42
32
 
43
33
  RetryLimitExceeded = Class.new(StandardError)
44
34
 
35
+ def previous_results
36
+ Thread.current[:faker_unique_generator_previous_results] ||= {}
37
+ Thread.current[:faker_unique_generator_previous_results][@generator] ||= Hash.new { |hash, key| hash[key] = Set.new }
38
+ end
39
+
45
40
  def clear
46
- @previous_results.clear
41
+ previous_results.clear
42
+ end
43
+
44
+ def self.marked_unique
45
+ Thread.current[:faker_unique_generator_marked_unique] ||= Set.new
47
46
  end
48
47
 
49
48
  def self.clear
@@ -54,7 +53,7 @@ module Faker
54
53
  def exclude(name, arguments, values)
55
54
  values ||= []
56
55
  values.each do |value|
57
- @previous_results[[name, arguments]] << value
56
+ previous_results[[name, arguments]] << value
58
57
  end
59
58
  end
60
59
  end
@@ -9,7 +9,7 @@ Here's how to set it:
9
9
  Faker::Config.locale = 'zh-CN'
10
10
  ```
11
11
 
12
- 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.
12
+ 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.
13
13
 
14
14
  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.
15
15
 
@@ -18,7 +18,9 @@ Faker::Config.locale = 'zh-CN'
18
18
  Faker::Hipster.word #=> "kogi"
19
19
  ```
20
20
 
21
- 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.
21
+ ## How to update a locale with more translations
22
+
23
+ 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.
22
24
 
23
25
  ```yaml
24
26
  # /lib/locales/zh-CN.yml
@@ -38,3 +40,17 @@ In our hypothetical example here, one would add something like this to the "test
38
40
  ```ruby
39
41
  assert Faker::Hipster.word.is_a? String
40
42
  ```
43
+
44
+ ## How to set the default locale for in threaded server environments
45
+
46
+ If you want to modify the default locale that will be used in new threads, set it in your configuration:
47
+
48
+ ```ruby
49
+ Faker::Config.default_locale = :pt
50
+ ```
51
+
52
+ In threaded server environments, e.g., Puma, locale per thread can be set as the following:
53
+
54
+ ```ruby
55
+ Faker::Config.locale = :es
56
+ ```
data/lib/locales/ar.yml CHANGED
@@ -25,6 +25,7 @@ ar:
25
25
  - "#{street_address}, #{city}"
26
26
  - "#{secondary_address} #{street_address}, #{city}"
27
27
  default_country: [المملكة العربية السعودية]
28
+ default_country_code: ["SA"]
28
29
  company:
29
30
  suffix: [شركة, وأبناؤه, المحدودة]
30
31
  buzzwords:
@@ -51,7 +52,11 @@ ar:
51
52
  - "#{University.prefix} #{Address.state} #{University.suffix}"
52
53
 
53
54
  name:
54
- first_name: [عمر, محمّد, أحمد, عبدالله, عبدالرحمن, عبدالرزّاق, عبدالصّمد, نوّاف , خالد, البراء, إسراء, فاطمة, مريم, سميّة, فوز, فوزية, خديجة, الخنساء, حنان, عبير, أنس, مالك, أبيّ, وائل, مشعل, وائل, أبو بكر, عمرو, عمّار, غدير, غادة, إيمان, نوف, خديجة, عائشة, عاتكة, شمس, دانة, ألهام, فهد, نايف, عبدالعزيز, عبدالمحسن, مهنّد, مصعب, حاتم, عبدالوهّاب, همّام, حمزة, خولة, خلود, ابتهال, أمة الله, صفية, خالدة, رابعة, بدوية, رؤية, سندس]
55
+ male_first_name: [حمزة, خالد, نوّاف, عبدالصّمد, عبدالرزّاق, عبدالرحمن, عبدالله, أحمد, محمّد, عمر, نايف, فهد, عمّار, عمرو, أبو بكر, وائل, وائل, مالك, عبدالوهّاب, حاتم, مصعب, مهنّد, عبدالمحسن, عبدالعزيز]
56
+ female_first_name: [عبير, حنان, الخنساء, خديجة, فوزية, فوز, سميّة, مريم, فاطمة, إسراء, ألهام, دانة, شمس, عائشة, خديجة, نوف, إيمان, غادة, غدير, سندس, رؤية, بدوية, رابعة, خالدة, صفية, ابتهال, خلود, خولة]
57
+ first_name:
58
+ - "#{female_first_name}"
59
+ - "#{male_first_name}"
55
60
  last_name: [الخالدي, العمريّ, القادريّ, التميمي, القحطاني, الأسمري, الأخضر, الزهراني, الغامدي, المالكيّ, مجدي, أبو ياسين, أبو خالد, إمام, نصريّ, الحكيم, الأمير, حسني, زكي, الجزايرلي, القلعاوي, الشريف, حلمي, الجريتلي, رشدي, شلبي, عيد, شاهين, المهندس, زايد, المهنّا, شوكت, سعيد, الصعيدي, فتحي, الصنعاني, العديني, بامحمّد, بامحفوظ, باسهل, العمودي, باصهيب, الدوعني, السقّاف, العطّاس, فاروق, خيّاط, كوسة, اللاحم, النابلسي, الغزاوي]
56
61
  prefix: [السيّد, السيّدة, الآنسة, الدكتور]
57
62
  name:
data/lib/locales/bg.yml CHANGED
@@ -15,9 +15,8 @@ bg:
15
15
  street_address:
16
16
  - "#{street_name}, #{building_number}"
17
17
  default_country: [България]
18
-
18
+ default_country_code: ["BG"]
19
19
  internet:
20
- free_email: [abv.bg, mail.bg, dir.bg, gmail.com, yahoo.com, hotmail.com]
21
20
  domain_suffix: [com, bg, info, бг, net, org]
22
21
 
23
22
  name: