Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/faker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def regexify(reg)
# with an array of values and selecting one of them.
def fetch(key)
fetched = sample(translate("faker.#{key}"))
if fetched&.match(%r{^/}) && fetched.match(%r{/$}) # A regex
if fetched.is_a?(::String) && fetched.start_with?('/') && fetched.end_with?('/') # A regex
regexify(fetched)
else
fetched
Expand All @@ -133,7 +133,7 @@ def fetch(key)
def fetch_all(key)
fetched = translate("faker.#{key}")
fetched = fetched.last if fetched.size <= 1
if !fetched.respond_to?(:sample) && fetched.match(%r{^/}) && fetched.match(%r{/$}) # A regex
if !fetched.respond_to?(:sample) && fetched.is_a?(::String) && fetched.start_with?('/') && fetched.end_with?('/') # A regex
regexify(fetched)
else
fetched
Expand All @@ -146,6 +146,9 @@ def fetch_all(key)
def parse(key)
fetched = fetch(key)

# A plain value cannot contain any tokens, so skip scanning for them
return numerify(fetched) unless fetched.include?('#{')

parts = fetched.scan(/(\(?)#\{([A-Za-z]+\.)?([^}]+)\}([^#]++)?/).map do |prefix, kls, meth, etc|
# If the token had a class Prefix (e.g., Name.first_name)
# grab the constant, otherwise use self
Expand Down
7 changes: 2 additions & 5 deletions lib/faker/default/name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ def name_with_middle
#
# @faker.version 0.9.0
def first_name
if parse('name.first_name').empty?
fetch('name.first_name')
else
parse('name.first_name')
end
parsed = parse('name.first_name')
parsed.empty? ? fetch('name.first_name') : parsed
end

##
Expand Down