mirror of
https://github.com/askn/faker.git
synced 2023-08-10 21:13:01 +03:00
add fetch
This commit is contained in:
parent
6a2833f187
commit
36fe59b671
12
README.md
12
README.md
@ -58,7 +58,7 @@ Faker::Address.latitude #=> "-58.17256227443719"
|
||||
Faker::Address.longitude #=> "-156.65548382095133"
|
||||
```
|
||||
|
||||
### Faker::Business
|
||||
### Faker::Business
|
||||
------------------
|
||||
|
||||
```crystal
|
||||
@ -72,7 +72,7 @@ Faker::Business.credit_card_type #=> "visa"
|
||||
```
|
||||
|
||||
|
||||
### Faker::Commerce
|
||||
### Faker::Commerce
|
||||
|
||||
```crystal
|
||||
|
||||
@ -85,7 +85,7 @@ Faker::Commerce.price #=> "44.6"
|
||||
|
||||
```
|
||||
|
||||
### Faker::Company
|
||||
### Faker::Company
|
||||
|
||||
```crystal
|
||||
|
||||
@ -96,7 +96,7 @@ Faker::Company.suffix #=> "Group"
|
||||
```
|
||||
|
||||
|
||||
### Faker::Internet
|
||||
### Faker::Internet
|
||||
|
||||
```crystal
|
||||
# Optional argument name=nil
|
||||
@ -146,7 +146,7 @@ Faker::Internet.slug('foo bar', '-') #=> "foo-bar"
|
||||
|
||||
```
|
||||
|
||||
### Faker::Lorem
|
||||
### Faker::Lorem
|
||||
|
||||
```crystal
|
||||
|
||||
@ -216,7 +216,7 @@ Faker::Number.digit #=> "1"
|
||||
|
||||
```
|
||||
|
||||
### Faker::PhoneNumber
|
||||
### Faker::PhoneNumber
|
||||
|
||||
```crystal
|
||||
|
||||
|
File diff suppressed because one or more lines are too long
60
src/faker.cr
60
src/faker.cr
@ -29,7 +29,63 @@ module Faker
|
||||
end
|
||||
|
||||
def self.fetch(data)
|
||||
d = data as Array
|
||||
d.sample
|
||||
data = data as Array
|
||||
fetched = data.sample as String
|
||||
if fetched.match(/^\//) && fetched.match(/\/$/) # A regex
|
||||
fetched = Faker.regexify(fetched)
|
||||
end
|
||||
|
||||
Faker.parse(fetched) as String
|
||||
end
|
||||
|
||||
def self.parse(st)
|
||||
st.gsub(/%\{([^\}]+)\}/) do |str, matches|
|
||||
# find_fn([Name.name, Name.first_name], $1)
|
||||
find_fnx($1)
|
||||
end
|
||||
end
|
||||
|
||||
# macro find_fn(list, fn)
|
||||
# case {{fn}}
|
||||
# {% for l in list %}
|
||||
# when "{{l}}"
|
||||
# {{l}}
|
||||
# {% end %}
|
||||
# else
|
||||
# "Hoaydaaa"
|
||||
# end
|
||||
# end
|
||||
|
||||
macro find_fnx(fn)
|
||||
case {{fn}}
|
||||
when "Address.building_number"
|
||||
Address.building_number
|
||||
when "Address.city_prefix"
|
||||
Address.city_prefix
|
||||
when "Address.city_suffix"
|
||||
Address.city_suffix
|
||||
when "Address.state"
|
||||
Address.state
|
||||
when "Address.street_name"
|
||||
Address.street_name
|
||||
when "Address.street_suffix"
|
||||
Address.street_suffix
|
||||
when "Company.name"
|
||||
Company.name
|
||||
when "Company.suffix"
|
||||
Company.suffix
|
||||
when "Name.first_name"
|
||||
Name.first_name
|
||||
when "Name.last_name"
|
||||
Name.last_name
|
||||
when "Name.name"
|
||||
Name.name
|
||||
when "Name.prefix"
|
||||
Name.prefix
|
||||
when "Name.suffix"
|
||||
Name.suffix
|
||||
else
|
||||
"Hoaydaaa"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,7 @@
|
||||
module Faker
|
||||
class Company
|
||||
def self.name
|
||||
Formats.sample.call
|
||||
Faker.fetch(Data["company"]["name"])
|
||||
end
|
||||
|
||||
def self.suffix
|
||||
@ -17,11 +17,5 @@ module Faker
|
||||
data = Data["company"]["bs"] as Array(Array(String))
|
||||
Faker.fetch(data.flatten)
|
||||
end
|
||||
|
||||
Formats = [
|
||||
->{ [Name.last_name, suffix].join(" ") },
|
||||
->{ [Name.last_name, Name.last_name].join("-") },
|
||||
->{ "%s, %s and %s" % [Name.last_name, Name.last_name, Name.last_name] },
|
||||
]
|
||||
end
|
||||
end
|
||||
|
@ -1,7 +1,7 @@
|
||||
module Faker
|
||||
class Name
|
||||
def self.name
|
||||
Formats.sample.call.join(" ")
|
||||
Faker.fetch(Data["name"]["name"])
|
||||
end
|
||||
|
||||
{% for data_type in %w(first_name last_name prefix suffix) %}
|
||||
@ -14,18 +14,5 @@ module Faker
|
||||
title = Data["name"]["title"] as Hash
|
||||
Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"])
|
||||
end
|
||||
|
||||
Formats = [
|
||||
->{ [Name.prefix, Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name, Name.suffix] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
->{ [Name.first_name, Name.last_name] },
|
||||
]
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user