Skip to content

Commit fe501c2

Browse files
committed
Improve #quoted? accessor for RDF::Statement, and set it when parsing embedded triples in NTriples/NQuads.
1 parent 3f26a8d commit fe501c2

4 files changed

Lines changed: 38 additions & 15 deletions

File tree

lib/rdf/model/statement.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def self.from(statement, graph_name: nil, **options)
7171
# @option options [RDF::Term] :graph_name (nil)
7272
# Note, in RDF 1.1, a graph name MUST be an {Resource}.
7373
# @option options [Boolean] :inferred used as a marker to record that this statement was inferred based on semantic relationships (T-Box).
74+
# @option options [Boolean] :quoted used as a marker to record that this statement quoted and appears as the subject or object of another RDF::Statement.
7475
# @return [RDF::Statement]
7576
#
7677
# @overload initialize(subject, predicate, object, **options)
@@ -83,6 +84,7 @@ def self.from(statement, graph_name: nil, **options)
8384
# @option options [RDF::Term] :graph_name (nil)
8485
# Note, in RDF 1.1, a graph name MUST be an {Resource}.
8586
# @option options [Boolean] :inferred used as a marker to record that this statement was inferred based on semantic relationships (T-Box).
87+
# @option options [Boolean] :quoted used as a marker to record that this statement quoted and appears as the subject or object of another RDF::Statement.
8688
# @return [RDF::Statement]
8789
def initialize(subject = nil, predicate = nil, object = nil, options = {})
8890
if subject.is_a?(Hash)
@@ -209,7 +211,7 @@ def asserted?
209211
##
210212
# @return [Boolean]
211213
def quoted?
212-
false
214+
!!@options[:quoted]
213215
end
214216

215217
##

lib/rdf/ntriples/reader.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ def read_quotedTriple
255255
if !match(ST_END)
256256
log_error("Expected end of statement (found: #{current_line.inspect})", lineno: lineno, exception: RDF::ReaderError)
257257
end
258-
RDF::Statement.new(subject, predicate, object)
258+
RDF::Statement.new(subject, predicate, object, quoted: true)
259259
end
260260
end
261261

spec/model_statement_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@
186186
it {is_expected.to be_inferred}
187187
end
188188

189+
context "when marked as quoted" do
190+
subject {RDF::Statement.new(RDF::Node.new, p, o, quoted: true)}
191+
it {is_expected.to be_quoted}
192+
end
193+
189194
it {is_expected.to respond_to(:to_h)}
190195
its(:to_h) do
191196
is_expected.to eql({

spec/ntriples_spec.rb

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -428,21 +428,37 @@
428428
end
429429
end
430430

431-
statements.each do |name, st|
432-
context name do
433-
let(:graph) {parse(st, rdfstar: true)}
431+
context "with rdfstar option" do
432+
statements.each do |name, st|
433+
context name do
434+
let(:graph) {parse(st, rdfstar: true)}
435+
436+
it "creates two unquoted statements" do
437+
expect(graph.count).to eql(1)
438+
graph.statements.each do |stmt|
439+
expect(stmt).not_to be_quoted
440+
end
441+
end
434442

435-
it "creates two statements" do
436-
expect(graph.count).to eql(1)
437-
end
443+
it "has a statement whose subject or object is a statement" do
444+
referencing = graph.statements.first
445+
expect(referencing).to be_a_statement
446+
if referencing.subject.statement?
447+
expect(referencing.subject).to be_a_statement
448+
else
449+
expect(referencing.object).to be_a_statement
450+
end
451+
end
438452

439-
it "has a statement whose subject or object is a statement" do
440-
referencing = graph.statements.first
441-
expect(referencing).to be_a_statement
442-
if referencing.subject.statement?
443-
expect(referencing.subject).to be_a_statement
444-
else
445-
expect(referencing.object).to be_a_statement
453+
it "statements which are subject or object of another statement are quoted" do
454+
referencing = graph.statements.first
455+
expect(referencing).to be_a_statement
456+
if referencing.subject.statement?
457+
expect(referencing.subject).to be_a_statement
458+
expect(referencing.subject).to be_quoted
459+
else
460+
expect(referencing.object).to be_a_statement
461+
end
446462
end
447463
end
448464
end

0 commit comments

Comments
 (0)