faker 3.2.0 → 3.2.3

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.
@@ -0,0 +1,230 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class HTML < Base
5
+ class << self
6
+ ##
7
+ # Produces a random HTML header format.
8
+ #
9
+ # @return [String]
10
+ #
11
+ # @example
12
+ # Faker::HTML.heading #=> "<h5>Autem</h5>"
13
+ #
14
+ # @faker.version 3.2.1
15
+ def heading
16
+ level = rand(1..6)
17
+ "<h#{level}>#{Lorem.word.capitalize}</h#{level}>"
18
+ end
19
+
20
+ ##
21
+ # Produces a random HTML paragraph format.
22
+ #
23
+ # @param sentence_count [Integer] The number of sentences in the paragraph.
24
+ # @param supplemental [Boolean] Include supplemental text.
25
+ # @param random_sentences_to_add [Integer] The number of random sentences to add to the paragraph.
26
+ # @param exclude_words [Array<String>] Words to exclude from the generated paragraph.
27
+ # @return [String]
28
+ #
29
+ # @example
30
+ # Faker::HTML.paragraph #=> "<p>Incidunt atque quis</p>"
31
+ #
32
+ # @faker.version 3.2.1
33
+ def paragraph(sentence_count: 3, supplemental: false, random_sentences_to_add: 0, exclude_words: nil)
34
+ "<p>#{Faker::Lorem.paragraph(sentence_count: sentence_count, supplemental: supplemental, random_sentences_to_add: random_sentences_to_add, exclude_words: exclude_words)}</p>"
35
+ end
36
+
37
+ ##
38
+ # Produces a random emphasis formatting on a random word in two HTML paragraphs.
39
+ #
40
+ # @return [String]
41
+ #
42
+ # @example
43
+ # Faker::HTML.emphasis #=> "<em>repellat id impedit</em>"
44
+ #
45
+ # @faker.version 3.2.1
46
+ def emphasis
47
+ "<em>#{Faker::Lorem.paragraph(sentence_count: 1)}</em>"
48
+ end
49
+
50
+ ##
51
+ # Produces a random ordered list in HTML format, with at least one element.
52
+ #
53
+ # @return [String]
54
+ #
55
+ # @example
56
+ # Faker::HTML.ordered_list #=> "<ol>\n<li>Qui reiciendis non consequatur atque.</li>\n<li>Quo doloremque veritatis tempora aut.</li>\n<li>Aspernatur.</li>\n<li>Ea ab.</li>\n<li>Qui.</li>\n<li>Sit pariatur nemo eveniet.</li>\n<li>Molestiae aut.</li>\n<li>Nihil molestias iure placeat.</li>\n<li>Dolore autem quisquam.</li>\n</ol>"
57
+ #
58
+ # @faker.version 3.2.1
59
+ def ordered_list
60
+ number = rand(1..10)
61
+
62
+ items = []
63
+ number.times do
64
+ items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
65
+ end
66
+
67
+ "<ol>\n#{items.join("\n")}\n</ol>"
68
+ end
69
+
70
+ ##
71
+ # Produces a random unordered list of items between 1 and 10 randomly in HTML format.
72
+ #
73
+ # @return [String]
74
+ #
75
+ # @example
76
+ # Faker::HTML.unordered_list #=> "<ul>\n<li>Voluptatum aliquid tempora molestiae facilis non sed.</li>\n<li>Nostrum omnis iste impedit voluptatum dolor.</li>\n<li>Esse quidem et facere.</li>\n</ul>"
77
+ #
78
+ # @faker.version 3.2.1
79
+ def unordered_list
80
+ number = rand(1..10)
81
+
82
+ items = []
83
+ number.times do
84
+ items << "<li>#{Faker::Lorem.sentence(word_count: 1)}</li>"
85
+ end
86
+
87
+ "<ul>\n#{items.join("\n")}\n</ul>"
88
+ end
89
+
90
+ ##
91
+ # Produces a random code block formatted in HTML.
92
+ #
93
+ # @return [String]
94
+ #
95
+ # @example
96
+ # Faker::HTML.code #=> "<code>Eos quasi qui.</code>"
97
+ #
98
+ # @faker.version 3.2.1
99
+ def code
100
+ "<code>#{Lorem.sentence(word_count: 1)}</code>"
101
+ end
102
+
103
+ ##
104
+ # Produces a random HTML table.
105
+ #
106
+ # @return [String]
107
+ #
108
+ # @example
109
+ # Faker::HTML.table #=> "<table>\n<thead>\n<th>ad</th>\n<th>similique</th>\n<th>voluptatem</th>\n</thead>\n<tbody>\n<td>corrupti</td>\n<td>est</td>\n<td>rerum</td>\n<td>molestiae</td>\n<td>quidem</td>\n<td>et</td>\n<td>in</td>\n<td>tempora</td>\n<td>at</td>\n<\tbody>\n<tfoot>\n<td>voluptatem</td>\n<td>debitis</td>\n<td>rem</td>\n</tfoot>\n</table>"
110
+ #
111
+ # @faker.version 3.2.1
112
+ def table
113
+ header_row = generate_table_row('th', 3)
114
+ footer_row = generate_table_row('td', 3)
115
+
116
+ body_rows = []
117
+ 3.times do
118
+ row = generate_table_row('td', 3)
119
+ body_rows << row
120
+ end
121
+
122
+ thead = "<thead>\n#{header_row}</thead>"
123
+ tbody = "<tbody>\n#{body_rows.join("\n")}</tbody>"
124
+ tfoot = "<tfoot>\n#{footer_row}</tfoot>"
125
+
126
+ "<table>\n#{thead}\n#{tbody}\n#{tfoot}\n</table>"
127
+ end
128
+
129
+ ##
130
+ # Generates a random <script> tag with the `src` attribute set to a random URL.
131
+ #
132
+ # @return [String]
133
+ #
134
+ # @example
135
+ # Faker::HTML.script #=> "<script src=\"http://gulgowski.name/jordan.weimann.js\"></script>"
136
+ #
137
+ # @faker.version 3.2.1
138
+ def script
139
+ "<script src=\"#{Faker::Internet.url}.js\"></script>"
140
+ end
141
+
142
+ ##
143
+ # Generates a random <link> tag with the `rel` attribute set to "stylesheet" and the `href` attribute set to a random URL.
144
+ #
145
+ # @param rel [String] The rel of the link tag.
146
+ # @return [String]
147
+ #
148
+ # @example
149
+ # Faker::HTML.link #=> "<link rel=\"stylesheet\" href=\"http://fay.io/darryl.barrows.css\">"
150
+ #
151
+ # @faker.version 3.2.1
152
+ def link(rel: 'stylesheet')
153
+ "<link rel=\"#{rel}\" href=\"#{Faker::Internet.url}.css\">"
154
+ end
155
+
156
+ ##
157
+ # Generates HTML content with customizable attributes for any HTML tag.
158
+ #
159
+ # @param tag [String] The HTML tag to generate.
160
+ # @param content [String] The Content of the HTML tag.
161
+ # @param attributes [Hash] The attributes to include in the tag.
162
+ # @return [String]
163
+ #
164
+ # @example
165
+ # Faker::HTML.element(tag: 'div', content: "This is a div with XSS attributes.", attributes: {class: 'xss', onclick: "alert('XSS')"}) #=> "<div class=\"xss\" onclick=\"alert('XSS')\">This is a div with XSS attributes.</div>"
166
+ #
167
+ # @faker.version 3.2.1
168
+ def element(tag: 'div', content: Lorem.sentence(word_count: 3), attributes: { class: Lorem.word, onclick: "#{Lorem.word}()" })
169
+ attribute_string = attributes.map { |key, value| "#{key}=\"#{value}\"" }.join(' ')
170
+ "<#{tag} #{attribute_string}>#{content}</#{tag}>"
171
+ end
172
+
173
+ ##
174
+ # Produces a random method from the methods above, excluding the methods listed in the arguments.
175
+ #
176
+ # @overload random(methods)
177
+ # @param methods [Symbol] Specify which methods to exclude.
178
+ #
179
+ # @return [String]
180
+ #
181
+ # @example
182
+ # Faker::HTML.random #=> returns output from a single method outlined above
183
+ # Faker::HTML.random(exclude: [:table]) #=> returns output from any single method outlined above except for "table"
184
+ # Faker::HTML.random(exclude: [:ordered_list, :unordered_list]) #=> returns output from any single method outlined above except for either ordered_list and unordered_list
185
+ #
186
+ # @faker.version 3.2.1
187
+ def random(exclude: [])
188
+ method_list = available_methods
189
+ exclude.each { |ex| method_list.delete_if { |meth| meth == ex.to_sym } }
190
+ send(method_list[Faker::Config.random.rand(0..method_list.length - 1)])
191
+ end
192
+
193
+ ##
194
+ # Generates a random HTML content sandwich, starting with a header, followed by paragraphs, and random elements.
195
+ #
196
+ # @param sentences [Integer] The number of sentences in each paragraph.
197
+ # @param repeat [Integer] The number of times to repeat the pattern (header, paragraph, random).
198
+ # @return [String]
199
+ #
200
+ # @example
201
+ # Faker::HTML.sandwich(sentences: 3, repeat: 2) #=> returns a sandwich of HTML content with 2 repetitions, each having a header, paragraph, and random element
202
+ #
203
+ # @faker.version 3.2.1
204
+ def sandwich(sentences: 3, repeat: 1)
205
+ text_block = []
206
+ text_block << heading
207
+ repeat.times do
208
+ text_block << paragraph(sentence_count: sentences)
209
+ text_block << random(exclude: %i[script link])
210
+ end
211
+ text_block.join("\n")
212
+ end
213
+
214
+ private
215
+
216
+ def available_methods
217
+ (HTML.public_methods(false) - Base.methods).sort
218
+ end
219
+
220
+ def generate_table_row(tag, cell_count)
221
+ row = "<tr>\n"
222
+ cell_count.times do
223
+ row += "<#{tag == 'th' ? 'th' : 'td'}>#{Lorem.word}</#{tag == 'th' ? 'th' : 'td'}>\n"
224
+ end
225
+ row += "</tr>\n"
226
+ row
227
+ end
228
+ end
229
+ end
230
+ end
@@ -46,9 +46,23 @@ module Faker
46
46
  end
