Skip to content

Commit 50e2d81

Browse files
committed
Allow URI#qname and URI#pname to take a :prefixes named parameter, which skips vocabulary navigation and uses any specified prefixes.
1 parent 8c7ef9d commit 50e2d81

2 files changed

Lines changed: 43 additions & 5 deletions

File tree

lib/rdf/model/uri.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,22 @@ def parent
621621
end
622622

623623
##
624-
# Returns a qualified name (QName) for this URI based on available vocabularies, if possible.
624+
# Returns a qualified name (QName) as a tuple of `[prefix, suffix]` for this URI based on available vocabularies, if possible.
625625
#
626626
# @example
627627
# RDF::URI('http://www.w3.org/2000/01/rdf-schema#').qname #=> [:rdfs, nil]
628628
# RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').qname #=> [:rdfs, :label]
629629
# RDF::RDFS.label.qname #=> [:rdfs, :label]
630630
#
631+
# @param [Hash{Symbol => String}] prefixes
632+
# Explicit set of prefixes to look for matches, defaults to loaded vocabularies.
631633
# @return [Array(Symbol, Symbol)] or `nil` if no QName found
632-
def qname
633-
if self.to_s =~ %r([:/#]([^:/#]*)$)
634+
def qname(prefixes: nil)
635+
if prefixes
636+
prefixes.each do |prefix, uri|
637+
return [prefix, self.to_s[uri.length..-1].to_sym] if self.start_with?(uri)
638+
end
639+
elsif self.to_s =~ %r([:/#]([^:/#]*)$)
634640
local_name = $1
635641
vocab_uri = local_name.empty? ? self.to_s : self.to_s[0...-(local_name.length)]
636642
Vocabulary.each do |vocab|
@@ -655,9 +661,11 @@ def qname
655661
##
656662
# Returns a string version of the QName or the full IRI
657663
#
664+
# @param [Hash{Symbol => String}] prefixes
665+
# Explicit set of prefixes to look for matches, defaults to loaded vocabularies.
658666
# @return [String] or `nil`
659-
def pname
660-
(q = self.qname) ? q.join(":") : to_s
667+
def pname(prefixes: nil)
668+
(q = self.qname(prefixes: prefixes)) ? q.join(":") : to_s
661669
end
662670

663671
##

spec/model_uri_spec.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,36 @@
962962
expect(RDF::RDFS.label.qname).to eql [:rdfs, :label]
963963
end
964964

965+
it "#qname with empty prefixes" do
966+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#').qname(prefixes: {})).to be_nil
967+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').qname(prefixes: {})).to be_nil
968+
expect(RDF::RDFS.label.qname(prefixes: {})).to be_nil
969+
end
970+
971+
it "#qname with explicit prefixes" do
972+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#').qname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql [:rdfs, :""]
973+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').qname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql [:rdfs, :label]
974+
expect(RDF::RDFS.label.qname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql [:rdfs, :label]
975+
end
976+
977+
it "#pname" do
978+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#').pname).to eql 'rdfs:'
979+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').pname).to eql 'rdfs:label'
980+
expect(RDF::RDFS.label.pname).to eql 'rdfs:label'
981+
end
982+
983+
it "#pname with empty prefixes" do
984+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#').pname(prefixes: {})).to eq 'http://www.w3.org/2000/01/rdf-schema#'
985+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').pname(prefixes: {})).to eq 'http://www.w3.org/2000/01/rdf-schema#label'
986+
expect(RDF::RDFS.label.pname(prefixes: {})).to eq 'http://www.w3.org/2000/01/rdf-schema#label'
987+
end
988+
989+
it "#pname with explicit prefixes" do
990+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#').pname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql 'rdfs:'
991+
expect(RDF::URI('http://www.w3.org/2000/01/rdf-schema#label').pname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql 'rdfs:label'
992+
expect(RDF::RDFS.label.pname(prefixes: {rdfs: 'http://www.w3.org/2000/01/rdf-schema#'})).to eql 'rdfs:label'
993+
end
994+
965995
it "#start_with?" do
966996
expect(RDF::URI('http://example.org/')).to be_start_with('http')
967997
expect(RDF::URI('http://example.org/')).not_to be_start_with('ftp')

0 commit comments

Comments
 (0)