Skip to content

Commit

Permalink
Move sampler macro
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Kapkov committed Oct 19, 2015
1 parent 045e445 commit d47f16b
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 20 deletions.
11 changes: 11 additions & 0 deletions lib/faker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/faker/address.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.Address do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc """
Functions for generating addresses.
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/app.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.App do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc """
Functions for generating app specific properties.
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/commerce/en.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/company/en.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.Company.En do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc """
Functions for company data in English
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/internet/en.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.Internet.En do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc"""
Generating internet related data in English
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/lorem.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.Lorem do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc """
Functions for generating Lorem Ipsum data
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/lorem/shakespeare/en.ex
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/lorem/shakespeare/ru.ex
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/faker/name/en.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Faker.Name.En do
import Faker.Sampler
import Faker, only: [sampler: 2]

@moduledoc """
Functions for name data in English
Expand Down
10 changes: 0 additions & 10 deletions lib/faker/sampler.ex
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/faker/team/en.ex
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit d47f16b

Please sign in to comment.