Skip to content

Commit 586624a

Browse files
committed
Fix broken RDF::Node.uuid. Note that the :grammar option is deprecated and provide some documentation for the :format option. Requires that either uuid or uuidtools gems be loaded.
Fixes #354.
1 parent a4dcf9a commit 586624a

4 files changed

Lines changed: 42 additions & 10 deletions

File tree

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ group :develop do
1212

1313
gem 'rest-client-components'
1414
gem 'benchmark-ips'
15+
16+
# Soft dependencies
17+
gem 'uuid'
18+
gem 'uuidtools'
1519
end
1620

1721
group :debug do

lib/rdf/model/node.rb

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,28 @@ def self.cache
3030
##
3131
# Returns a blank node with a random UUID-based identifier.
3232
#
33+
# (Depends on availability of either `uuid` or `uuidtools` gems).
34+
#
35+
# @param [:default, :compact] format (:default)
36+
# Formats supported by the UUID generator:
37+
# * `:default` Produces 36 characters, including hyphens separating the UUID value parts
38+
# * `:compact` Produces a 32 digits (hexadecimal) value with no hyphens
39+
# * `:urn` Adds the prefix urn:uuid: to the default format
3340
# @param [Regexp] grammar (nil)
3441
# a grammar specification that the generated UUID must match
42+
# The UUID is generated such that its initial part is guaranteed
43+
# to match the given `grammar`, e.g. `/^[A-Za-z][A-Za-z0-9]*/`.
44+
# Some RDF storage systems (e.g. AllegroGraph) require this.
45+
# Requires that the `uuid` gem be loadable to use `format`
3546
# @return [RDF::Node]
36-
def self.uuid(grammar: nil)
47+
def self.uuid(format: :default, grammar: nil)
3748
case
3849
when grammar
39-
# The UUID is generated such that its initial part is guaranteed
40-
# to match the given `grammar`, e.g. `/^[A-Za-z][A-Za-z0-9]*/`.
41-
# Some RDF storage systems (e.g. AllegroGraph) require this.
42-
# @see http://github.com/bendiken/rdf/pull/43
43-
uuid = RDF::Util::UUID.generate(options) until uuid =~ grammar
50+
warn "[DEPRECATION] The grammar parameter to RDF::Node#uri is deprecated.\n" +
51+
"Called from #{Gem.location_of_caller.join(':')}"
52+
uuid = RDF::Util::UUID.generate(format: format) until uuid =~ grammar
4453
else
45-
uuid = RDF::Util::UUID.generate(options)
54+
uuid = RDF::Util::UUID.generate(format: format)
4655
end
4756
self.new(uuid)
4857
end

lib/rdf/util/uuid.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ module UUID
1414
# [UUIDTools]: http://rubygems.org/gems/uuidtools
1515
#
1616
# @param [:default, :compact, :urn] format (:default)
17-
# @param [Hash{Symbol => Object}] options
18-
# any options to pass through to the underlying UUID library
1917
# @return [String] a UUID string
2018
# @raise [LoadError] if no UUID library is available
2119
# @see http://rubygems.org/gems/uuid
2220
# @see http://rubygems.org/gems/uuidtools
23-
def self.generate(format: :default, **options)
21+
def self.generate(format: :default)
2422
begin
2523
require 'uuid'
2624
::UUID.generate(format)

spec/model_node_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
end
2020
end
2121

22+
describe ".uuid" do
23+
it "generates a BNode" do
24+
expect(described_class.uuid).to be_a_node
25+
end
26+
27+
it "accepts format: :default" do
28+
expect(described_class.uuid(format: :default)).to be_a_node
29+
end
30+
31+
it "accepts format: :compact" do
32+
expect(described_class.uuid(format: :compact)).to be_a_node
33+
end
34+
35+
it "accepts grammar: option with deprecation" do
36+
expect {
37+
expect(described_class.uuid(grammar: /\S+/)).to be_a_node
38+
}.to write('[DEPRECATION]').to(:error)
39+
expect(described_class.uuid(format: :compact)).to be_a_node
40+
end
41+
end
42+
2243
context "as method" do
2344
it "with no args" do
2445
expect(described_class).to receive(:new).with(no_args)

0 commit comments

Comments
 (0)