1
0
mirror of https://github.com/askn/faker.git synced 2023-08-10 21:13:01 +03:00

add fetch

This commit is contained in:
Aşkın Gedik 2016-01-12 09:02:02 +02:00
parent 6a2833f187
commit 36fe59b671
5 changed files with 66 additions and 30 deletions

View File

@ -58,7 +58,7 @@ Faker::Address.latitude #=> "-58.17256227443719"
Faker::Address.longitude #=> "-156.65548382095133" Faker::Address.longitude #=> "-156.65548382095133"
``` ```
### Faker::Business ### Faker::Business
------------------ ------------------
```crystal ```crystal
@ -72,7 +72,7 @@ Faker::Business.credit_card_type #=> "visa"
``` ```
### Faker::Commerce ### Faker::Commerce
```crystal ```crystal
@ -85,7 +85,7 @@ Faker::Commerce.price #=> "44.6"
``` ```
### Faker::Company ### Faker::Company
```crystal ```crystal
@ -96,7 +96,7 @@ Faker::Company.suffix #=> "Group"
``` ```
### Faker::Internet ### Faker::Internet
```crystal ```crystal
# Optional argument name=nil # Optional argument name=nil
@ -146,7 +146,7 @@ Faker::Internet.slug('foo bar', '-') #=> "foo-bar"
``` ```
### Faker::Lorem ### Faker::Lorem
```crystal ```crystal
@ -216,7 +216,7 @@ Faker::Number.digit #=> "1"
``` ```
### Faker::PhoneNumber ### Faker::PhoneNumber
```crystal ```crystal

File diff suppressed because one or more lines are too long

View File

@ -29,7 +29,63 @@ module Faker
end end
def self.fetch(data) def self.fetch(data)
d = data as Array data = data as Array
d.sample 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
end end

View File

@ -1,7 +1,7 @@
module Faker module Faker
class Company class Company
def self.name def self.name
Formats.sample.call Faker.fetch(Data["company"]["name"])
end end
def self.suffix def self.suffix
@ -17,11 +17,5 @@ module Faker
data = Data["company"]["bs"] as Array(Array(String)) data = Data["company"]["bs"] as Array(Array(String))
Faker.fetch(data.flatten) Faker.fetch(data.flatten)
end 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
end end

View File

@ -1,7 +1,7 @@
module Faker module Faker
class Name class Name
def self.name def self.name
Formats.sample.call.join(" ") Faker.fetch(Data["name"]["name"])
end end
{% for data_type in %w(first_name last_name prefix suffix) %} {% for data_type in %w(first_name last_name prefix suffix) %}
@ -14,18 +14,5 @@ module Faker
title = Data["name"]["title"] as Hash title = Data["name"]["title"] as Hash
Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"]) Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"])
end 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
end end