core: add __uris__ to vocabulary namespaces which returns all URIs of the vocabulary

This commit is contained in:
Marcel Otto 2017-05-26 22:24:44 +02:00
parent 44cb4b0815
commit 4e523b1bf1
2 changed files with 27 additions and 1 deletions

View file

@ -78,7 +78,19 @@ defmodule RDF.Vocabulary.Namespace do
def __strict__, do: @strict
@terms unquote(Macro.escape(terms))
def __terms__, do: @terms |> Map.keys
def __terms__, do: @terms |> Map.keys
@doc """
Returns all known URIs of the vocabulary.
"""
def __uris__ do
@terms
|> Enum.map(fn
{term, true} -> term_to_uri(@base_uri, term)
{_alias, term} -> term_to_uri(@base_uri, term)
end)
|> Enum.uniq
end
define_vocab_terms unquote(lowercased_terms), unquote(base_uri)

View file

@ -217,6 +217,20 @@ defmodule RDF.Vocabulary.NamespaceTest do
end
end
test "__uris__ returns all URIs of the vocabulary" do
alias TestNS.{Example1, Example5}
assert length(Example1.__uris__) == 2
assert RDF.uri(Example1.foo) in Example1.__uris__
assert RDF.uri(Example1.Bar) in Example1.__uris__
assert length(Example5.__uris__) == 4
assert RDF.uri(Example5.Term1) in Example5.__uris__
assert RDF.uri(Example5.term2) in Example5.__uris__
assert RDF.uri(Example5.Term3) in Example5.__uris__
assert RDF.uri(Example5.term4) in Example5.__uris__
end
describe "invalid character handling" do
test "when a term contains unallowed characters and no alias defined, it fails when invalid_characters = :fail" do
assert_raise RDF.Namespace.InvalidTermError, ~r/Foo-bar.*foo-bar/s,