Add RDF.Description.subject/1 and RDF.Description.change_subject/2

This commit is contained in:
Marcel Otto 2020-07-28 14:45:12 +02:00
parent e9432ef556
commit 58c5772011
3 changed files with 28 additions and 1 deletions

View file

@ -7,6 +7,10 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
## Unreleased
### Added
- `RDF.Description.subject/1` and `RDF.Description.change_subject/2`
### Changed
- for consistency reasons the internal `:id` struct field of `RDF.BlankNode` was renamed

View file

@ -50,6 +50,18 @@ defmodule RDF.Description do
def new({subject, predicate, object}), do: new(subject) |> add({predicate, object})
def new(subject), do: %__MODULE__{subject: coerce_subject(subject)}
@doc """
Returns the subject IRI or blank node of a description.
"""
def subject(%__MODULE__{} = description), do: description.subject
@doc """
Changes the subject of a description.
"""
def change_subject(%__MODULE__{} = description, new_subject) do
%__MODULE__{description | subject: coerce_subject(new_subject)}
end
@doc """
Add statements to a `RDF.Description`.
@ -130,7 +142,9 @@ defmodule RDF.Description do
Note: As it is a destructive function this function is more strict in its handling of
`RDF.Description`s than `add/3`. The subject of a `RDF.Description` to be put must
match.
match. If you want to overwrite existing statements with those from the description of
another subject, you'll have to explicitly change the subject with `change_subject/2`
first before using `put/3`.
## Examples

View file

@ -34,6 +34,15 @@ defmodule RDF.DescriptionTest do
end
end
test "subject/1" do
assert Description.subject(description()) == description().subject
end
test "change_subject/2" do
assert Description.change_subject(description(), EX.NewSubject).subject ==
iri(EX.NewSubject)
end
describe "add/3" do
test "with a triple" do
assert Description.add(description(), {iri(EX.Subject), EX.predicate(), iri(EX.Object)})