From d47f16bf59d7f8a0f9e4c460f5817fbcfed14d78 Mon Sep 17 00:00:00 2001 From: Igor Kapkov Date: Mon, 19 Oct 2015 21:20:07 +1100 Subject: [PATCH] Move sampler macro --- lib/faker.ex | 11 +++++++++++ lib/faker/address.ex | 2 +- lib/faker/app.ex | 2 +- lib/faker/commerce/en.ex | 2 +- lib/faker/company/en.ex | 2 +- lib/faker/internet/en.ex | 2 +- lib/faker/lorem.ex | 2 +- lib/faker/lorem/shakespeare/en.ex | 2 +- lib/faker/lorem/shakespeare/ru.ex | 2 +- lib/faker/name/en.ex | 2 +- lib/faker/sampler.ex | 10 ---------- lib/faker/team/en.ex | 2 +- 12 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/faker.ex b/lib/faker.ex index c765544dc..fa5671eb6 100644 --- a/lib/faker.ex +++ b/lib/faker.ex @@ -75,4 +75,15 @@ defmodule Faker do def locale(lang) when is_atom(lang) do Application.put_env(:faker, :locale, lang) end + + defmacro sampler(name, data) do + count = Enum.count(data) + + quote do + def unquote(name)() do + unquote(data) + |> Enum.at(:crypto.rand_uniform(0, unquote(count))) + end + end + end end diff --git a/lib/faker/address.ex b/lib/faker/address.ex index a8c7eea16..e15d884be 100644 --- a/lib/faker/address.ex +++ b/lib/faker/address.ex @@ -1,5 +1,5 @@ defmodule Faker.Address do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for generating addresses. diff --git a/lib/faker/app.ex b/lib/faker/app.ex index a8a16ba77..c06fa4af6 100644 --- a/lib/faker/app.ex +++ b/lib/faker/app.ex @@ -1,5 +1,5 @@ defmodule Faker.App do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for generating app specific properties. diff --git a/lib/faker/commerce/en.ex b/lib/faker/commerce/en.ex index 696c44358..dc6bdbaf5 100644 --- a/lib/faker/commerce/en.ex +++ b/lib/faker/commerce/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Commerce.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Fuctions for generating commerce related data in English diff --git a/lib/faker/company/en.ex b/lib/faker/company/en.ex index ed7753125..d98109563 100644 --- a/lib/faker/company/en.ex +++ b/lib/faker/company/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Company.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for company data in English diff --git a/lib/faker/internet/en.ex b/lib/faker/internet/en.ex index e8db9cd6c..25082678e 100644 --- a/lib/faker/internet/en.ex +++ b/lib/faker/internet/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Internet.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc""" Generating internet related data in English diff --git a/lib/faker/lorem.ex b/lib/faker/lorem.ex index cd3400649..0f0bebad8 100644 --- a/lib/faker/lorem.ex +++ b/lib/faker/lorem.ex @@ -1,5 +1,5 @@ defmodule Faker.Lorem do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for generating Lorem Ipsum data diff --git a/lib/faker/lorem/shakespeare/en.ex b/lib/faker/lorem/shakespeare/en.ex index 4320f838c..6d35e2471 100644 --- a/lib/faker/lorem/shakespeare/en.ex +++ b/lib/faker/lorem/shakespeare/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Lorem.Shakespeare.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Random quotes from William Shakespeare's plays, sonnets and poems in English. diff --git a/lib/faker/lorem/shakespeare/ru.ex b/lib/faker/lorem/shakespeare/ru.ex index 029bddad9..1b47b718c 100644 --- a/lib/faker/lorem/shakespeare/ru.ex +++ b/lib/faker/lorem/shakespeare/ru.ex @@ -1,5 +1,5 @@ defmodule Faker.Lorem.Shakespeare.Ru do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Random quotes from William Shakespeare's plays, sonnets and poems in Russian. diff --git a/lib/faker/name/en.ex b/lib/faker/name/en.ex index 16c82186d..e660d44ed 100644 --- a/lib/faker/name/en.ex +++ b/lib/faker/name/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Name.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for name data in English diff --git a/lib/faker/sampler.ex b/lib/faker/sampler.ex index b31238b5a..de99273c3 100644 --- a/lib/faker/sampler.ex +++ b/lib/faker/sampler.ex @@ -1,12 +1,2 @@ defmodule Faker.Sampler do - defmacro sampler(name, data) do - count = Enum.count(data) - - quote do - def unquote(name)() do - unquote(data) - |> Enum.at(:crypto.rand_uniform(0, unquote(count))) - end - end - end end diff --git a/lib/faker/team/en.ex b/lib/faker/team/en.ex index 6f4750b17..87bf9a5a1 100644 --- a/lib/faker/team/en.ex +++ b/lib/faker/team/en.ex @@ -1,5 +1,5 @@ defmodule Faker.Team.En do - import Faker.Sampler + import Faker, only: [sampler: 2] @moduledoc """ Functions for generating team related data in English