From 69fbdd60b36374e89d20b69c35add0f2fd8e12d6 Mon Sep 17 00:00:00 2001 From: Marcel Otto Date: Mon, 28 Sep 2020 11:22:46 +0200 Subject: [PATCH] Add RDF.Graph.base_iri/1 and RDF.Graph.prefixes/1 --- CHANGELOG.md | 10 ++++++++-- lib/rdf/graph.ex | 12 ++++++++++++ test/unit/graph_test.exs | 12 ++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8ad58c..cb6dde3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,8 +9,14 @@ This project adheres to [Semantic Versioning](http://semver.org/) and ### Added -- `RDF.Description.subject/1` and `RDF.Description.change_subject/2` -- `RDF.Graph.name/1` and `RDF.Graph.change_name/2` +- to `RDF.Description` + - `RDF.Description.subject/1` + - `RDF.Description.change_subject/2` +- to `RDF.Graph` + - `RDF.Graph.name/1` + - `RDF.Graph.change_name/2` + - `RDF.Graph.base_iri/1` + - `RDF.Graph.prefixes/1` ### Changed diff --git a/lib/rdf/graph.ex b/lib/rdf/graph.ex index 76a3351..81efef9 100644 --- a/lib/rdf/graph.ex +++ b/lib/rdf/graph.ex @@ -901,6 +901,12 @@ defmodule RDF.Graph do def equal?(_, _), do: false + @doc """ + Returns the prefixes of the given `graph` as a `RDF.PrefixMap`. + """ + @spec prefixes(t) :: PrefixMap.t() | nil + def prefixes(%__MODULE__{} = graph), do: graph.prefixes + @doc """ Adds `prefixes` to the given `graph`. @@ -955,6 +961,12 @@ defmodule RDF.Graph do %__MODULE__{graph | prefixes: nil} end + @doc """ + Returns the base IRI of the given `graph`. + """ + @spec base_iri(t) :: IRI.t() | nil + def base_iri(%__MODULE__{} = graph), do: graph.base_iri + @doc """ Sets the base IRI of the given `graph`. diff --git a/test/unit/graph_test.exs b/test/unit/graph_test.exs index 4eacd93..2e82c41 100644 --- a/test/unit/graph_test.exs +++ b/test/unit/graph_test.exs @@ -810,6 +810,11 @@ defmodule RDF.GraphTest do |> Graph.equal?(Graph.new({EX.S, EX.p(), EX.O}, name: EX.Graph2)) end + test "prefixes/1" do + assert Graph.prefixes(graph()) == nil + assert %Graph{prefixes: PrefixMap.new()} |> Graph.prefixes() == PrefixMap.new() + end + describe "add_prefixes/2" do test "when prefixes already exist" do graph = Graph.new(prefixes: %{xsd: XSD}) |> Graph.add_prefixes(ex: EX) @@ -857,6 +862,13 @@ defmodule RDF.GraphTest do assert Graph.clear_prefixes(Graph.new(prefixes: %{ex: EX})) == Graph.new() end + test "base_iri/1" do + assert Graph.base_iri(graph()) == nil + + assert %Graph{base_iri: ~I} |> Graph.base_iri() == + ~I + end + describe "set_base_iri/1" do test "when given an IRI" do graph = Graph.new() |> Graph.set_base_iri(~I)