Skip to content

Commit

Permalink
support prefix hashes
Browse files Browse the repository at this point in the history
update tests
leave docs alone since hash format is now supported
  • Loading branch information
ConorSheehan1 authored and gkellogg committed Nov 13, 2018
1 parent 6e9bb94 commit 8957d3a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 9 additions & 7 deletions lib/sparql/client/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,16 @@ def slice(start, length)
# @param [string] string
# @return [Query]
# @see http://www.w3.org/TR/sparql11-query/#prefNames
def prefix(*args)
case args.length
when 1
(options[:prefixes] ||= []) << args[0]
when 2
(options[:prefixes] ||= []) << "#{args[0]}: <#{args[1]}>"
def prefix(val)
options[:prefixes] ||= []
if val.kind_of? String
options[:prefixes] << val
elsif val.kind_of? Hash
val.each do |k, v|
options[:prefixes] << "#{k}: <#{v}>"
end
else
raise ArgumentError, "wrong number of arguments (#{args.length} for 1 or 2)"
raise ArgumentError, "prefix must be a kind of String or a Hash"
end
self
end
Expand Down
17 changes: 14 additions & 3 deletions spec/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,20 @@
expect(subject.select.prefix(prefixes[0]).prefix(prefixes[1]).where([:s, :p, :o]).to_s).to eq "PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { ?s ?p ?o . }"
end

it "should support RDF::URI PREFIX" do
prefixes = [[:dc, RDF::URI("http://purl.org/dc/elements/1.1/")], [:foaf, RDF::URI("http://xmlns.com/foaf/0.1/")]]
expect(subject.select.prefix(*prefixes[0]).prefix(*prefixes[1]).where([:s, :p, :o]).to_s).to eq "PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { ?s ?p ?o . }"
it "should support hash PREFIX" do
prefixes = [{dc: RDF::URI("http://purl.org/dc/elements/1.1/")}, {foaf: RDF::URI("http://xmlns.com/foaf/0.1/")}]
expect(subject.select.prefix(prefixes[0]).prefix(prefixes[1]).where([:s, :p, :o]).to_s).to eq "PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { ?s ?p ?o . }"
end

it "should support multiple values in PREFIX hash" do
expect(subject.select.prefix(dc: RDF::URI("http://purl.org/dc/elements/1.1/"), foaf: RDF::URI("http://xmlns.com/foaf/0.1/")).where([:s, :p, :o]).to_s).to eq "PREFIX dc: <http://purl.org/dc/elements/1.1/> PREFIX foaf: <http://xmlns.com/foaf/0.1/> SELECT * WHERE { ?s ?p ?o . }"
end

it "should raise an ArgumentError for invalid PREFIX type" do
inavlid_prefix_types = [RDF::URI('missing prefix hash'), 0, []]
inavlid_prefix_types.each do |invalid_arg|
expect { subject.select.prefix(invalid_arg) }.to raise_error ArgumentError, "prefix must be a kind of String or a Hash"
end
end

it "should support OPTIONAL" do
Expand Down

0 comments on commit 8957d3a

Please sign in to comment.