Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Name Generation and Registry for Rhea
defmodule DB.NameRegistry do
@moduledoc """
Creates and verifies usage of names
"""
import Ecto.Query
alias DB.Repo
@adjectives ~w(
aged ancient autumn billowing bitter black blue bold
broken cold cool crimson damp dark dawn delicate divine
dry empty falling floral fragrant frosty green hidden
holy icy late lingering little lively long misty morning
muddy nameless old patient polished proud purple quiet
red restless rough shy silent small snowy solitary
sparkling spring still summer throbbing twilight
wandering weathered white wild winter wispy withered young
)
@nouns ~w(
bird breeze brook bush butterfly cherry cloud darkness
dawn dew dream dust feather field fire firefly flower
fog forest frog frost glade glitter grass hamster haze
hill lake leaf meadow moon morning mountain night paper
pine pond rain resonance river sea shadow shape silence
sky smoke snow snowflake sound star sun sun sunset surf
thunder tree violet voice water water waterfall wave
wildflower wind wood
)
def generate(type) do
type
|> initialize()
|> verify()
end
def register(map, type) do
map
|> Map.put(:name, generate(type))
end
defp initialize(type) do
%{
type: type,
value: create()
}
end
defp create do
adjective = @adjectives |> Enum.random
noun = @nouns |> Enum.random
[adjective, noun] |> Enum.join("-")
end
defp verify(%{type: type, value: value}) do
case duplicates?(type, value) do
true -> generate(type)
false -> value
_ -> {:error, "An unknown error has occurred in name registry"}
end
end
defp duplicates?(type, value) do
!Enum.empty?(Repo.all(from x in type, where: x.name == ^value))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment