From 58c577201134c81ab0e8c2e9cc33543210ad2892 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Tue, 28 Jul 2020 14:45:12 +0200 Subject: [PATCH] Add RDF.Description.subject/1 and RDF.Description.change_subject/2 --- CHANGELOG.md | 4 ++++ lib/rdf/description.ex | 16 +++++++++++++++- test/unit/description_test.exs | 9 +++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dee707..8ca2791 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rdf/description.ex b/lib/rdf/description.ex index 2790e7f..7881abc 100644 --- a/lib/rdf/description.ex +++ b/lib/rdf/description.ex @@ -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 diff --git a/test/unit/description_test.exs b/test/unit/description_test.exs index 1c31fa8..e5db30c 100644 --- a/test/unit/description_test.exs +++ b/test/unit/description_test.exs @@ -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)})