Add RDF.Description.take/2

This commit is contained in:
Marcel Otto 2019-10-14 09:13:56 +02:00
parent 9e81f69636
commit cf568dd06e
3 changed files with 28 additions and 1 deletions

View file

@ -5,6 +5,19 @@ This project adheres to [Semantic Versioning](http://semver.org/) and
[Keep a CHANGELOG](http://keepachangelog.com).
## Unreleased
### Added
- `RDF.Description.take/2` creates a description from another one by limiting
its statements to a set of predicates
- Mix formatter configuration for using `defvocab` without parens
[Compare v0.6.2...HEAD](https://github.com/marcelotto/rdf-ex/compare/v0.6.2...HEAD)
## 0.6.2 - 2019-09-08
### Added

View file

@ -616,6 +616,15 @@ defmodule RDF.Description do
end
end
@doc """
Creates a description from another one by limiting its statements to those using one of the given `predicates`.
If `predicates` contains properties that are not used in the `description`, they're simply ignored.
"""
def take(%RDF.Description{predications: predications} = description, predicates) do
predicates = Enum.map(predicates, &(coerce_predicate/1))
%RDF.Description{description | predications: Map.take(predications, predicates)}
end
@doc """
Checks if two `RDF.Description`s are equal.

View file

@ -144,7 +144,6 @@ defmodule RDF.DescriptionTest do
refute description_includes_predication(desc, {EX.predicate, iri(EX.Object3)})
end
test "another description" do
desc = description([{EX.predicate1, EX.Object1}, {EX.predicate2, EX.Object2}])
|> Description.add(Description.new({EX.Other, EX.predicate3, EX.Object3}))
@ -358,6 +357,12 @@ defmodule RDF.DescriptionTest do
%{p: ["Foo"]}
end
test "take/2" do
assert Description.new([{EX.S, EX.p1, EX.O1}, {EX.S, EX.p2, EX.O2}])
|> Description.take([EX.p2, EX.p3]) ==
Description.new({EX.S, EX.p2, EX.O2})
end
test "equal/2" do
assert Description.new({EX.S, EX.p, EX.O}) |> Description.equal?(Description.new({EX.S, EX.p, EX.O}))
refute Description.new({EX.S, EX.p, EX.O}) |> Description.equal?(Description.new({EX.S, EX.p, EX.O2}))