jsonld-ex/lib/json/ld/node_identifier_map.ex
Andrew Shu 244a4b34b2 Fix warnings (#3)
Fixes most of the warnings. Leaves some warnings on functions marked TODO.
2017-09-14 01:41:12 +02:00

49 lines
1.1 KiB
Elixir

defmodule JSON.LD.NodeIdentifierMap do
@moduledoc nil
use GenServer
# Client API
def start_link(opts \\ []) do
GenServer.start_link(__MODULE__, :ok, opts)
end
def stop(pid, reason \\ :normal, timeout \\ :infinity) do
GenServer.stop(pid, reason, timeout)
end
@doc """
Generate Blank Node Identifier
Details at <https://www.w3.org/TR/json-ld-api/#generate-blank-node-identifier>
"""
def generate_blank_node_id(pid, identifier \\ nil) do
GenServer.call(pid, {:generate_id, identifier})
end
# Server Callbacks
def init(:ok) do
{:ok, %{map: %{}, counter: 0}}
end
def handle_call({:generate_id, identifier}, _, %{map: map, counter: counter} = state) do
if identifier && map[identifier] do
{:reply, map[identifier], state}
else
blank_node_id = "_:b#{counter}"
{:reply, blank_node_id, %{
counter: counter + 1,
map:
if identifier do
Map.put(map, identifier, blank_node_id)
else
map
end
}}
end
end
end