Skip to content

Commit 3e2326c

Browse files
committed
Add RDF::Vocabulary.__prefix__= to set the prefix to use for a given vocabulary. (fixes #431).
1 parent 54ae4d4 commit 3e2326c

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

lib/rdf/vocabulary.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,26 @@ def inspect
648648
# @return [Symbol]
649649
# @since 0.3.0
650650
def __prefix__
651-
__name__.split('::').last.downcase.to_sym
651+
instance_variable_defined?(:@__prefix__) ?
652+
@__prefix__ :
653+
__name__.split('::').last.downcase.to_sym
654+
end
655+
656+
##
657+
# Returns a suggested CURIE/PName prefix for this vocabulary class.
658+
#
659+
# @example Overriding a standard vocabulary prefix.
660+
# RDF::Vocab::DC.__prefix__ = :dcterms
661+
# RDF::Vocab::DC.title.pname #=> 'dcterms:title'
662+
#
663+
# @param [Symbol] prefix
664+
# @return [Symbol]
665+
# @since 3.2.3
666+
def __prefix__=(prefix)
667+
params = RDF::Vocabulary.vocab_map[__prefix__]
668+
@__prefix__ = prefix.to_sym
669+
RDF::Vocabulary.register(@__prefix__, self, **params)
670+
@__prefix__
652671
end
653672

654673
protected

spec/vocabulary_spec.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,22 @@
909909
end
910910

911911
context 'Vocabs outside of the RDF::Vocab namespace' do
912-
uri = 'urn:x-bogus:test-vocab:'
913-
TestVocab = Class.new RDF::Vocabulary(uri)
914-
RDF::Vocabulary.register :testvocab, TestVocab
912+
let(:uri) {'urn:x-bogus:test-vocab:'}
913+
before(:all) {
914+
TestVocab = Class.new RDF::Vocabulary('urn:x-bogus:test-vocab:')
915+
RDF::Vocabulary.register :testvocab, TestVocab
916+
}
917+
915918
it 'correctly expands the pname of an arbitrary class' do
916919
expect(RDF::Vocabulary.expand_pname('testvocab:test')).to eq(TestVocab.test)
917920
end
921+
922+
it 'uses custom prefix' do
923+
TestVocab.__prefix__ = :tv
924+
term = RDF::Vocabulary.expand_pname('tv:test')
925+
expect(term).to eq(TestVocab.test)
926+
expect(term.pname).to eq 'tv:test'
927+
expect(RDF::Vocabulary.find_term(term.to_s)).to eq TestVocab.test
928+
end
918929
end
919930
end

0 commit comments

Comments
 (0)