Skip to content

Commit

Permalink
Finish 3.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gkellogg committed Jun 16, 2020
2 parents 1a33e00 + e9ca05a commit f92daa7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.1.2
3.1.3
7 changes: 7 additions & 0 deletions lib/rdf/changeset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,12 @@ def append_statements(target, arg)
stmts.each { |s| send(target) << s }
end

# This simply returns its argument as a query in order to trick
# {RDF::Mutable#delete} into working.
def query(stmt)
RDF::Query.new RDF::Query::Pattern.from(stmt)
end

undef_method :load
end # Changeset
end # RDF
42 changes: 39 additions & 3 deletions lib/rdf/vocabulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,15 @@ def each(&block)
end

##
# A hash of all vocabularies by prefix showing relevant URI and associated vocabulary Class Name
# A hash of all vocabularies by prefix showing relevant URI and
# associated vocabulary Class Name
#
# @return [Hash{Symbol => Hash{Symbol => String}}]
def vocab_map
VOCABS
# Create an initial duplicate of RDF::VOCABS. We want to
# ensure the hash itself is modifiable but the values are
# frozen.
@vocab_map ||= RDF::VOCABS.transform_values(&:freeze)
end

##
Expand All @@ -100,6 +105,36 @@ def from_sym(sym)
RDF.const_get(sym.to_sym)
end

##
# Register a vocabulary for internal prefix lookups. Parameters
# of interest include `:uri`, `:class_name`, `:source`, and `:skip`.
#
# @param prefix [Symbol] the prefix to use
# @param vocab [String, Class] either the URI or the vocab class
# @param params [Hash{Symbol => String}] Relevant parameters
# @return [Hash] The parameter hash, but frozen
def register(prefix, vocab, **params)
# check the input
raise ArgumentError, "#{prefix} must be symbol-able" unless
[String, Symbol].any? { |c| prefix.is_a? c }

# note an explicit uri: param overrides
case vocab
when String then params[:uri] ||= vocab
when Class
raise ArgumentError, 'vocab must be an RDF::(Strict)Vocabulary' unless
vocab.ancestors.include? RDF::Vocabulary
params[:class] = vocab
params[:uri] ||= vocab.to_uri.to_s
end

# fill in the class name
params[:class_name] ||= prefix.to_s.upcase

# now freeze and assign
vocab_map[prefix.to_s.to_sym] = params.freeze
end

##
# Limits iteration over vocabularies to just those selected
#
Expand Down Expand Up @@ -347,7 +382,8 @@ def expand_pname(pname)
if prefix == "rdf"
RDF[suffix]
elsif vocab_detail = RDF::Vocabulary.vocab_map[prefix.to_sym]
vocab = RDF::Vocabulary.from_sym(vocab_detail[:class_name])
vocab = vocab_detail[:class] ||
RDF::Vocabulary.from_sym(vocab_detail[:class_name])
suffix.to_s.empty? ? vocab.to_uri : vocab[suffix]
else
(RDF::Vocabulary.find_term(pname) rescue nil) || RDF::URI(pname, validate: true)
Expand Down
10 changes: 9 additions & 1 deletion spec/vocabulary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -858,12 +858,20 @@
}.each do |pred, props|
it "emits #{pred}" do
graph = RDF::Graph.new {|g| props[:term].each_statement {|s| g << s}}

expect(graph.map(&:subject)).to all(eql(props[:term]))
expect(graph.query({predicate: props[:predicate]}).map(&:object)).to all(be_a(props[:value].class))
expect(graph.query({predicate: props[:predicate]}).map(&:object)).to include props[:value]
end
end
end
end

context 'Vocabs outside of the RDF::Vocab namespace' do
uri = 'urn:x-bogus:test-vocab:'
TestVocab = Class.new RDF::Vocabulary(uri)
RDF::Vocabulary.register :testvocab, TestVocab
it 'correctly expands the pname of an arbitrary class' do
expect(RDF::Vocabulary.expand_pname('testvocab:test')).to equal(TestVocab.test)
end
end
end

0 comments on commit f92daa7

Please sign in to comment.