Skip to content

Commit 332f8eb

Browse files
author
Tom Johnson
committed
Add graph scopes to queryable transactions
This changes the Transaction semantics to behave like Graph. Queryable is somewhat opinionated that inserting triples to a queryable should result in those triples being accessible through query. In light of this, the answer to #278 seems to be that graph scope must be symmetrical for read and write operations. That interpretation is implemented here.
1 parent a4f85c8 commit 332f8eb

2 files changed

Lines changed: 25 additions & 4 deletions

File tree

lib/rdf/transaction.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Transaction
7474
##
7575
# @see RDF::Enumerable#each
7676
def each(*args, &block)
77-
@snapshot.each(*args, &block)
77+
read_target.each(*args, &block)
7878
end
7979

8080
##
@@ -203,6 +203,12 @@ def readable?
203203
true
204204
end
205205

206+
##
207+
# @see RDF::Enumerable#has_statement?
208+
def has_statement?(statement)
209+
read_target.has_statement?(statement)
210+
end
211+
206212
##
207213
# Returns a developer-friendly representation of this transaction.
208214
#
@@ -271,11 +277,11 @@ def delete_statement(statement)
271277
end
272278

273279
def query_pattern(*args, &block)
274-
@snapshot.send(:query_pattern, *args, &block)
280+
read_target.send(:query_pattern, *args, &block)
275281
end
276282

277283
def query_execute(*args, &block)
278-
@snapshot.send(:query_execute, *args, &block)
284+
read_target.send(:query_execute, *args, &block)
279285
end
280286

281287
undef_method :load, :update, :clear
@@ -289,12 +295,18 @@ def query_execute(*args, &block)
289295
# @param statement [RDF::Statement]
290296
# @return [RDF::Statement]
291297
def process_statement(statement)
292-
if graph_name && statement.graph_name.nil?
298+
if graph_name
293299
statement = statement.dup
294300
statement.graph_name = graph_name
295301
end
296302
statement
297303
end
304+
305+
def read_target
306+
return @snapshot if graph_name.nil?
307+
return @snapshot.project_graph(nil) if graph_name == false
308+
@snapshot.project_graph(graph_name)
309+
end
298310

299311
public
300312

spec/model_graph_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@
200200
context "when querying statements" do
201201
require 'rdf/spec/queryable'
202202
it_behaves_like 'an RDF::Queryable'
203+
204+
context 'with graph_name' do
205+
require 'rdf/spec/queryable'
206+
it_behaves_like 'an RDF::Queryable' do
207+
let(:queryable) do
208+
RDF::Graph.new(graph_name: RDF::URI('g'), data: RDF::Repository.new)
209+
end
210+
end
211+
end
203212
end
204213

205214
context "Examples" do

0 commit comments

Comments
 (0)