rdf-kv 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.yardopts +7 -0
- data/Gemfile +0 -3
- data/lib/rdf/kv/version.rb +1 -1
- data/lib/rdf/kv.rb +47 -30
- data/lib/rdf-kv.rb +1 -0
- data/rdf-kv.gemspec +6 -4
- metadata +45 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5e9ee04b73aea9ca848276b9e7c28efe2e42a7617b4c4be587f26a262d1e487
|
4
|
+
data.tar.gz: 90a5036bbb9d090c1859c7fd1069dba484b4533ee3bcbbfb53751e7c792494e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 13160ab47776b4fc531de45f341f0d05e7cb5eb04886b541aaf1f6418bf1aeaf5943bd2b9bfca8da0054af2f2cc964a79401757ac3d5482454fb0dd2f1d3dedd
|
7
|
+
data.tar.gz: 67da241b365cb8d278b0d0588fe3dc5df93984e1734c0c865d0b76a04e8fb34bfddf93f7357bca85119f649441da768ef104fd46a22479202e278b9c05fca932
|
data/.gitignore
CHANGED
data/.yardopts
ADDED
data/Gemfile
CHANGED
data/lib/rdf/kv/version.rb
CHANGED
data/lib/rdf/kv.rb
CHANGED
@@ -35,7 +35,7 @@ class RDF::KV
|
|
35
35
|
|
36
36
|
GRAMMAR = /#{PARTIAL_STMT}/o
|
37
37
|
MAP = %i[modifier term1 term2 designator term1 designator graph
|
38
|
-
term1 term2 designator graph deref]
|
38
|
+
term1 term2 designator graph deref].freeze
|
39
39
|
|
40
40
|
# these should be instance_exec'd
|
41
41
|
SPECIALS = {
|
@@ -47,10 +47,14 @@ class RDF::KV
|
|
47
47
|
val.each do |v, _|
|
48
48
|
next unless m = /^\s*(#{NCNAME}):\s+(.*)$/o.match(v)
|
49
49
|
prefix, uri = m.captures
|
50
|
-
@
|
50
|
+
@prefixes[prefix.to_sym] = RDF::Vocabulary.new uri
|
51
51
|
end
|
52
52
|
},
|
53
|
-
}
|
53
|
+
}.freeze
|
54
|
+
|
55
|
+
def random_uuid_ncname
|
56
|
+
UUID::NCName.to_ncname_64 UUIDTools::UUID.random_create.to_s, version: 1
|
57
|
+
end
|
54
58
|
|
55
59
|
# macros are initially represented as a pair: the macro value and a
|
56
60
|
# flag denoting whether or not the macro itself contains macros and to
|
@@ -58,9 +62,8 @@ class RDF::KV
|
|
58
62
|
GENERATED = {
|
59
63
|
NEW_UUID: [[-> { UUIDTools::UUID.random_create.to_s }, false]],
|
60
64
|
NEW_UUID_URN: [[-> { UUIDTools::UUID.random_create.to_uri }, false]],
|
61
|
-
NEW_BNODE: [[-> { "_:#{
|
62
|
-
|
63
|
-
}
|
65
|
+
NEW_BNODE: [[-> { "_:#{random_uuid_ncname}" }, false]],
|
66
|
+
}.freeze
|
64
67
|
|
65
68
|
# just the classics
|
66
69
|
DEFAULT_NS = {
|
@@ -70,15 +73,14 @@ class RDF::KV
|
|
70
73
|
xsd: RDF::XSD,
|
71
74
|
}.freeze
|
72
75
|
|
76
|
+
# Given a (massaged) set of macros, dereference the given array of
|
77
|
+
# strings and return it.
|
73
78
|
def deref_content strings, macros
|
74
79
|
strings = [strings] unless strings.is_a? Array
|
75
80
|
# bail out early if there is nothing to do
|
76
81
|
return strings unless strings.any? { |s| /#{MACRO}/o.match s }
|
77
82
|
out = []
|
78
83
|
strings.each do |s|
|
79
|
-
# sometimes these are arrays of arrays
|
80
|
-
#s = s.first if s.is_a? Array
|
81
|
-
|
82
84
|
# chunks are parallel output; each element is a value
|
83
85
|
chunks = []
|
84
86
|
s.scan(/\G#{MACROS}/o) do |m|
|
@@ -125,6 +127,10 @@ class RDF::KV
|
|
125
127
|
out
|
126
128
|
end
|
127
129
|
|
130
|
+
# Given the structure of macro declarations, dereference any
|
131
|
+
# recursively-defined macros, and return a new structure with a key
|
132
|
+
# and array of _values_, rather than an array of `[value, deref]`
|
133
|
+
# pairs.
|
128
134
|
def massage_macros macros
|
129
135
|
seen = {}
|
130
136
|
done = GENERATED.transform_values { |v| v.map { |w| w.first } }
|
@@ -147,9 +153,7 @@ class RDF::KV
|
|
147
153
|
next unless deref
|
148
154
|
|
149
155
|
if deref.is_a? Array
|
150
|
-
deref.each
|
151
|
-
done[m] ? dm[m] = true : pm[m] = true
|
152
|
-
end
|
156
|
+
deref.each { |m| done[m] ? dm[m] = true : pm[m] = true }
|
153
157
|
else
|
154
158
|
m = {}
|
155
159
|
val.scan(/#{MACRO}/o).compact.each do |x|
|
@@ -161,7 +165,8 @@ class RDF::KV
|
|
161
165
|
|
162
166
|
done[m] ? dm[m] = true : pm[m] = true
|
163
167
|
end
|
164
|
-
|
168
|
+
|
169
|
+
# replace the deref flag with the elements to deref with
|
165
170
|
pair[1] = m.empty? ? false : m.keys.sort
|
166
171
|
end
|
167
172
|
end
|
@@ -174,6 +179,7 @@ class RDF::KV
|
|
174
179
|
q << m
|
175
180
|
end
|
176
181
|
|
182
|
+
# put the current key back on the queue but put the dependencies first
|
177
183
|
queue = q + [k] + queue
|
178
184
|
next
|
179
185
|
end
|
@@ -205,7 +211,7 @@ class RDF::KV
|
|
205
211
|
# ugh now we gotta do urls
|
206
212
|
if m = /^(#{NCNAME}):(\S*)$/o.match(term)
|
207
213
|
prefix, slug = m.captures
|
208
|
-
if !slug.start_with?(?/) and vocab =
|
214
|
+
if !slug.start_with?(?/) and vocab = prefixes[prefix.to_sym]
|
209
215
|
return vocab[slug]
|
210
216
|
end
|
211
217
|
end
|
@@ -238,37 +244,39 @@ class RDF::KV
|
|
238
244
|
|
239
245
|
# call the callback if we have one
|
240
246
|
term = callback.call term if callback
|
241
|
-
|
247
|
+
|
242
248
|
term
|
243
249
|
end
|
244
250
|
|
245
251
|
public
|
246
252
|
|
247
|
-
attr_reader :subject, :graph, :
|
253
|
+
attr_reader :subject, :graph, :prefixes, :callback
|
254
|
+
# why is this :target, :source
|
255
|
+
alias_method :namespaces, :prefixes
|
248
256
|
|
249
257
|
# Initialize the processor.
|
250
258
|
#
|
251
|
-
# @param subject
|
252
|
-
# @param graph
|
253
|
-
# @param
|
254
|
-
# @param callback
|
259
|
+
# @param subject [RDF::URI] The default subject. Required.
|
260
|
+
# @param graph [RDF::URI] The default context. Optional.
|
261
|
+
# @param prefixes [Hash] Namespace/prefix mappings. Optional.
|
262
|
+
# @param callback [#call] A callback that expects and returns a term.
|
255
263
|
# Optional.
|
256
264
|
#
|
257
|
-
def initialize subject: nil, graph: nil,
|
265
|
+
def initialize subject: nil, graph: nil, prefixes: {}, callback: nil
|
258
266
|
# look at all of our pretty assertions
|
259
267
|
raise ArgumentError, 'subject must be an RDF::Resource' unless
|
260
268
|
subject.is_a? RDF::Resource
|
261
269
|
raise ArgumentError, 'graph must be an RDF::Resource' unless
|
262
270
|
graph.nil? or graph.is_a? RDF::Resource
|
263
|
-
raise ArgumentError, '
|
264
|
-
|
271
|
+
raise ArgumentError, 'prefixes must be hashable' unless
|
272
|
+
prefixes.respond_to? :to_h
|
265
273
|
rase ArgumentError, 'callback must be callable' unless
|
266
274
|
callback.nil? or callback.respond_to? :call
|
267
275
|
|
268
|
-
@subject
|
269
|
-
@graph
|
270
|
-
@callback
|
271
|
-
@
|
276
|
+
@subject = subject
|
277
|
+
@graph = graph
|
278
|
+
@callback = callback
|
279
|
+
@prefixes = DEFAULT_NS.merge(prefixes.to_h.map do |k, v|
|
272
280
|
k = k.to_s.to_sym unless k.is_a? Symbol
|
273
281
|
# coerce to uri
|
274
282
|
v = RDF::URI(v.to_s) unless v.is_a? RDF::Resource
|
@@ -327,7 +335,7 @@ class RDF::KV
|
|
327
335
|
end
|
328
336
|
rescue Exception => e
|
329
337
|
# again this should be nicer
|
330
|
-
raise e
|
338
|
+
raise Error.new e
|
331
339
|
end
|
332
340
|
|
333
341
|
# this will be our output
|
@@ -384,7 +392,10 @@ class RDF::KV
|
|
384
392
|
o = contents[:term2] || subject
|
385
393
|
else
|
386
394
|
s, p = (contents[:term2] ? contents.values_at(:term1, :term2) :
|
387
|
-
[subject, contents[:term1]]).map
|
395
|
+
[subject, contents[:term1]]).map do |t|
|
396
|
+
t = resolve_term t
|
397
|
+
callback ? callback.call(t) : t
|
398
|
+
end
|
388
399
|
end
|
389
400
|
|
390
401
|
# the operation depends on whether the `-` modifier is present
|
@@ -394,9 +405,13 @@ class RDF::KV
|
|
394
405
|
# string then we're deleting a wildcard, same if we `=` overwrite
|
395
406
|
if !reverse and op == :delete && values.include?('') ||
|
396
407
|
contents[:modifier][?=]
|
408
|
+
|
409
|
+
# create a random variable name so we don't pass in a nil
|
410
|
+
var = RDF::Query::Variable.new random_uuid_ncname
|
411
|
+
|
397
412
|
# i can't remember why we don't do this in reverse, probably
|
398
413
|
# because it is too easy to shoot yourself in the foot
|
399
|
-
patch.delete RDF::Statement(s, p,
|
414
|
+
patch.delete RDF::Statement(s, p, var, graph_name: g)
|
400
415
|
|
401
416
|
# nuke these since it will be pointless to evaluate further
|
402
417
|
values.clear if op == :delete
|
@@ -418,4 +433,6 @@ class RDF::KV
|
|
418
433
|
|
419
434
|
patch
|
420
435
|
end
|
436
|
+
|
437
|
+
class Error < RuntimeError; end
|
421
438
|
end
|
data/lib/rdf-kv.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rdf/kv'
|
data/rdf-kv.gemspec
CHANGED
@@ -29,16 +29,18 @@ RDF::Changeset.
|
|
29
29
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
30
|
spec.require_paths = ['lib']
|
31
31
|
|
32
|
-
spec.required_ruby_version = Gem::Requirement.new('>= 2.
|
32
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.7')
|
33
33
|
# dev/test dependencies
|
34
34
|
spec.add_development_dependency 'bundler', '~> 2'
|
35
|
-
spec.add_development_dependency '
|
35
|
+
spec.add_development_dependency 'rake', '~> 13'
|
36
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
37
|
+
spec.add_development_dependency 'rdf-vocab', '~> 3'
|
36
38
|
|
37
39
|
# stuff we use
|
38
|
-
spec.add_runtime_dependency 'rdf', '
|
40
|
+
spec.add_runtime_dependency 'rdf', '~> 3' # include my changes
|
39
41
|
spec.add_runtime_dependency 'uuidtools', '~> 2'
|
40
42
|
|
41
43
|
# stuff i wrote
|
42
|
-
spec.add_runtime_dependency 'uuid-ncname', '>= 0.
|
44
|
+
spec.add_runtime_dependency 'uuid-ncname', '>= 0.4'
|
43
45
|
|
44
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdf-kv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dorian Taylor
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-02-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,34 +24,62 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: rdf-vocab
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
30
58
|
requirements:
|
31
59
|
- - "~>"
|
32
60
|
- !ruby/object:Gem::Version
|
33
|
-
version: '3
|
61
|
+
version: '3'
|
34
62
|
type: :development
|
35
63
|
prerelease: false
|
36
64
|
version_requirements: !ruby/object:Gem::Requirement
|
37
65
|
requirements:
|
38
66
|
- - "~>"
|
39
67
|
- !ruby/object:Gem::Version
|
40
|
-
version: '3
|
68
|
+
version: '3'
|
41
69
|
- !ruby/object:Gem::Dependency
|
42
70
|
name: rdf
|
43
71
|
requirement: !ruby/object:Gem::Requirement
|
44
72
|
requirements:
|
45
|
-
- - "
|
73
|
+
- - "~>"
|
46
74
|
- !ruby/object:Gem::Version
|
47
|
-
version: 3
|
75
|
+
version: '3'
|
48
76
|
type: :runtime
|
49
77
|
prerelease: false
|
50
78
|
version_requirements: !ruby/object:Gem::Requirement
|
51
79
|
requirements:
|
52
|
-
- - "
|
80
|
+
- - "~>"
|
53
81
|
- !ruby/object:Gem::Version
|
54
|
-
version: 3
|
82
|
+
version: '3'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
84
|
name: uuidtools
|
57
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -72,14 +100,14 @@ dependencies:
|
|
72
100
|
requirements:
|
73
101
|
- - ">="
|
74
102
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.
|
103
|
+
version: '0.4'
|
76
104
|
type: :runtime
|
77
105
|
prerelease: false
|
78
106
|
version_requirements: !ruby/object:Gem::Requirement
|
79
107
|
requirements:
|
80
108
|
- - ">="
|
81
109
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
110
|
+
version: '0.4'
|
83
111
|
description: |
|
84
112
|
This module implements https://doriantaylor.com/rdf-kv, taking
|
85
113
|
key-value input (e.g. from a Web form) and converting it into an
|
@@ -93,12 +121,14 @@ files:
|
|
93
121
|
- ".gitignore"
|
94
122
|
- ".rspec"
|
95
123
|
- ".travis.yml"
|
124
|
+
- ".yardopts"
|
96
125
|
- Gemfile
|
97
126
|
- LICENSE
|
98
127
|
- README.md
|
99
128
|
- Rakefile
|
100
129
|
- bin/console
|
101
130
|
- bin/setup
|
131
|
+
- lib/rdf-kv.rb
|
102
132
|
- lib/rdf/kv.rb
|
103
133
|
- lib/rdf/kv/version.rb
|
104
134
|
- rdf-kv.gemspec
|
@@ -107,7 +137,7 @@ licenses:
|
|
107
137
|
- Apache-2.0
|
108
138
|
metadata:
|
109
139
|
homepage_uri: https://github.com/doriantaylor/rb-rdf-kv
|
110
|
-
post_install_message:
|
140
|
+
post_install_message:
|
111
141
|
rdoc_options: []
|
112
142
|
require_paths:
|
113
143
|
- lib
|
@@ -115,15 +145,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
145
|
requirements:
|
116
146
|
- - ">="
|
117
147
|
- !ruby/object:Gem::Version
|
118
|
-
version: 2.
|
148
|
+
version: '2.7'
|
119
149
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
150
|
requirements:
|
121
151
|
- - ">="
|
122
152
|
- !ruby/object:Gem::Version
|
123
153
|
version: '0'
|
124
154
|
requirements: []
|
125
|
-
rubygems_version: 3.
|
126
|
-
signing_key:
|
155
|
+
rubygems_version: 3.3.15
|
156
|
+
signing_key:
|
127
157
|
specification_version: 4
|
128
158
|
summary: Ruby implementation of the RDF-KV protocol
|
129
159
|
test_files: []
|