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

Uniquify no argument methods

This commit is contained in:
Ricky Chilcott 2018-09-22 08:59:26 -04:00
parent 68316d43a1
commit 5e4fc757e5
15 changed files with 165 additions and 12 deletions

View File

@ -21,4 +21,15 @@ describe Faker::Address do
Faker::Address.latitude.should eq "-23.850993082533705"
Faker::Address.longitude.should eq "-4.488135572134695"
end
it "should generate unique result" do
Faker.seed 123456
vals = Array(String).new
10_000.times do |t|
vals << Faker::Address.unique_city
end
vals.size.should eq vals.uniq.size
end
end

View File

@ -1,4 +1,5 @@
require "./data.cr"
require "./faker/base"
require "./faker/*"
module Faker

View File

@ -1,19 +1,24 @@
module Faker
class Address
class Address < Base
def self.zip_code
Faker.numerify(["#####", "#####-####"].sample(Faker.rng))
end
uniquify_builder(zip_code)
{% for data_type in %w(state state_abbr city_suffix city_prefix country street_suffix country_code) %}
def self.{{data_type.id}}
Faker.fetch(Data["address"]["{{data_type.id}}"])
end
uniquify_builder({{data_type.id}})
{% end %}
def self.city
Faker.fetch(Data["address"]["city"])
end
uniquify_builder(city)
def self.street_name
[
->{ [Name.last_name, street_suffix].join(" ") },
@ -21,6 +26,8 @@ module Faker
].sample(Faker.rng).call
end
uniquify_builder(street_name)
def self.street_address
Faker.numerify([
->{ "##### %s" % street_name },
@ -32,14 +39,20 @@ module Faker
].sample(Faker.rng).call)
end
uniquify_builder(street_address)
def self.secondary_address
Faker.numerify(Faker.fetch(Data["address"]["secondary_address"]))
end
uniquify_builder(street_address)
def self.building_number
Faker.bothify(Faker.fetch(Data["address"]["building_number"]))
end
uniquify_builder(building_number)
def self.postcode
Faker.bothify([
->{ "??# #??" },
@ -47,16 +60,24 @@ module Faker
].sample(Faker.rng).call)
end
uniquify_builder(postcode)
def self.latitude
((Faker.rng.rand * 180) - 90).to_s
end
uniquify_builder(latitude)
def self.longitude
((Faker.rng.rand * 360) - 180).to_s
end
uniquify_builder(longitude)
def self.time_zone
Faker.fetch(Data["address"]["time_zone"])
end
uniquify_builder(time_zone)
end
end

25
src/faker/base.cr Normal file
View File

@ -0,0 +1,25 @@
module Faker
class NonUniqueValue < Exception
end
class Base
macro uniquify_builder(attribute_name)
@@__unique_vals_for_{{attribute_name}} = Array(String | Int32).new
def self.unique_{{attribute_name}}(max_retries = 10_0000)
meth = ->self.{{attribute_name}}
max_retries.times do |t|
val = meth.call
if !@@__unique_vals_for_{{attribute_name}}.includes?(val)
@@__unique_vals_for_{{attribute_name}} << val
return val
end
end
raise NonUniqueValue.new("Unable to generate unique value for {{attribute_name}}")
end
end
end
end

View File

@ -1,16 +1,22 @@
module Faker
class Business
class Business < Base
def self.credit_card_number
Faker.fetch(Data["business"]["credit_card_numbers"])
end
uniquify_builder(credit_card_numer)
def self.credit_card_expiry_date
credit_card_expiry_date = Faker.fetch(Data["business"]["credit_card_expiry_dates"]).as String
Time.parse_local(credit_card_expiry_date, "%Y-%m-%d")
end
uniquify_builder(credit_card_expiry_date)
def self.credit_card_type
Faker.fetch(Data["business"]["credit_card_types"])
end
uniquify_builder(credit_card_type)
end
end

View File

@ -1,9 +1,11 @@
module Faker
class Commerce
class Commerce < Base
def self.color
Faker.fetch(Data["color"]["name"])
end
uniquify_builder(color)
def self.department(max = 3, fixed_amount = false)
num = max if fixed_amount
num ||= 1 + Faker.rng.rand(max)
@ -17,20 +19,28 @@ module Faker
end
end
# TODO: uniquify_builder(department)
def self.material
product_name = Data["commerce"]["product_name"].as Hash
Faker.fetch(product_name["material"])
end
uniquify_builder(material)
def self.product_name
product_name = Data["commerce"]["product_name"].as Hash
Faker.fetch(product_name["adjective"]) + " " + Faker.fetch(product_name["material"]) + " " + Faker.fetch(product_name["product"])
end
uniquify_builder(product_name)
def self.price(range = 0.0..100.0)
(Faker.rng.rand(range) * 100).floor/100.0
end
# TODO: uniquify_builder(price)
private def self.categories(num)
categories = [] of String
while categories.size < num