47
47
 
48
48
  def ssn_valid
49
- ssn = regexify(/[0-8]\d{2}-\d{2}-\d{4}/)
50
- # We could still have all 0s in one segment or another
51
- INVALID_SSN.any? { |regex| regex =~ ssn } ? ssn_valid : ssn
49
+ generate(:string) do |g|
50
+ g.computed(name: :first) do
51
+ range = [1..665, 667..899].sample(random: Faker::Config.random)
52
+ n = Faker::Base.rand(range)
53
+ format('%03d', n)
54
+ end
55
+ g.lit('-')
56
+ g.computed(name: :second) do
57
+ n = Faker::Base.rand(1..99)
58
+ format('%02d', n)
59
+ end
60
+ g.lit('-')
61
+ g.computed(name: :third) do
62
+ n = Faker::Base.rand(1..9999)
63
+ format('%04d', n)
64
+ end
65
+ end
52
66
  end
53
67
 
54
68
  ##
@@ -159,7 +159,8 @@ module Faker
159
159
  #
160
160
  # @faker.version 2.1.3
161
161
  def password(min_length: 8, max_length: 16, mix_case: true, special_characters: false)
162
- raise ArgumentError, 'max_length must be more than min_length' if max_length < min_length
162
+ raise ArgumentError, 'min_length and max_length must be greater than or equal to one' if min_length < 1 || max_length < 1
163
+ raise ArgumentError, 'min_length must be smaller than or equal to max_length' unless min_length <= max_length
163
164
 
