RDF.BlankNode.Generate can generate blank nodes for every value

- not just strings
This commit is contained in:
Marcel Otto 2018-08-29 21:26:13 +02:00
parent adc1d953dc
commit 598fd731e6
4 changed files with 21 additions and 7 deletions

View file

@ -55,10 +55,10 @@ defmodule RDF.BlankNode.Generator do
@doc """
Generates a blank node for a given string according to the `RDF.BlankNode.Generator.Algorithm` set up.
Generates a blank node for a given value according to the `RDF.BlankNode.Generator.Algorithm` set up.
"""
def generate_for(pid, string) do
GenServer.call(pid, {:generate_for, string})
def generate_for(pid, value) do
GenServer.call(pid, {:generate_for, value})
end

View file

@ -27,6 +27,6 @@ defmodule RDF.BlankNode.Generator.Algorithm do
given state and return a tuple consisting of the generated blank node and the
new state.
"""
@callback generate_for(string :: binary, state :: map) :: {RDF.BlankNode.t, map}
@callback generate_for(value :: any, state :: map) :: {RDF.BlankNode.t, map}
end

View file

@ -36,11 +36,11 @@ defmodule RDF.BlankNode.Increment do
end
@impl BlankNode.Generator.Algorithm
def generate_for(string, %{map: map, counter: counter} = state) do
case Map.get(map, string) do
def generate_for(value, %{map: map, counter: counter} = state) do
case Map.get(map, value) do
nil ->
{bnode(counter, state),
%{state | map: Map.put(map, string, counter), counter: counter + 1}}
%{state | map: Map.put(map, value, counter), counter: counter + 1}}
previous ->
{bnode(previous, state), state}
end

View file

@ -65,4 +65,18 @@ defmodule RDF.BlankNode.IncrementTest do
assert Generator.generate_for(generator, "foo") == bnode("b2")
assert Generator.generate(generator) == bnode("b6")
end
test "generator with non-string values" do
{:ok, generator} = Generator.start_link(Increment, prefix: "b", hash: true)
assert Generator.generate(generator) == bnode("b0")
assert Generator.generate(generator) == bnode("b1")
assert Generator.generate_for(generator, {:foo, 42}) == bnode("b2")
assert Generator.generate(generator) == bnode("b3")
assert Generator.generate_for(generator, [:bar, 3.14]) == bnode("b4")
assert Generator.generate(generator) == bnode("b5")
assert Generator.generate_for(generator, {:foo, 42}) == bnode("b2")
assert Generator.generate(generator) == bnode("b6")
end
end