Faker Elixir (octopus) v1.0.2 FakerElixir.Helper

Provide useful helpers

Summary

Functions

Will iterate through the enumerable as a constant cycle. Really useful when you want to seed your database with a pre-defined cycle

Fill a pattern with random letters

Fill a pattern with random number

Pick randomly a value in an enumerable

Will generate only unique values for the function given. If unique!/2 can’t generate an unique value after X tries it raises an OverflowError

Functions

cycle(id, enumerable)

Will iterate through the enumerable as a constant cycle. Really useful when you want to seed your database with a pre-defined cycle

Warning: The id (first param given) should be unique for each different cycle

Examples

Basic

iex(1)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Peter"
iex(2)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Audrey"
iex(3)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Laurent"
iex(4)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Frank"
iex(5)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Peter"
iex(6)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Audrey"
iex(7)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Laurent"
iex(8)> FakerElixir.Helper.cycle(:zombies, ["Peter", "Audrey", "Laurent", "Frank"])
"Frank"

# ... and so on.

Seed example

defmodule Seed do

  alias FakerElixir.Helper

  def make do
    warriors = Stream.repeatedly(fn() -> fixture(:warrior) end)
      |> Enum.take(5)
  end


  defp fixture(:warrior) do
    %{
      name: Helper.cycle(:name, ["anubis", "zeus", "behamut"]),
      lvl: Helper.cycle(:lvl, [10, 25, 90])
    }
  end

end

iex> Seed.make
[
  %{lvl: 10, name: "anubis"},
  %{lvl: 25, name: "zeus"},
  %{lvl: 90, name: "behamut"},
  %{lvl: 10, name: "anubis"},
  %{lvl: 25, name: "zeus"}
]
letterify(pattern)

Fill a pattern with random letters

Examples

iex > FakerElixir.Helper.letterify("###")
"ahh"

iex > FakerElixir.Helper.letterify("#.#.#.#")
"k.e.n.u"
numerify(pattern)

Fill a pattern with random number

Examples

iex > FakerElixir.Helper.numerify("Apt. ###")
"Apt. 902"

iex > FakerElixir.Helper.numerify("06.##.##.##.##")
"06.67.18.21.27"
pick(enumerable)

Pick randomly a value in an enumerable

Examples

iex> FakerElixir.Helper.pick(["paris", "athens", "london"])
"london"
iex> FakerElixir.Helper.pick(0..100)
51
unique!(id, func)

Will generate only unique values for the function given. If unique!/2 can’t generate an unique value after X tries it raises an OverflowError.

Warning: The id (first param given) should be unique for each different unique functions

Examples

Generate unique emails

# Create stream to generate unique emails
stream = Stream.repeatedly(fn ->

  FakerElixir.Helper.unique!(:unique_emails, fn -> FakerElixir.Internet.email(:popular) end)

end)

# Grab 400 unique emails
emails = 
  stream |> Enum.take(400)

Basic Seed

In this example you are sure the names and emails generated in the fixture are unique !

defmodule Seed do

  alias FakerElixir.Helper

  def make do
    warriors = Stream.repeatedly(fn() -> fixture(:zombie) end)
      |> Enum.take(50)
  end


  defp fixture(:zombie) do
    %{
      name: Helper.unique!(:names, fn -> FakerElixir.Name.name end),
      email: Helper.unique!(:emails, fn -> FakerElixir.Internet.email(:popular) end)
    }
  end

end

iex> Seed.make
[
  %{email: "leclerc@gmail.com", name: "Mathilde Rousselle"},
  %{email: "sarah@gmail.com", name: "Ambre Marie"},
  %{email: "lola@hotmail.fr", name: "Paul Carton"},
  %{email: "raffin.louise@gmail.com", name: "Lola Beguin"},
  %{email: "de.elisa@yahoo.fr", name: "Enzo Perret"},
  %{email: "clemence.dubos@free.fr", name: "Chloé Devaux"},
  %{email: "justine@hotmail.fr", name: "Mathilde Pouliquen"},
  %{email: "rousselle.jeanne@hotmail.fr", name: "Léo Monin"},
  %{email: "monin.julien@gmail.com", ...},
  %{email: "metais.celia@yahoo.fr", ...}
]