Add RDF.IRI.to_string/1

This commit is contained in:
Marcel Otto 2019-04-29 20:55:36 +02:00
parent 9587b1fd08
commit f89b34ed4b
3 changed files with 43 additions and 8 deletions

View file

@ -9,6 +9,8 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
### Added
- `RDF.IRI.to_string/1` returns the string representation of an `RDF.IRI`
(implicitly resolving vocabulary namespace terms)
- `RDF.Literal.matches?/3` for XQuery regex pattern matching
- `RDF.Decimal.digit_count/1` and `RDF.Decimal.fraction_digit_count/1` for
determining the number of digits of decimal literals

View file

@ -226,10 +226,30 @@ defmodule RDF.IRI do
do: nil
defimpl String.Chars do
def to_string(%RDF.IRI{value: value}) do
value
end
end
@doc """
Returns the given IRI as a string.
Note that this function can also handle `RDF.Vocabulary.Namespace` terms.
## Examples
iex> RDF.IRI.to_string RDF.IRI.new("http://example.com/#foo")
"http://example.com/#foo"
iex> RDF.IRI.to_string EX.foo
"http://example.com/#foo"
iex> RDF.IRI.to_string EX.Foo
"http://example.com/#Foo"
"""
def to_string(iri)
def to_string(%RDF.IRI{value: value}),
do: value
def to_string(qname) when is_atom(qname),
do: qname |> new() |> __MODULE__.to_string()
defimpl String.Chars do
def to_string(iri), do: RDF.IRI.to_string(iri)
end
end

View file

@ -313,13 +313,26 @@ defmodule RDF.IRITest do
end
end
describe "to_string/1" do
test "with IRIs" do
assert IRI.to_string(IRI.new("http://example.com/")) == "http://example.com/"
end
test "to_string/1" do
test "with IRI resolvable namespace terms" do
assert IRI.to_string(EX.Foo) == "http://example.com/#Foo"
assert IRI.to_string(EX.foo) == "http://example.com/#foo"
end
test "with non-resolvable atoms" do
assert_raise RDF.Namespace.UndefinedTermError, fn -> IRI.to_string(Foo.Bar) end
end
end
test "String.Chars protocol implementation" do
assert to_string(IRI.new("http://example.com/")) == "http://example.com/"
end
test "inspect/1" do
test "Inspect protocol implementation" do
assert inspect(IRI.new("http://example.com/")) == "~I<http://example.com/>"
end