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

Get all methods to have a unique_ variant

This commit is contained in:
Ricky Chilcott 2019-06-19 09:15:51 -04:00
parent 4d8f2e6f6e
commit bd1a8efc6c
9 changed files with 68 additions and 33 deletions

View File

@ -23,9 +23,15 @@ puts Faker::Address.longitude
puts "\n### Faker::Commerce\n\n"
puts Faker::Commerce.color
puts Faker::Commerce.unique_color
puts Faker::Commerce.department
puts Faker::Commerce.unique_department
puts Faker::Commerce.unique_department(2)
puts Faker::Commerce.unique_department(2, true)
puts Faker::Commerce.product_name
puts Faker::Commerce.price
puts Faker::Commerce.unique_price
puts Faker::Commerce.unique_price(0.0..10_000.00)
puts "\n### Faker::Company\n\n"
@ -38,10 +44,16 @@ puts "\n\t### Faker::Internet\n\n"
puts Faker::Internet.email
puts Faker::Internet.email("Nancy")
puts Faker::Internet.unique_email("Nancy")
puts Faker::Internet.unique_email("Nancy")
puts Faker::Internet.free_email
puts Faker::Internet.unique_free_email
puts Faker::Internet.free_email("Nancy")
puts Faker::Internet.unique_free_email("Nancy")
puts Faker::Internet.safe_email
puts Faker::Internet.unique_safe_email
puts Faker::Internet.safe_email("Nancy")
puts Faker::Internet.unique_safe_email("Nancy")
puts Faker::Internet.user_name
puts Faker::Internet.user_name("Nancy")
@ -88,12 +100,25 @@ puts Faker::Name.prefix
puts Faker::Name.suffix
puts Faker::Name.title
puts "\n\t### Faker::Avatar\n\n"
puts Faker::Avatar.image
puts Faker::Avatar.image("borp")
puts Faker::Avatar.unique_image("borp")
puts Faker::Avatar.unique_image("borp")
puts "\n\t### Faker::Number\n\n"
puts Faker::Number.number(10)
# puts Faker::Number.unique_number(10)
# puts Faker::Number.unique_number(10)
# puts Faker::Number.unique_number(10)
puts Faker::Number.decimal(2)
puts Faker::Number.decimal(2, 3)
puts Faker::Number.unique_decimal(2)
# puts Faker::Number.unique_decimal(2, 3)
puts Faker::Number.digit
puts Faker::Number.unique_digit
puts "\n\t### Faker::PhoneNumber\n\n"
@ -101,6 +126,10 @@ puts Faker::PhoneNumber.phone_number
puts "\n\t### Faker::Business\n\n"
p Faker::Business.credit_card_number
p Faker::Business.credit_card_expiry_date
p Faker::Business.credit_card_type
puts Faker::Business.credit_card_number
puts Faker::Business.credit_card_expiry_date
puts Faker::Business.credit_card_type
puts Faker::Business.unique_credit_card_number
puts Faker::Business.unique_credit_card_expiry_date
puts Faker::Business.unique_credit_card_type

View File

@ -1,8 +1,10 @@
module Faker
class Avatar
class Avatar < Base
def self.image(slug = nil)
slug ||= Faker::Lorem.word
"http://robohash.org/#{slug}"
end
uniquify_builder(image, slug = nil)
end
end

View File

@ -3,14 +3,16 @@ module Faker
end
class Base
macro uniquify_builder(attribute_name)
@@__unique_vals_for_{{attribute_name}} = Array(String | Int32).new
alias Any = String | Int32 | Float64 | Time
def self.unique_{{attribute_name}}(max_retries = 10_0000)
meth = ->self.{{attribute_name}}
macro uniquify_builder(attribute_name, *modified_method_attributes)
@@__unique_vals_for_{{attribute_name}} = Array(Any).new
def self.unique_{{attribute_name}}({% if !modified_method_attributes.empty? %}{{*modified_method_attributes}},{% end %} max_retries = 10_0000)
# meth = ->self.{{attribute_name}}
max_retries.times do |t|
val = meth.call
val = self.{{attribute_name}}({{*modified_method_attributes}})
if !@@__unique_vals_for_{{attribute_name}}.includes?(val)
@@__unique_vals_for_{{attribute_name}} << val

View File

@ -4,7 +4,7 @@ module Faker
Faker.fetch(Data["business"]["credit_card_numbers"])
end
uniquify_builder(credit_card_numer)
uniquify_builder(credit_card_number)
def self.credit_card_expiry_date
credit_card_expiry_date = Faker.fetch(Data["business"]["credit_card_expiry_dates"]).as String

