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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: deb27dba84f5646d65c33ad83bd4ac842724ed0458f943194c572ef06b9ab679
4
- data.tar.gz: 150094cd1b73702b87ada062070d66752bab1c28be502aaad8ca754fa7d51815
3
+ metadata.gz: e5e9ee04b73aea9ca848276b9e7c28efe2e42a7617b4c4be587f26a262d1e487
4
+ data.tar.gz: 90a5036bbb9d090c1859c7fd1069dba484b4533ee3bcbbfb53751e7c792494e5
5
5
  SHA512:
6
- metadata.gz: 49fdd4acc90ca6089385473885b8fc49a9160e7d628eaf53242ab209f9b9858941d533df9966f7a77b853b20e4b203791ac43e58b566443ebab66e4196b18cfc
7
- data.tar.gz: 58f9571d7c07fadbe58ca1e171382ea0a00ed43e86b02236dbba8e496be078a9ec62b29b03ce2d927b6ac609817318320adfa52d0f0049c62755f463ee20fe83
6
+ metadata.gz: 13160ab47776b4fc531de45f341f0d05e7cb5eb04886b541aaf1f6418bf1aeaf5943bd2b9bfca8da0054af2f2cc964a79401757ac3d5482454fb0dd2f1d3dedd
7
+ data.tar.gz: 67da241b365cb8d278b0d0588fe3dc5df93984e1734c0c865d0b76a04e8fb34bfddf93f7357bca85119f649441da768ef104fd46a22479202e278b9c05fca932
data/.gitignore CHANGED
@@ -12,3 +12,4 @@ Gemfile.lock
12
12
  .rspec_status
13
13
  .\#*
14
14
  \#*\#
15
+ *.gem
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --output-dir doc/yard
2
+ --protected
3
+ --no-private
4
+ --hide-void-return
5
+ --markup markdown
6
+ --readme README.md
7
+ -
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source "https://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in rdf-kv.gemspec
4
4
  gemspec
5
-
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
@@ -1,5 +1,5 @@
1
1
  module RDF
2
2
  class KV
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.3"
4
4
  end
5
5
  end
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
- @namespaces[prefix.to_sym] = RDF::Vocabulary.new uri
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: [[-> { "_:#{UUID::NCName.to_ncname_64(
62
- UUIDTools::UUID.random_create.to_s, version: 1) }" }, false]],
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 do |m|
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
- # push the deref
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 = namespaces[prefix.to_sym]
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, :namespaces, :callback
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 [RDF::URI] The default subject. Required.
252
- # @param graph [RDF::URI] The default context. Optional.
253
- # @param namespaces [Hash] Namespace/prefix mappings. Optional.
254
- # @param callback [#call] A callback that expects and returns a term.
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, namespaces: {}, callback: 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, 'namespaces must be hashable' unless
264
- namespaces.respond_to? :to_h
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 = subject
269
- @graph = graph
270
- @callback = callback
271
- @namespaces = DEFAULT_NS.merge(namespaces.to_h.map do |k, v|
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 { |t| resolve_term t }
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, nil, graph_name: g)
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.3.0')
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 'rdf-vocab', '~> 3.1'
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', '>= 3.1.1' # include my changes
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.2.5'
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.0
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: 2020-01-09 00:00:00.000000000 Z
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.1'
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.1'
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.1.1
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.1.1
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.2.5
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.2.5
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.3.0
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.1.2
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: []