View File

@ -1,26 +1,36 @@
module Faker
class Company
class Company < Base
def self.name
Faker.fetch(Data["company"]["name"])
end
uniquify_builder(name)
def self.suffix
Faker.fetch(Data["company"]["suffix"])
end
uniquify_builder(suffix)
def self.catch_phrase
data = Data["company"]["buzzwords"].as Array(Array(String))
Faker.fetch(data.flatten)
end
uniquify_builder(catch_phrase)
def self.bs
data = Data["company"]["bs"].as Array(Array(String))
Faker.fetch(data.flatten)
end
uniquify_builder(bs)
def self.logo
rand_num = Faker.rng.rand(13) + 1
"https://pigment.github.io/fake-logos/logos/medium/color/#{rand_num}.png"
end
uniquify_builder(logo)
end
end

View File

@ -1,5 +1,5 @@
module Faker
class Finance
class Finance < Base
CREDIT_CARD_TYPES = (Data["credit_card"].as(Hash)).keys
def self.credit_card(types : Array = [] of Array(String))
@ -21,5 +21,6 @@ module Faker
template = template.gsub "L", luhn_digit.to_s
template
end
# TODO: uniquify_builder(credit_card)
end
end

View File

@ -1,14 +1,17 @@
# Port of http://shinytoylabs.com/jargon/
module Faker
class Hacker
class Hacker < Base
def self.say_something_smart
phrases.sample(Faker.rng)
end
uniquify_builder(say_something_smart)
{% for data_type in %w(abbreviation adjective noun verb ingverb) %}
def self.{{data_type.id}}
Faker.fetch(Data["hacker"]["{{data_type.id}}"])
end
uniquify_builder({{data_type.id}})
{% end %}
def self.phrases

View File

@ -1,17 +1,23 @@
module Faker
class Internet
class Internet < Base
def self.email(name = nil)
[user_name(name), domain_name].join("@")
end
# TODO: uniquify_builder(email)
def self.free_email(name = nil)
[user_name(name), Faker.fetch(Data["internet"]["free_email"])].join("@")
end
# TODO: uniquify_builder(free_email)
def self.safe_email(name = nil)
[user_name(name), "example." + %w(org com net).shuffle(Faker.rng).first].join("@")
end
# TODO: uniquify_builder(safe_email)
def self.user_name(specifier = nil, separators = %w(. _))
if specifier.is_a? String
return specifier.scan(/\w+/).map { |s| s[0] }.shuffle(Faker.rng).join(separators.sample(Faker.rng)).downcase
@ -51,18 +57,26 @@ module Faker
].sample(Faker.rng).call
end
# TODO: uniquify_builder(user_name)
def self.domain_name
[domain_word, domain_suffix].join(".")
end
uniquify_builder(domain_name)
def self.domain_word
Company.name.split(" ").first.gsub(/\W/, "").downcase
end
uniquify_builder(domain_word)
def self.domain_suffix
Faker.fetch(Data["internet"]["domain_suffix"])
end
uniquify_builder(domain_suffix)
def self.ip_v4_address
[
(2..254).to_a.sample(Faker.rng),
@ -72,27 +86,37 @@ module Faker
].join('.')
end
uniquify_builder(ip_v4_address)
def self.ip_v6_address
ip_v6_space = (0..65535).to_a
container = (1..8).map { |_| ip_v6_space.sample(Faker.rng) }
container.map { |n| n.to_s(16) }.join(':')
end
uniquify_builder(ip_v6_address)
def self.mac_address(prefix = "")
prefix_digits = prefix.split(":").map { |d| d.to_i?(16) ? d.to_i?(16) : 0 }
address_digits = (1..(6 - prefix_digits.size)).map { Faker.rng.rand(256) }
(prefix_digits + address_digits).map { |d| "%02x" % d }.join(":")
end
# TODO: uniquify_builder(mac_address)
def self.url(host = domain_name, path = "/#{user_name}")
"http://#{host}#{path}"
end
# TODO: uniquify_builder(url)
def self.slug(words = nil, glue = nil)
glue ||= %w(- _ .).sample(Faker.rng)
(words || Lorem.words(2).join(' ')).gsub(' ', glue).downcase
end
# TODO: uniquify_builder(slug)
def self.password(min_length = 8, max_length = 16, mix_case = true, special_chars = false)
temp = Lorem.characters(min_length)
diff_length = max_length - min_length
@ -115,5 +139,6 @@ module Faker
return temp
end
# TODO: uniquify_builder(password)
end
end

View File

@ -1,20 +1,26 @@
module Faker
# Based on Perl"s Text::Lorem
class Lorem
class Lorem < Base
def self.character
characters(1)
end
uniquify_builder(character)
def self.characters(char_count = 255)
chars = ("a".."z").to_a + (0..9).to_a
Array(String).new(char_count < 0 ? 0 : char_count, "").map { chars.sample(Faker.rng) }.join("")
end
# TODO: uniquify_builder(characters)
def self.word
words = Data["lorem"]["words"].as Array
Faker.fetch(words)
end
uniquify_builder(word)
def self.words(num = 3, supplemental = false)
words = Data["lorem"]["words"].as Array(String)
words += (Data["lorem"]["supplemental"].as Array(String)) if supplemental
@ -22,10 +28,14 @@ module Faker
words.shuffle(Faker.rng)[0, num]
end
# TODO: uniquify_builder(words)
def self.sentence(word_count = 4, supplemental = false, random_words_to_add = 6)
words(word_count + Faker.rng.rand(random_words_to_add.to_i).to_i, supplemental).join(" ").capitalize + "."
end
# TODO: uniquify_builder(sentence)
def self.sentences(sentence_count = 3, supplemental = false)
([] of String).tap do |sentences|
1.upto(sentence_count) do
@ -34,10 +44,14 @@ module Faker
end
end
# TODO: uniquify_builder(sentences)
def self.paragraph(sentence_count = 3, supplemental = false, random_sentences_to_add = 3)
sentences(sentence_count + Faker.rng.rand(random_sentences_to_add.to_i).to_i, supplemental).join(" ")
end
# TODO: uniquify_builder(paragraph)
def self.paragraphs(paragraph_count = 3, supplemental = false)
([] of String).tap do |paragraphs|
1.upto(paragraph_count) do
@ -45,5 +59,6 @@ module Faker
end
end
end
# TODO: uniquify_builder(paragraphs)
end
end

View File

@ -1,14 +1,17 @@
module Faker
class Name
class Name < Base
{% for data_type in %w(first_name last_name name prefix suffix) %}
def self.{{data_type.id}}
Faker.fetch(Data["name"]["{{data_type.id}}"])
end
uniquify_builder({{data_type.id}})
{% end %}
def self.title
title = Data["name"]["title"].as Hash
Faker.fetch(title["descriptor"]) + " " + Faker.fetch(title["level"]) + " " + Faker.fetch(title["job"])
end
uniquify_builder(title)
end
end

View File

@ -1,39 +1,53 @@
module Faker
class Number
class Number < Base
def self.number(digits)
(1..digits).map { digit }.join ""
end
# TODO: uniquify_builder(number)
def self.digit
Faker.rng.rand(10).to_s
end
uniquify_builder(digit)
def self.decimal(l_digits, r_digits = 2)
l_d = number(l_digits)
r_d = number(r_digits)
"#{l_d}.#{r_d}"
end
# TODO: uniquify_builder(decimal)
def self.between(from = 1.00, to = 5000.00)
Faker.rand_in_range(from, to)
end
# TODO: uniquify_builder(between)
def self.positive(from = 1.00, to = 5000.00)
random_number = between(from, to)
greater_than_zero(random_number)
end
# TODO: uniquify_builder(positive)
def self.negative(from = -5000.00, to = -1.00)
random_number = between(from, to)
less_than_zero(random_number)
end
# TODO: uniquify_builder(negative)
def self.hexadecimal(digits)
hex = ""
digits.times { hex += Faker.rng.rand(15).to_s(16) }
hex
end
# TODO: uniquify_builder(hexadecimal)
private def self.greater_than_zero(number)
if number > 0
number

View File

@ -1,7 +1,9 @@
module Faker
class PhoneNumber
class PhoneNumber < Base
def self.phone_number
Faker.numerify(Faker.fetch(Data["phone_number"]["formats"]))
end
uniquify_builder(phone_number)
end
end

View File

@ -1,15 +1,21 @@
module Faker
class Team
class Team < Base
def self.name
Faker.fetch(Data["team"]["name"])
end
uniquify_builder(name)
def self.creature
Faker.fetch(Data["team"]["creature"])
end
uniquify_builder(creature)
def self.state
Faker.fetch(Data["address"]["state"])
end
uniquify_builder(state)
end
end