core: Description.delete with another, possibly not matching subject description

This commit is contained in:
Marcel Otto 2017-06-02 15:12:53 +02:00
parent d772d8f1ed
commit 88900b0966
2 changed files with 27 additions and 1 deletions

View file

@ -73,6 +73,11 @@ defmodule RDF.Description do
@doc """
Adds statements to a `RDF.Description`.
Note: When the statements to be added are given as another `RDF.Description`,
the subject must not match subject of the description to which the statements
are added. As opposed to that `RDF.Data.merge/2` will produce a `RDF.Graph`
containing both descriptions.
"""
def add(description, statements)
@ -221,6 +226,11 @@ defmodule RDF.Description do
@doc """
Deletes statements from a `RDF.Description`.
Note: When the statements to be deleted are given as another `RDF.Description`,
the subject must not match subject of the description from which the statements
are deleted. If you want to delete only a matching description subject, you can
use `RDF.Data.delete/2`.
"""
def delete(description, statements)
@ -239,6 +249,12 @@ defmodule RDF.Description do
end
end
def delete(description = %RDF.Description{}, other_description = %RDF.Description{}) do
Enum.reduce other_description, description, fn ({_, predicate, object}, description) ->
delete(description, predicate, object)
end
end
def delete(description = %RDF.Description{}, predications = %{}) do
Enum.reduce predications, description, fn ({predicate, objects}, description) ->
delete(description, predicate, objects)

View file

@ -270,13 +270,23 @@ defmodule RDF.DescriptionTest do
test "multiple statements with a map of predications",
%{empty_description: empty_description, description3: description3} do
assert Description.delete(empty_description, [{EX.p, [EX.O1, EX.O2]}]) == empty_description
assert Description.delete(empty_description, %{EX.p => EX.O1}) == empty_description
assert Description.delete(description3, %{
EX.p1 => EX.O1,
EX.p2 => [EX.O2, EX.O3],
EX.p3 => [~B<foo>, ~L"bar"],
}) == Description.new(EX.S, EX.p1, EX.O2)
end
test "multiple statements with another description",
%{empty_description: empty_description, description1: description1, description3: description3} do
assert Description.delete(empty_description, description1) == empty_description
assert Description.delete(description3, Description.new(EX.S, %{
EX.p1 => EX.O1,
EX.p2 => [EX.O2, EX.O3],
EX.p3 => [~B<foo>, ~L"bar"],
})) == Description.new(EX.S, EX.p1, EX.O2)
end
end