164
165
  character_types = []
165
166
  required_min_length = 0
@@ -182,27 +183,27 @@ module Faker
182
183
  character_bag = []
183
184
 
184
185
  # use lower_chars by default and add upper_chars if mix_case
185
- lower_chars = ('a'..'z').to_a
186
- password << lower_chars[rand(lower_chars.count - 1)]
186
+ lower_chars = self::LLetters
187
+ password << sample(lower_chars)
187
188
  character_bag += lower_chars
188
189
 
189
- digits = ('1'..'9').to_a
190
- password << digits[rand(digits.count - 1)]
190
+ digits = ('0'..'9').to_a
191
+ password << sample(digits)
191
192
  character_bag += digits
192
193
 
193
- if character_types.include?(:mix_case)
194
- upper_chars = ('A'..'Z').to_a
195
- password << upper_chars[rand(upper_chars.count - 1)]
194
+ if mix_case
195
+ upper_chars = self::ULetters
196
+ password << sample(upper_chars)
196
197
  character_bag += upper_chars
197
198
  end
198
199
 
199
- if character_types.include?(:special_characters)
200
+ if special_characters
200
201
  special_chars = %w[! @ # $ % ^ & *]
201
- password << special_chars[rand(special_chars.count - 1)]
202
+ password << sample(special_chars)
202
203
  character_bag += special_chars
203
204
  end
204
205
 
205
- password << character_bag[rand(character_bag.count - 1)] while password.length < target_length
206
+ password << sample(character_bag) while password.length < target_length
206
207
 
207
208
  shuffle(password).join
208
209
  end
@@ -10,10 +10,13 @@ module Faker
10
10
  #
11
11
  # @example
12
12
  # Faker::Lorem.word #=> "soluto"
13
+ # Faker::Lorem.word(exclude_words: 'error') #=> "nisi"
14
+ # Faker::Lorem.word(exclude_words: 'id, error') #=> "et"
15
+ # Faker::Lorem.word(exclude_words: ['id', 'error']) #=> "consequatur"
13
16
  #
14
17
  # @faker.version 2.1.3
15
- def word
16
- sample(translate('faker.lorem.words'))
18
+ def word(exclude_words: nil)
19
+ words(number: 1, exclude_words: exclude_words).first
17
20
  end
18
21
 
19
22
  ##
@@ -97,12 +97,13 @@ module Faker
97
97
  # @example
98
98
  # Faker::Types.rb_array #=> ["a"]
99
99
  # Faker::Types.rb_array(len: 4) #=> ["a", 1, 2, "bob"]
100
+ # Faker::Types.rb_array(len: 2, type: -> { Faker::Types.rb_string }) #=> ["cat", "foo"]
100
101
  #
101
102
  # @faker.version 1.8.6
102
- def rb_array(len: 1)
103
+ def rb_array(len: 1, type: -> { random_type })
103
104
  [].tap do |ar|
104
105
  len.times do
105
- ar.push random_type
106
+ ar.push type.is_a?(Proc) ? type.call : type
106
107
  end
107
108
  end
108
109
  end
@@ -22,14 +22,25 @@ module Faker
22
22
  #
23
23
  # @faker.version 1.6.4
24
24
  def vin
25
- front = 8.times.map { VIN_KEYSPACE.sample(random: Faker::Config.random) }.join
26
- back = 8.times.map { VIN_KEYSPACE.sample(random: Faker::Config.random) }.join
27
- checksum = "#{front}A#{back}".chars.each_with_index.map do |char, i|
28
- value = (char =~ /\A\d\z/ ? char.to_i : VIN_TRANSLITERATION[char.to_sym])
29
- value * VIN_WEIGHT[i]
30
- end.inject(:+) % 11
31
- checksum = 'X' if checksum == 10
32
- "#{front}#{checksum}#{back}"
25
+ generate(:string) do |g|
26
+ g.letter(name: :wmi, ranges: ['100'..'199', '400'..'499', '500'..'599', '700'..'799', '7A0'..'7F9'])
27
+ g.letter(name: :vds, length: 5, ranges: [VIN_KEYSPACE])
28
+ g.computed(name: :checksum, deps: %i[wmi vds model_year plant_code vis]) do |wmi, vds, model_year, plant_code, vis|
29
+ checksum = "#{wmi}#{vds}0#{model_year}#{plant_code}#{vis}".chars.each_with_index.map do |char, i|
30
+ value = (char =~ /\A\d\z/ ? char.to_i : VIN_TRANSLITERATION[char.to_sym])
31
+ value * VIN_WEIGHT[i]
32
+ end.inject(:+) % 11
33
+
34
+ if checksum == 10
35
+ 'X'
36
+ else
37
+ checksum
38
+ end
39
+ end
40
+ g.letter(name: :model_year, length: 1, ranges: [VIN_KEYSPACE - %w[U Z 0]])
41
+ g.letter(name: :plant_code, length: 1, ranges: [VIN_KEYSPACE])
42
+ g.int(name: :vis, length: 6)
43
+ end
33
44
  end
34
45
 
35
46
  # Produces a random vehicle manufacturer.
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Faker
4
+ class Games
5
+ class FinalFantasyXIV < Base
6
+ class << self
7
+ ##
8
+ # Produces the name of a character from FFXIV.
9
+ #
10
+ # @return [String]
11
+ #
12
+ # @example
13
+ # Faker::Games::FinalFantasyXIV.character #=> "Y'shtola Rhul"
14
+ #
15
+ # @faker.version next
16
+ def character
17
+ fetch('games.final_fantasy_xiv.characters')
18
+ end
19
+
20
+ ##
21
+ # Produces a job name from Final Fantasy XIV. Either a battle or non-battle playable job.
22
+ #
23
+ # @return [String]
24
+ #
25
+ # @example
26
+ # Faker::Games::FinalFantasyXIV.job #=> "Paladin"
27
+ #
28
+ # @faker.version next
29
+ def job
30
+ fetch('games.final_fantasy_xiv.jobs')
31
+ end
32
+
33
+ # Produces the name of a playable race from Final Fantasy XIV.
34
+ #
35
+ # @return [String]
36
+ #
37
+ # @example
38
+ # Faker::Games::FinalFantasyXIV.race #=> "Miqo'te"
39
+ #
40
+ # @faker.version next
41
+ def race
42
+ fetch('games.final_fantasy_xiv.races')
43
+ end
44
+
45
+ ##
46
+ # Produces a data center from Final Fantasy XIV.
47
+ #
48
+ # @return [String]
49
+ #
50
+ # @example
51
+ # Faker::Games::FinalFantasyXIV.data_center #=> "Aether"
52
+ #
53
+ # @faker.version next
54
+ def data_center
55
+ fetch('games.final_fantasy_xiv.data_centers')
56
+ end
57
+
58
+ ##
59
+ # Produces a geographical zone from Final Fantasy XIV.
60
+ #
61
+ # @return [String]
62
+ #
63
+ # @example
64
+ # Faker::Games::FinalFantasyXIV.zone #=> "Eastern La Noscea"
65
+ #
66
+ # @faker.version next
67
+ def zone
68
+ fetch('games.final_fantasy_xiv.zones')
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -11,7 +11,7 @@ module Faker
11
11
  #
12
12
  # @param region [String] airport region, currently available -> united_states or european_union
13
13
  #
14
- # @retrun [String]
14
+ # @return [String]
15
15
  #
16
16
  # @example
17
17
  # Faker::Travel::Airport.name(size: 'large', region: 'united_states') => "Los Angeles International Airport"
@@ -28,7 +28,7 @@ module Faker
28
28
  #
29
29
  # @param region [String] airport region, currently available -> united_states or european_union
30
30
  #
31
- # @retrun [String]
31
+ # @return [String]
32
32
  #
33
33
  # @example
34
34
  # Faker::Travel::Airport.iata(size: 'large', region: 'united_states') => "LAX"
@@ -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
@@ -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
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.2.0'
4
+ VERSION = '3.2.3'
5
5
  end
data/lib/faker.rb CHANGED
@@ -11,14 +11,18 @@ I18n.load_path += Dir[File.join(mydir, 'locales', '**/*.yml')]
11
11
 
12
12
  module Faker
13
13
  module Config
14
+ @default_locale = nil
15
+
14
16
  class << self
17
+ attr_writer :default_locale
18
+
15
19
  def locale=(new_locale)
16
20
  Thread.current[:faker_config_locale] = new_locale
17
21
  end
18
22
 
19
23
  def locale
20
24
  # Because I18n.locale defaults to :en, if we don't have :en in our available_locales, errors will happen
21
- 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)
22
26
  end
