Skip to content
This repository has been archived by the owner on Dec 21, 2017. It is now read-only.

Commit

Permalink
When adding to a List, first remove existing elements in list.
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Aug 24, 2010
1 parent fad1b52 commit b9a1c22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/rdf_context/graph.rb
Expand Up @@ -266,13 +266,25 @@ def add(*triples)
end

##
# Adds a list of resources as an RDF list by creating bnodes and first/rest triples
# Adds a list of resources as an RDF list by creating bnodes and first/rest triples.
# Removes existing sequence nodes.
#
# @param [URIRef, BNode] subject the subject of the triple
# @param [URIRef] predicate the predicate of the triple
# @param [Array] objects List of objects to serialize
# @return [Graph] Returns the graph
# @raise [Error] Checks parameter types and raises if they are incorrect.
def add_seq(subject, predicate, objects)
self.triples(Triple.new(subject, predicate, nil)).each do |t, ctx|
bn = t.object
while bn != RDF_NS.nil
rest = properties(bn)[RDF_NS.rest.to_s].first
remove(Triple.new(bn, nil, nil))
bn = rest
end
end
remove(Triple.new(subject, predicate, nil))

if objects.empty?
add_triple(subject, predicate, RDF_NS.nil)
return self
Expand Down
12 changes: 12 additions & 0 deletions spec/graph_spec.rb
Expand Up @@ -463,6 +463,18 @@
l.should be_a(BNode)
g.seq(l).should == [@ex.john, @ex.jane, @ex.rick]
end

it "should remove existing sequence when adding entry to sequence" do
g = Graph.new(:store => ListStore.new)
g.add_seq(@ex.List, @ex.includes, [@ex.john, @ex.jane, @ex.rick])
g.add_seq(@ex.List, @ex.includes, [@ex.john, @ex.jane, @ex.rick, @ex.julie])
puts g.properties(@ex.List).inspect
l = g.properties(@ex.List)[@ex.includes.to_s].first
l.should be_a(BNode)
g.seq(l).should == [@ex.john, @ex.jane, @ex.rick, @ex.julie]

g.triples(Triple.new(nil, RDF_NS.first, @ex.john)).length.should == 1
end
end
end

Expand Down

0 comments on commit b9a1c22

Please sign in to comment.