-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from rickychilcott/feature/unique
Generate unique address
- Loading branch information
Showing
19 changed files
with
208 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
language: crystal | ||
crystal: | ||
- latest | ||
- nightly | ||
script: | ||
- crystal spec | ||
- crystal run examples/test.cr | ||
- crystal run examples/test2.cr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
require "./data.cr" | ||
require "./faker/base" | ||
require "./faker/*" | ||
|
||
module Faker | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module Faker | ||
class NonUniqueValue < Exception | ||
end | ||
|
||
class Base | ||
alias Any = String | Int32 | Float64 | Time | ||
|
||
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) | ||
max_retries.times do |t| | ||
val = self.{{attribute_name}}({{*modified_method_attributes}}) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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_number) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.