23
27
 
24
28
  def own_locale
@@ -48,19 +52,23 @@ module Faker
48
52
 
49
53
  ## by default numerify results do not start with a zero
50
54
  def numerify(number_string, leading_zero: false)
51
- return number_string.gsub(/#/) { rand(10).to_s } if leading_zero
55
+ return number_string.gsub('#') { rand(10).to_s } if leading_zero
52
56
 
53
- 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 }
54
58
  end
55
59
 
56
60
  def letterify(letter_string)
57
- letter_string.gsub(/\?/) { sample(ULetters) }
61
+ letter_string.gsub('?') { sample(ULetters) }
58
62
  end
59
63
 
60
64
  def bothify(string)
61
65
  letterify(numerify(string))
62
66
  end
63
67
 
68
+ def generate(as_type, &block)
69
+ PositionalGenerator.new(as_type, &block).generate
70
+ end
71
+
64
72
  # Given a regular expression, attempt to generate a string
65
73
  # that would match it. This is a rather simple implementation,
66
74
  # so don't be shocked if it blows up on you in a spectacular fashion.
@@ -84,7 +92,7 @@ module Faker
84
92
  reg = reg.source if reg.respond_to?(:source) # Handle either a Regexp or a String that looks like a Regexp
85
93
  reg
86
94
  .gsub(%r{^/?\^?}, '').gsub(%r{\$?/?$}, '') # Ditch the anchors
87
- .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}
88
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]
89
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)
90
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