core: simplify implementation of Description DSL by supporting descriptions as subject in Description.new

This commit is contained in:
Marcel Otto 2017-05-25 15:29:39 +02:00
parent 88d260ac1e
commit c82e76cb9e
3 changed files with 13 additions and 9 deletions

View file

@ -27,8 +27,12 @@ defmodule RDF.Description do
do: new(statement) |> add(more_statements) do: new(statement) |> add(more_statements)
def new(subject), def new(subject),
do: %RDF.Description{subject: convert_subject(subject)} do: %RDF.Description{subject: convert_subject(subject)}
def new(%RDF.Description{} = description, predicate, objects),
do: RDF.Description.add(description, predicate, objects)
def new(subject, predicate, objects), def new(subject, predicate, objects),
do: new(subject) |> add(predicate, objects) do: new(subject) |> add(predicate, objects)
def new(subject, statements) when is_list(statements), def new(subject, statements) when is_list(statements),
do: new(subject) |> add(statements) do: new(subject) |> add(statements)
def new(subject, %RDF.Description{predications: predications}), def new(subject, %RDF.Description{predications: predications}),

View file

@ -103,11 +103,6 @@ defmodule RDF.Vocabulary.Namespace do
term_to_uri(@base_uri, term) term_to_uri(@base_uri, term)
end end
def unquote(:"$handle_undefined_function")(term,
[%RDF.Description{} = description | objects]) do
RDF.Description.add(description, term_to_uri(@base_uri, term), objects)
end
def unquote(:"$handle_undefined_function")(term, [subject | objects]) do def unquote(:"$handle_undefined_function")(term, [subject | objects]) do
RDF.Description.new(subject, term_to_uri(@base_uri, term), objects) RDF.Description.new(subject, term_to_uri(@base_uri, term), objects)
end end
@ -197,10 +192,6 @@ defmodule RDF.Vocabulary.Namespace do
@doc "`RDF.Description` builder for <#{@tmp_uri}>" @doc "`RDF.Description` builder for <#{@tmp_uri}>"
def unquote(name)(subject, object) def unquote(name)(subject, object)
def unquote(name)(%RDF.Description{} = description, object) do
RDF.Description.add(description, @tmp_uri, object)
end
def unquote(name)(subject, object) do def unquote(name)(subject, object) do
RDF.Description.new(subject, @tmp_uri, object) RDF.Description.new(subject, @tmp_uri, object)
end end

View file

@ -59,6 +59,15 @@ defmodule RDF.DescriptionTest do
assert description_of_subject(desc, uri(EX.Subject)) assert description_of_subject(desc, uri(EX.Subject))
assert description_includes_predication(desc, {uri(EX.Predicate), uri(EX.Object)}) assert description_includes_predication(desc, {uri(EX.Predicate), uri(EX.Object)})
end end
test "with another description as subject, it performs and add " do
desc = Description.new({EX.S, EX.p, EX.O})
assert Description.new(desc, EX.p2, EX.O2) ==
Description.add(desc, EX.p2, EX.O2)
assert Description.new(desc, EX.p, [EX.O1, EX.O2]) ==
Description.add(desc, EX.p, [EX.O1, EX.O2])
end
end end