2017-03-12 13:27:52 +00:00
|
|
|
defmodule RDF.Namespace do
|
|
|
|
@moduledoc """
|
2017-08-20 20:35:14 +00:00
|
|
|
A behaviour for resolvers of module atoms to `RDF.IRI`s.
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
Currently there's only one type of such namespaces: `RDF.Vocabulary.Namespace`,
|
|
|
|
but other types are thinkable and might be implemented in the future, eg.
|
|
|
|
namespaces for JSON-LD contexts.
|
2017-03-12 13:27:52 +00:00
|
|
|
"""
|
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
@doc """
|
2017-08-20 20:35:14 +00:00
|
|
|
Resolves a term to a `RDF.IRI`.
|
2017-06-10 19:52:16 +00:00
|
|
|
"""
|
2017-08-20 20:35:14 +00:00
|
|
|
@callback __resolve_term__(atom) :: RDF.IRI.t
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
@doc """
|
|
|
|
All terms of a `RDF.Namespace`.
|
|
|
|
"""
|
2017-03-12 13:27:52 +00:00
|
|
|
@callback __terms__() :: [atom]
|
|
|
|
|
|
|
|
|
2017-06-10 19:52:16 +00:00
|
|
|
@doc """
|
2017-08-20 20:35:14 +00:00
|
|
|
Resolves a qualified term to a `RDF.IRI`.
|
2017-06-10 19:52:16 +00:00
|
|
|
|
|
|
|
It determines a `RDF.Namespace` from the qualifier of the given term and
|
|
|
|
delegates to remaining part of the term to `__resolve_term__/1` of this
|
|
|
|
determined namespace.
|
|
|
|
"""
|
2017-03-12 13:27:52 +00:00
|
|
|
def resolve_term(expr)
|
|
|
|
|
2017-08-20 20:35:14 +00:00
|
|
|
def resolve_term(%RDF.IRI{} = iri), do: iri
|
2017-06-10 21:08:49 +00:00
|
|
|
|
|
|
|
def resolve_term(namespaced_term) when is_atom(namespaced_term) do
|
|
|
|
namespaced_term
|
|
|
|
|> to_string()
|
|
|
|
|> do_resolve_term()
|
|
|
|
end
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
|
2017-06-10 02:26:11 +00:00
|
|
|
defp do_resolve_term("Elixir." <> _ = namespaced_term) do
|
2017-03-12 13:27:52 +00:00
|
|
|
{term, namespace} =
|
2017-06-10 02:26:11 +00:00
|
|
|
namespaced_term
|
2017-03-12 13:27:52 +00:00
|
|
|
|> Module.split
|
|
|
|
|> List.pop_at(-1)
|
2017-06-10 21:08:49 +00:00
|
|
|
do_resolve_term(Module.concat(namespace), String.to_atom(term))
|
2017-06-10 02:26:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp do_resolve_term(namespaced_term) do
|
|
|
|
raise RDF.Namespace.UndefinedTermError,
|
|
|
|
"#{namespaced_term} is not a term on a RDF.Namespace"
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|
|
|
|
|
2017-06-10 21:08:49 +00:00
|
|
|
defp do_resolve_term(RDF, term), do: do_resolve_term(RDF.NS.RDF, term)
|
|
|
|
|
|
|
|
defp do_resolve_term(namespace, term) do
|
2017-08-21 19:06:57 +00:00
|
|
|
if Code.ensure_compiled?(namespace) and
|
|
|
|
Keyword.has_key?(namespace.__info__(:functions), :__resolve_term__)do
|
2017-06-10 21:08:49 +00:00
|
|
|
namespace.__resolve_term__(term)
|
|
|
|
else
|
|
|
|
raise RDF.Namespace.UndefinedTermError,
|
|
|
|
"#{namespace} is not a RDF.Namespace"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-03-12 13:27:52 +00:00
|
|
|
end
|