Packages

Pure-Elixir tabletop RPG game support and rules modeled on the System Reference Document (SRD) 5.2 with opt-in support for some 5.1 rules.

Current section

Files

Jump to
srd_5e lib srd content conditions.ex
Raw

lib/srd/content/conditions.ex

defmodule Srd.Content.Conditions do
@moduledoc """
SRD conditions, looked up by slug.
"""
alias Srd.Content.Condition
@data_file Path.expand("../../../priv/data/conditions.exs", __DIR__)
@external_resource @data_file
@conditions @data_file
|> Code.eval_file()
|> elem(0)
|> Map.new(fn data -> {data.slug, Condition.new(data)} end)
@doc """
Every condition.
"""
@spec all() :: [Condition.t()]
def all, do: Map.values(conditions())
@doc """
Look up a condition by slug, returning `nil` if there is none.
"""
@spec get(String.t()) :: Condition.t() | nil
def get(slug), do: Map.get(conditions(), slug)
@doc """
Look up a condition by slug, raising if there is none.
"""
@spec fetch!(String.t()) :: Condition.t()
def fetch!(slug) do
case get(slug) do
nil -> raise KeyError, "no condition with slug #{inspect(slug)}"
condition -> condition
end
end
defp conditions, do: @conditions
end