View File

@ -19,7 +19,7 @@ module Faker
end
end
# TODO: uniquify_builder(department)
uniquify_builder(department, max = 3, fixed_amount = false)
def self.material
product_name = Data["commerce"]["product_name"].as Hash
@ -39,7 +39,7 @@ module Faker
(Faker.rng.rand(range) * 100).floor/100.0
end
# TODO: uniquify_builder(price)
uniquify_builder(price, range = 0.0..100.0)
private def self.categories(num)
categories = [] of String

View File

@ -21,6 +21,6 @@ module Faker
template = template.gsub "L", luhn_digit.to_s
template
end
# TODO: uniquify_builder(credit_card)
# TODO: uniquify_builder(credit_card, types : Array = [] of Array(String))
end
end

View File

@ -4,19 +4,19 @@ module Faker
[user_name(name), domain_name].join("@")
end
# TODO: uniquify_builder(email)
uniquify_builder(email, name = nil)
def self.free_email(name = nil)
[user_name(name), Faker.fetch(Data["internet"]["free_email"])].join("@")
end
# TODO: uniquify_builder(free_email)
uniquify_builder(free_email, name = nil)
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)
uniquify_builder(safe_email, name = nil)
def self.user_name(specifier = nil, separators = %w(. _))
if specifier.is_a? String
@ -57,7 +57,7 @@ module Faker
].sample(Faker.rng).call
end
# TODO: uniquify_builder(user_name)
uniquify_builder(user_name, specifier = nil, separators = %w(. _))
def self.domain_name
[domain_word, domain_suffix].join(".")
@ -102,20 +102,20 @@ module Faker
(prefix_digits + address_digits).map { |d| "%02x" % d }.join(":")
end
# TODO: uniquify_builder(mac_address)
uniquify_builder(mac_address, prefix = "")
def self.url(host = domain_name, path = "/#{user_name}")
"http://#{host}#{path}"
end
# TODO: uniquify_builder(url)
uniquify_builder(url, host = domain_name, path = "/#{user_name}")
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)
uniquify_builder(slug, words = nil, glue = nil)
def self.password(min_length = 8, max_length = 16, mix_case = true, special_chars = false)
temp = Lorem.characters(min_length)
@ -139,6 +139,7 @@ module Faker
return temp
end
# TODO: uniquify_builder(password)
uniquify_builder(password, min_length = 8, max_length = 16, mix_case = true, special_chars = false)
end
end

View File

@ -12,7 +12,7 @@ module Faker
Array(String).new(char_count < 0 ? 0 : char_count, "").map { chars.sample(Faker.rng) }.join("")
end
# TODO: uniquify_builder(characters)
uniquify_builder(characters)
def self.word
words = Data["lorem"]["words"].as Array
@ -28,13 +28,13 @@ module Faker
words.shuffle(Faker.rng)[0, num]
end
# TODO: uniquify_builder(words)
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)
uniquify_builder(sentence)
def self.sentences(sentence_count = 3, supplemental = false)
([] of String).tap do |sentences|
@ -44,13 +44,13 @@ module Faker
end
end
# TODO: uniquify_builder(sentences)
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)
uniquify_builder(paragraph)
def self.paragraphs(paragraph_count = 3, supplemental = false)
([] of String).tap do |paragraphs|
@ -59,6 +59,7 @@ module Faker
end
end
end
# TODO: uniquify_builder(paragraphs)
uniquify_builder(paragraphs)
end
end

View File

@ -4,7 +4,7 @@ module Faker
(1..digits).map { digit }.join ""
end
# TODO: uniquify_builder(number)
uniquify_builder(number, digits)
def self.digit
Faker.rng.rand(10).to_s
@ -18,27 +18,27 @@ module Faker
"#{l_d}.#{r_d}"
end
# TODO: uniquify_builder(decimal)
uniquify_builder(decimal, l_digits, r_digits = 2)
def self.between(from = 1.00, to = 5000.00)
Faker.rand_in_range(from, to)
end
# TODO: uniquify_builder(between)
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)
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)
uniquify_builder(negative)
def self.hexadecimal(digits)
hex = ""
@ -46,7 +46,7 @@ module Faker
hex
end
# TODO: uniquify_builder(hexadecimal)
uniquify_builder(hexadecimal)
private def self.greater_than_zero(number)
if number > 0