Skip to content

Commit 2119468

Browse files
committed
Add Term#terms and Statement#terms which returns an array including itself, and array including the non-nil non-statement terms, respectively.
1 parent 0e84e3d commit 2119468

8 files changed

Lines changed: 54 additions & 5 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This is a pure-Ruby library for working with [Resource Description Framework
77

88
[![Gem Version](https://badge.fury.io/rb/rdf.png)](https://badge.fury.io/rb/rdf)
99
[![Build Status](https://github.com/ruby-rdf/rdf/workflows/CI/badge.svg?branch=develop)](https://github.com/ruby-rdf/rdf/actions?query=workflow%3ACI)
10-
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/rdf/badge.svg)](https://coveralls.io/github/ruby-rdf/rdf)
10+
[![Coverage Status](https://coveralls.io/repos/ruby-rdf/rdf/badge.svg?branch=develop)](https://coveralls.io/github/ruby-rdf/rdf?branch=develop)
1111
[![Gitter chat](https://badges.gitter.im/ruby-rdf/rdf.png)](https://gitter.im/ruby-rdf/rdf)
1212

1313
## Features

etc/n-triples-star.ebnf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[1] ntriplesDoc ::= triple? (EOL triple)* EOL?
2+
[2] triple ::= subject predicate object '.'
3+
[3] subject ::= IRIREF | BLANK_NODE_LABEL | embTriple
4+
[4] predicate ::= IRIREF
5+
[5] object ::= IRIREF | BLANK_NODE_LABEL | literal | embTriple
6+
[6] literal ::= STRING_LITERAL_QUOTE ('^^' IRIREF | LANGTAG)?
7+
[7] embTriple ::= '<<' subject predicate object '>>'

etc/n-triples.ebnf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[1] ntriplesDoc ::= triple? (EOL triple)* EOL?
2+
[2] triple ::= subject predicate object '.'
3+
[3] subject ::= IRIREF | BLANK_NODE_LABEL
4+
[4] predicate ::= IRIREF
5+
[5] object ::= IRIREF | BLANK_NODE_LABEL | literal
6+
[6] literal ::= STRING_LITERAL_QUOTE ('^^' IRIREF | LANGTAG)?

lib/rdf/mixin/enumerable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def enum_object
511511
def terms(unique: true)
512512
unless unique
513513
enum_statement.
514-
map(&:to_quad).
514+
map(&:terms).
515515
flatten.
516516
compact
517517
else
@@ -551,8 +551,8 @@ def each_term
551551
if block_given?
552552
values = {}
553553
each_statement do |statement|
554-
statement.to_quad.each do |value|
555-
unless value.nil? || values.include?(value.hash)
554+
statement.terms.each do |value|
555+
unless values.include?(value.hash)
556556
values[value.hash] = true
557557
yield value
558558
end

lib/rdf/model/statement.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def has_object?
240240
end
241241

242242
##
243-
# Returns `true` if any resource of this statement is a blank node.
243+
# Returns `true` if any resource of this statement is a blank node
244+
# or has an embedded statement including a blank node.
244245
#
245246
# @return [Boolean]
246247
# @since 2.0
@@ -359,6 +360,13 @@ def to_triple
359360
end
360361
alias_method :to_a, :to_triple
361362

363+
##
364+
# Returns an array of all the non-nil non-statement terms.
365+
# @return [Array(RDF::Term)]
366+
def terms
367+
to_quad.map {|t| t.respond_to?(:terms) ? t.terms : t}.flatten.compact
368+
end
369+
362370
##
363371
# Canonicalizes each unfrozen term in the statement
364372
#

lib/rdf/model/term.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ def to_term
7272
self
7373
end
7474

75+
##
76+
# Returns an array including just itself.
77+
#
78+
# @return [Array<RDF::Value>]
79+
def terms
80+
[self]
81+
end
82+
7583
##
7684
# Returns the base representation of this term.
7785
#

spec/model_statement_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
it {is_expected.not_to be_quoted}
7676
it {is_expected.to be_statement}
7777
it {is_expected.not_to be_inferred}
78+
its(:terms) {is_expected.to include(s, p, o)}
7879
end
7980

8081
context "when created with a blank node subject" do
@@ -116,6 +117,12 @@
116117
specify {expect(RDF::Statement(:s, p, o)).to eql (RDF::Statement(:s, p, o))}
117118
specify {expect(RDF::Statement(s, p, :o)).to eq (RDF::Statement(s, p, :o))}
118119
specify {expect(RDF::Statement(s, p, :o)).to eql (RDF::Statement(s, p, :o))}
120+
121+
describe "#terms" do
122+
subject {RDF::Statement(:s, p, o)}
123+
specify {expect(subject.terms).not_to include(:s)}
124+
specify {expect(subject.terms).to include(p, o)}
125+
end
119126
end
120127

121128
context "when used with strings" do
@@ -293,6 +300,14 @@
293300
it "is embedded for statements having a statement object" do
294301
expect(RDF::Statement(:s, :p, RDF::Statement(:s1, :p1, :o1))).to be_embedded
295302
end
303+
304+
context "#terms" do
305+
let(:s1) {RDF::URI.new("http://example.org/s1")}
306+
let(:p1) {RDF::URI("http://example.org/p1")}
307+
let(:o1) {RDF::URI.new("http://example.org/o1")}
308+
subject {RDF::Statement(s, p, RDF::Statement(s1, p1, o1))}
309+
specify {expect(subject.terms).to include(s, p, s1, p1, o1)}
310+
end
296311
end
297312

298313
context "c14n" do

spec/model_term_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
is_expected.to be_term
2323
end
2424

25+
its(:terms) do
26+
is_expected.to be_a(Array)
27+
is_expected.to eq [subject]
28+
end
29+
2530
it "#to_term" do
2631
expect(subject.to_term).to equal subject
2732
end

0 commit comments

Comments
 (0)