json-ld 3.2.3 → 3.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (99) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/lib/json/ld/api.rb +807 -764
  4. data/lib/json/ld/compact.rb +304 -304
  5. data/lib/json/ld/conneg.rb +179 -161
  6. data/lib/json/ld/context.rb +2080 -1945
  7. data/lib/json/ld/expand.rb +745 -666
  8. data/lib/json/ld/extensions.rb +14 -13
  9. data/lib/json/ld/flatten.rb +257 -247
  10. data/lib/json/ld/format.rb +202 -194
  11. data/lib/json/ld/frame.rb +525 -502
  12. data/lib/json/ld/from_rdf.rb +224 -166
  13. data/lib/json/ld/html/nokogiri.rb +123 -121
  14. data/lib/json/ld/html/rexml.rb +151 -147
  15. data/lib/json/ld/reader.rb +107 -100
  16. data/lib/json/ld/resource.rb +224 -205
  17. data/lib/json/ld/streaming_reader.rb +574 -507
  18. data/lib/json/ld/streaming_writer.rb +93 -92
  19. data/lib/json/ld/to_rdf.rb +171 -167
  20. data/lib/json/ld/utils.rb +270 -264
  21. data/lib/json/ld/version.rb +24 -14
  22. data/lib/json/ld/writer.rb +334 -311
  23. data/lib/json/ld.rb +103 -96
  24. metadata +78 -209
  25. data/spec/api_spec.rb +0 -132
  26. data/spec/compact_spec.rb +0 -3482
  27. data/spec/conneg_spec.rb +0 -373
  28. data/spec/context_spec.rb +0 -2036
  29. data/spec/expand_spec.rb +0 -4496
  30. data/spec/flatten_spec.rb +0 -1203
  31. data/spec/format_spec.rb +0 -115
  32. data/spec/frame_spec.rb +0 -2498
  33. data/spec/from_rdf_spec.rb +0 -1005
  34. data/spec/matchers.rb +0 -20
  35. data/spec/rdfstar_spec.rb +0 -25
  36. data/spec/reader_spec.rb +0 -883
  37. data/spec/resource_spec.rb +0 -76
  38. data/spec/spec_helper.rb +0 -281
  39. data/spec/streaming_reader_spec.rb +0 -237
  40. data/spec/streaming_writer_spec.rb +0 -145
  41. data/spec/suite_compact_spec.rb +0 -22
  42. data/spec/suite_expand_spec.rb +0 -36
  43. data/spec/suite_flatten_spec.rb +0 -34
  44. data/spec/suite_frame_spec.rb +0 -29
  45. data/spec/suite_from_rdf_spec.rb +0 -22
  46. data/spec/suite_helper.rb +0 -411
  47. data/spec/suite_html_spec.rb +0 -22
  48. data/spec/suite_http_spec.rb +0 -35
  49. data/spec/suite_remote_doc_spec.rb +0 -22
  50. data/spec/suite_to_rdf_spec.rb +0 -30
  51. data/spec/support/extensions.rb +0 -44
  52. data/spec/test-files/test-1-compacted.jsonld +0 -10
  53. data/spec/test-files/test-1-context.jsonld +0 -7
  54. data/spec/test-files/test-1-expanded.jsonld +0 -5
  55. data/spec/test-files/test-1-input.jsonld +0 -10
  56. data/spec/test-files/test-1-rdf.ttl +0 -8
  57. data/spec/test-files/test-2-compacted.jsonld +0 -20
  58. data/spec/test-files/test-2-context.jsonld +0 -7
  59. data/spec/test-files/test-2-expanded.jsonld +0 -16
  60. data/spec/test-files/test-2-input.jsonld +0 -20
  61. data/spec/test-files/test-2-rdf.ttl +0 -14
  62. data/spec/test-files/test-3-compacted.jsonld +0 -11
  63. data/spec/test-files/test-3-context.jsonld +0 -8
  64. data/spec/test-files/test-3-expanded.jsonld +0 -10
  65. data/spec/test-files/test-3-input.jsonld +0 -11
  66. data/spec/test-files/test-3-rdf.ttl +0 -8
  67. data/spec/test-files/test-4-compacted.jsonld +0 -10
  68. data/spec/test-files/test-4-context.jsonld +0 -7
  69. data/spec/test-files/test-4-expanded.jsonld +0 -6
  70. data/spec/test-files/test-4-input.jsonld +0 -10
  71. data/spec/test-files/test-4-rdf.ttl +0 -5
  72. data/spec/test-files/test-5-compacted.jsonld +0 -13
  73. data/spec/test-files/test-5-context.jsonld +0 -7
  74. data/spec/test-files/test-5-expanded.jsonld +0 -9
  75. data/spec/test-files/test-5-input.jsonld +0 -13
  76. data/spec/test-files/test-5-rdf.ttl +0 -7
  77. data/spec/test-files/test-6-compacted.jsonld +0 -10
  78. data/spec/test-files/test-6-context.jsonld +0 -7
  79. data/spec/test-files/test-6-expanded.jsonld +0 -10
  80. data/spec/test-files/test-6-input.jsonld +0 -10
  81. data/spec/test-files/test-6-rdf.ttl +0 -6
  82. data/spec/test-files/test-7-compacted.jsonld +0 -23
  83. data/spec/test-files/test-7-context.jsonld +0 -4
  84. data/spec/test-files/test-7-expanded.jsonld +0 -20
  85. data/spec/test-files/test-7-input.jsonld +0 -23
  86. data/spec/test-files/test-7-rdf.ttl +0 -14
  87. data/spec/test-files/test-8-compacted.jsonld +0 -34
  88. data/spec/test-files/test-8-context.jsonld +0 -11
  89. data/spec/test-files/test-8-expanded.jsonld +0 -24
  90. data/spec/test-files/test-8-frame.jsonld +0 -18
  91. data/spec/test-files/test-8-framed.jsonld +0 -25
  92. data/spec/test-files/test-8-input.jsonld +0 -30
  93. data/spec/test-files/test-8-rdf.ttl +0 -15
  94. data/spec/test-files/test-9-compacted.jsonld +0 -20
  95. data/spec/test-files/test-9-context.jsonld +0 -13
  96. data/spec/test-files/test-9-expanded.jsonld +0 -14
  97. data/spec/test-files/test-9-input.jsonld +0 -12
  98. data/spec/to_rdf_spec.rb +0 -1551
  99. data/spec/writer_spec.rb +0 -427
data/lib/json/ld/utils.rb CHANGED
@@ -1,305 +1,311 @@
1
- # -*- encoding: utf-8 -*-
2
1
  # frozen_string_literal: true
3
- module JSON::LD
4
- module Utils
5
- ##
6
- # Is value a node? A value is a node if
7
- # * it is a Hash
8
- # * it is not a @value, @set or @list
9
- # * it has more than 1 key or any key is not @id
10
- # @param [Object] value
11
- # @return [Boolean]
12
- def node?(value)
13
- value.is_a?(Hash) &&
14
- !(value.key?('@value') || value.key?('@list') || value.key?('@set')) &&
15
- (value.length > 1 || !value.key?('@id'))
16
- end
17
2
 
18
- ##
19
- # Is value a node reference?
20
- # @param [Object] value
21
- # @return [Boolean]
22
- def node_reference?(value)
23
- value.is_a?(Hash) && value.length == 1 && value.key?('@id')
24
- end
3
+ module JSON
4
+ module LD
5
+ module Utils
6
+ ##
7
+ # Is value a node? A value is a node if
8
+ # * it is a Hash
9
+ # * it is not a @value, @set or @list
10
+ # * it has more than 1 key or any key is not @id
11
+ # @param [Object] value
12
+ # @return [Boolean]
13
+ def node?(value)
14
+ value.is_a?(Hash) &&
15
+ !(value.key?('@value') || value.key?('@list') || value.key?('@set')) &&
16
+ (value.length > 1 || !value.key?('@id'))
17
+ end
25
18
 
26
- ##
27
- # Is value a node or a node reference reference?
28
- # @param [Object] value
29
- # @return [Boolean]
30
- def node_or_ref?(value)
31
- value.is_a?(Hash) &&
32
- !(value.key?('@value') || value.key?('@list') || value.key?('@set'))
33
- end
19
+ ##
20
+ # Is value a node reference?
21
+ # @param [Object] value
22
+ # @return [Boolean]
23
+ def node_reference?(value)
24
+ value.is_a?(Hash) && value.length == 1 && value.key?('@id')
25
+ end
34
26
 
35
- ##
36
- # Is value a blank node? Value is a blank node
37
- #
38
- # @param [Object] value
39
- # @return [Boolean]
40
- def blank_node?(value)
41
- case value
42
- when nil then true
43
- when String then value.start_with?('_:')
44
- else
45
- (node?(value) || node_reference?(value)) && value.fetch('@id', '_:').start_with?('_:')
27
+ ##
28
+ # Is value a node or a node reference reference?
29
+ # @param [Object] value
30
+ # @return [Boolean]
31
+ def node_or_ref?(value)
32
+ value.is_a?(Hash) &&
33
+ !(value.key?('@value') || value.key?('@list') || value.key?('@set'))
46
34
  end
47
- end
48
35
 
49
- ##
50
- # Is value an expaned @graph?
51
- #
52
- # Note: A value is a graph if all of these hold true:
53
- # 1. It is an object.
54
- # 2. It has an `@graph` key.
55
- # 3. It may have '@context', '@id' or '@index'
56
- #
57
- # @param [Object] value
58
- # @return [Boolean]
59
- def graph?(value)
60
- value.is_a?(Hash) && (value.keys - UTIL_GRAPH_KEYS) == ['@graph']
61
- end
36
+ ##
37
+ # Is value a blank node? Value is a blank node
38
+ #
39
+ # @param [Object] value
40
+ # @return [Boolean]
41
+ def blank_node?(value)
42
+ case value
43
+ when nil then true
44
+ when String then value.start_with?('_:')
45
+ else
46
+ (node?(value) || node_reference?(value)) && value.fetch('@id', '_:').start_with?('_:')
47
+ end
48
+ end
62
49
 
63
- ##
64
- # Is value a simple graph (lacking @id)?
65
- #
66
- # @param [Object] value
67
- # @return [Boolean]
68
- def simple_graph?(value)
69
- graph?(value) && !value.key?('@id')
70
- end
71
-
72
- ##
73
- # Is value an expaned @list?
74
- #
75
- # @param [Object] value
76
- # @return [Boolean]
77
- def list?(value)
78
- value.is_a?(Hash) && value.key?('@list')
79
- end
50
+ ##
51
+ # Is value an expaned @graph?
52
+ #
53
+ # Note: A value is a graph if all of these hold true:
54
+ # 1. It is an object.
55
+ # 2. It has an `@graph` key.
56
+ # 3. It may have '@context', '@id' or '@index'
57
+ #
58
+ # @param [Object] value
59
+ # @return [Boolean]
60
+ def graph?(value)
61
+ value.is_a?(Hash) && (value.keys - UTIL_GRAPH_KEYS) == ['@graph']
62
+ end
80
63
 
81
- ##
82
- # Is value annotated?
83
- #
84
- # @param [Object] value
85
- # @return [Boolean]
86
- def index?(value)
87
- value.is_a?(Hash) && value.key?('@index')
88
- end
64
+ ##
65
+ # Is value a simple graph (lacking @id)?
66
+ #
67
+ # @param [Object] value
68
+ # @return [Boolean]
69
+ def simple_graph?(value)
70
+ graph?(value) && !value.key?('@id')
71
+ end
89
72
 
90
- ##
91
- # Is value literal?
92
- #
93
- # @param [Object] value
94
- # @return [Boolean]
95
- def value?(value)
96
- value.is_a?(Hash) && value.key?('@value')
97
- end
73
+ ##
74
+ # Is value an expaned @list?
75
+ #
76
+ # @param [Object] value
77
+ # @return [Boolean]
78
+ def list?(value)
79
+ value.is_a?(Hash) && value.key?('@list')
80
+ end
98
81
 
99
- ##
100
- # Represent an id as an IRI or Blank Node
101
- # @param [String] id
102
- # @param [RDF::URI] base (nil)
103
- # @return [RDF::Resource]
104
- def as_resource(id, base = nil)
105
- @nodes ||= {} # Re-use BNodes
106
- if id.start_with?('_:')
107
- (@nodes[id] ||= RDF::Node.new(namer.get_sym(id)))
108
- elsif base
109
- base.join(id)
110
- else
111
- RDF::URI(id)
82
+ ##
83
+ # Is value annotated?
84
+ #
85
+ # @param [Object] value
86
+ # @return [Boolean]
87
+ def index?(value)
88
+ value.is_a?(Hash) && value.key?('@index')
112
89
  end
113
- end
114
90
 
115
- ##
116
- # Represent as an array
117
- # @param [Object] object
118
- # @return [Array<Object>]
119
- def as_array(object)
120
- object.is_a?(Array) ? object : [object]
121
- end
91
+ ##
92
+ # Is value literal?
93
+ #
94
+ # @param [Object] value
95
+ # @return [Boolean]
96
+ def value?(value)
97
+ value.is_a?(Hash) && value.key?('@value')
98
+ end
122
99
 
123
- ##
124
- # Compares two JSON-LD values for equality. Two JSON-LD values will be
125
- # considered equal if:
126
- #
127
- # 1. They are both primitives of the same type and value.
128
- # 2. They are both @values with the same @value, @type, @language,
129
- # and @index, OR
130
- # 3. They both have @ids that are the same.
131
- #
132
- # @param [Object] v1 the first value.
133
- # @param [Object] v2 the second value.
134
- #
135
- # @return [Boolean] v1 and v2 are considered equal
136
- def compare_values(v1, v2)
137
- case
138
- when node_or_ref?(v1) && node_or_ref?(v2) then v1['@id'] && v1['@id'] == v2['@id']
139
- when value?(v1) && value?(v2)
140
- v1['@value'] == v2['@value'] &&
141
- v1['@type'] == v2['@type'] &&
142
- v1['@language'] == v2['@language'] &&
143
- v1['@index'] == v2['@index']
144
- else
145
- v1 == v2
100
+ ##
101
+ # Represent an id as an IRI or Blank Node
102
+ # @param [String] id
103
+ # @param [RDF::URI] base (nil)
104
+ # @return [RDF::Resource]
105
+ def as_resource(id, base = nil)
106
+ @nodes ||= {} # Re-use BNodes
107
+ if id.start_with?('_:')
108
+ (@nodes[id] ||= RDF::Node.new(namer.get_sym(id)))
109
+ elsif base
110
+ base.join(id)
111
+ else
112
+ RDF::URI(id)
113
+ end
114
+ end
115
+
116
+ ##
117
+ # Represent as an array
118
+ # @param [Object] object
119
+ # @return [Array<Object>]
120
+ def as_array(object)
121
+ object.is_a?(Array) ? object : [object]
146
122
  end
147
- end
148
123
 
149
- # Adds a value to a subject. If the value is an array, all values in the
150
- # array will be added.
151
- #
152
- # @param [Hash] subject the hash to add the value to.
153
- # @param [String] property the property that relates the value to the subject.
154
- # @param [Object] value the value to add.
155
- # @param [Boolean] property_is_array (false)
156
- # true if the property is always an array, false if not.
157
- # @param [Boolean] value_is_array (false)
158
- # true if the value to be added should be preserved as an array (lists)
159
- # @param [Boolean] allow_duplicate (true)
160
- # true to allow duplicates, false not to (uses
161
- # a simple shallow comparison of subject ID or value).
162
- def add_value(subject, property, value, property_is_array: false, value_is_array: false, allow_duplicate: true)
163
- if value_is_array
164
- subject[property] = value
165
- elsif value.is_a?(Array)
166
- subject[property] = [] if value.empty? && property_is_array
167
- value.each do |v|
168
- add_value(subject, property, v,
169
- property_is_array: property_is_array, allow_duplicate: allow_duplicate)
124
+ ##
125
+ # Compares two JSON-LD values for equality. Two JSON-LD values will be
126
+ # considered equal if:
127
+ #
128
+ # 1. They are both primitives of the same type and value.
129
+ # 2. They are both @values with the same @value, @type, @language,
130
+ # and @index, OR
131
+ # 3. They both have @ids that are the same.
132
+ #
133
+ # @param [Object] v1 the first value.
134
+ # @param [Object] v2 the second value.
135
+ #
136
+ # @return [Boolean] v1 and v2 are considered equal
137
+ def compare_values(v1, v2)
138
+ if node_or_ref?(v1) && node_or_ref?(v2)
139
+ v1['@id'] && v1['@id'] == v2['@id']
140
+ elsif value?(v1) && value?(v2)
141
+ v1['@value'] == v2['@value'] &&
142
+ v1['@type'] == v2['@type'] &&
143
+ v1['@language'] == v2['@language'] &&
144
+ v1['@index'] == v2['@index']
145
+ else
146
+ v1 == v2
170
147
  end
171
- elsif subject[property]
172
- # check if subject already has value if duplicates not allowed
173
- _has_value = !allow_duplicate && has_value?(subject, property, value)
148
+ end
149
+
150
+ # Adds a value to a subject. If the value is an array, all values in the
151
+ # array will be added.
152
+ #
153
+ # @param [Hash] subject the hash to add the value to.
154
+ # @param [String] property the property that relates the value to the subject.
155
+ # @param [Object] value the value to add.
156
+ # @param [Boolean] property_is_array (false)
157
+ # true if the property is always an array, false if not.
158
+ # @param [Boolean] value_is_array (false)
159
+ # true if the value to be added should be preserved as an array (lists)
160
+ # @param [Boolean] allow_duplicate (true)
161
+ # true to allow duplicates, false not to (uses
162
+ # a simple shallow comparison of subject ID or value).
163
+ def add_value(subject, property, value, property_is_array: false, value_is_array: false, allow_duplicate: true)
164
+ if value_is_array
165
+ subject[property] = value
166
+ elsif value.is_a?(Array)
167
+ subject[property] = [] if value.empty? && property_is_array
168
+ value.each do |v|
169
+ add_value(subject, property, v,
170
+ property_is_array: property_is_array, allow_duplicate: allow_duplicate)
171
+ end
172
+ elsif subject[property]
173
+ # check if subject already has value if duplicates not allowed
174
+ _has_value = !allow_duplicate && has_value?(subject, property, value)
174
175
 
175
- # make property an array if value not present or always an array
176
- if !subject[property].is_a?(Array) && (!_has_value || property_is_array)
177
- subject[property] = [subject[property]]
176
+ # make property an array if value not present or always an array
177
+ if !subject[property].is_a?(Array) && (!_has_value || property_is_array)
178
+ subject[property] =
179
+ [subject[property]]
180
+ end
181
+ subject[property] << value unless _has_value
182
+ else
183
+ subject[property] = property_is_array ? [value] : value
178
184
  end
179
- subject[property] << value unless _has_value
180
- else
181
- subject[property] = property_is_array ? [value] : value
182
185
  end
183
- end
184
186
 
185
- # Returns True if the given subject has the given property.
186
- #
187
- # @param subject the subject to check.
188
- # @param property the property to look for.
189
- #
190
- # @return [Boolean] true if the subject has the given property, false if not.
191
- def property?(subject, property)
192
- return false unless value = subject[property]
193
- !value.is_a?(Array) || !value.empty?
194
- end
187
+ # Returns True if the given subject has the given property.
188
+ #
189
+ # @param subject the subject to check.
190
+ # @param property the property to look for.
191
+ #
192
+ # @return [Boolean] true if the subject has the given property, false if not.
193
+ def property?(subject, property)
194
+ return false unless (value = subject[property])
195
+
196
+ !value.is_a?(Array) || !value.empty?
197
+ end
195
198
 
196
- # Determines if the given value is a property of the given subject.
197
- #
198
- # @param [Hash] subject the subject to check.
199
- # @param [String] property the property to check.
200
- # @param [Object] value the value to check.
201
- #
202
- # @return [Boolean] true if the value exists, false if not.
203
- def has_value?(subject, property, value)
204
- if property?(subject, property)
205
- val = subject[property]
206
- is_list = list?(val)
207
- if val.is_a?(Array) || is_list
208
- val = val['@list'] if is_list
209
- val.any? {|v| compare_values(value, v)}
210
- elsif !val.is_a?(Array)
211
- compare_values(value, val)
199
+ # Determines if the given value is a property of the given subject.
200
+ #
201
+ # @param [Hash] subject the subject to check.
202
+ # @param [String] property the property to check.
203
+ # @param [Object] value the value to check.
204
+ #
205
+ # @return [Boolean] true if the value exists, false if not.
206
+ def has_value?(subject, property, value)
207
+ if property?(subject, property)
208
+ val = subject[property]
209
+ is_list = list?(val)
210
+ if val.is_a?(Array) || is_list
211
+ val = val['@list'] if is_list
212
+ val.any? { |v| compare_values(value, v) }
213
+ elsif !val.is_a?(Array)
214
+ compare_values(value, val)
215
+ else
216
+ false
217
+ end
212
218
  else
213
219
  false
214
220
  end
215
- else
216
- false
217
221
  end
218
- end
219
222
 
220
- private
221
- UTIL_GRAPH_KEYS = %w(@context @id @index).freeze
223
+ private
224
+
225
+ UTIL_GRAPH_KEYS = %w[@context @id @index].freeze
226
+
227
+ # Merge the last value into an array based for the specified key if hash is not null and value is not already in that array
228
+ def merge_value(hash, key, value)
229
+ return unless hash
222
230
 
223
- # Merge the last value into an array based for the specified key if hash is not null and value is not already in that array
224
- def merge_value(hash, key, value)
225
- return unless hash
226
- values = hash[key] ||= []
227
- if key == '@list'
228
- values << value
229
- elsif list?(value)
230
- values << value
231
- elsif !values.include?(value)
232
- values << value
231
+ values = hash[key] ||= []
232
+ if key == '@list'
233
+ values << value
234
+ elsif list?(value)
235
+ values << value
236
+ elsif !values.include?(value)
237
+ values << value
238
+ end
233
239
  end
234
240
  end
235
- end
236
241
 
237
- ##
238
- # Utility class for mapping old blank node identifiers, or unnamed blank
239
- # nodes to new identifiers
240
- class BlankNodeMapper < Hash
241
242
  ##
242
- # Just return a Blank Node based on `old`. Manufactures
243
- # a node if `old` is nil or empty
244
- # @param [String] old ("")
245
- # @return [String]
246
- def get_sym(old = "")
247
- old = RDF::Node.new.to_s if old.to_s.empty?
248
- old.to_s.sub(/_:/, '')
249
- end
243
+ # Utility class for mapping old blank node identifiers, or unnamed blank
244
+ # nodes to new identifiers
245
+ class BlankNodeMapper < Hash
246
+ ##
247
+ # Just return a Blank Node based on `old`. Manufactures
248
+ # a node if `old` is nil or empty
249
+ # @param [String] old ("")
250
+ # @return [String]
251
+ def get_sym(old = "")
252
+ old = RDF::Node.new.to_s if old.to_s.empty?
253
+ old.to_s.sub(/_:/, '')
254
+ end
250
255
 
251
- ##
252
- # Get a new mapped name for `old`
253
- #
254
- # @param [String] old ("")
255
- # @return [String]
256
- def get_name(old = "")
257
- "_:" + get_sym(old)
256
+ ##
257
+ # Get a new mapped name for `old`
258
+ #
259
+ # @param [String] old ("")
260
+ # @return [String]
261
+ def get_name(old = "")
262
+ "_:" + get_sym(old)
263
+ end
258
264
  end
259
- end
260
265
 
261
- class BlankNodeUniqer < BlankNodeMapper
262
- ##
263
- # Use the uniquely generated bnodes, rather than a sequence
264
- # @param [String] old ("")
265
- # @return [String]
266
- def get_sym(old = "")
267
- old = old.to_s.sub(/_:/, '')
268
- if old && self.key?(old)
269
- self[old]
270
- elsif !old.empty?
271
- self[old] = RDF::Node.new.to_unique_base[2..-1]
272
- else
273
- RDF::Node.new.to_unique_base[2..-1]
266
+ class BlankNodeUniqer < BlankNodeMapper
267
+ ##
268
+ # Use the uniquely generated bnodes, rather than a sequence
269
+ # @param [String] old ("")
270
+ # @return [String]
271
+ def get_sym(old = "")
272
+ old = old.to_s.sub(/_:/, '')
273
+ if old && key?(old)
274
+ self[old]
275
+ elsif !old.empty?
276
+ self[old] = RDF::Node.new.to_unique_base[2..]
277
+ else
278
+ RDF::Node.new.to_unique_base[2..]
279
+ end
274
280
  end
275
281
  end
276
- end
277
282
 
278
- class BlankNodeNamer < BlankNodeMapper
279
- # @param [String] prefix
280
- def initialize(prefix)
281
- @prefix = prefix.to_s
282
- @num = 0
283
- super
284
- end
283
+ class BlankNodeNamer < BlankNodeMapper
284
+ # @param [String] prefix
285
+ def initialize(prefix)
286
+ @prefix = prefix.to_s
287
+ @num = 0
288
+ super
289
+ end
285
290
 
286
- ##
287
- # Get a new symbol mapped from `old`
288
- # @param [String] old ("")
289
- # @return [String]
290
- def get_sym(old = "")
291
- old = old.to_s.sub(/_:/, '')
292
- if !old.empty? && self.key?(old)
293
- self[old]
294
- elsif !old.empty?
295
- @num += 1
296
- #puts "allocate #{@prefix + (@num - 1).to_s} to #{old.inspect}"
297
- self[old] = @prefix + (@num - 1).to_s
298
- else
299
- # Not referenced, just return a new unique value
300
- @num += 1
301
- #puts "allocate #{@prefix + (@num - 1).to_s} to #{old.inspect}"
302
- @prefix + (@num - 1).to_s
291
+ ##
292
+ # Get a new symbol mapped from `old`
293
+ # @param [String] old ("")
294
+ # @return [String]
295
+ def get_sym(old = "")
296
+ old = old.to_s.sub(/_:/, '')
297
+ if !old.empty? && key?(old)
298
+ self[old]
299
+ elsif !old.empty?
300
+ @num += 1
301
+ # puts "allocate #{@prefix + (@num - 1).to_s} to #{old.inspect}"
302
+ self[old] = @prefix + (@num - 1).to_s
303
+ else
304
+ # Not referenced, just return a new unique value
305
+ @num += 1
306
+ # puts "allocate #{@prefix + (@num - 1).to_s} to #{old.inspect}"
307
+ @prefix + (@num - 1).to_s
308
+ end
303
309
  end
304
310
  end
305
311
  end
@@ -1,20 +1,30 @@
1
- # -*- encoding: utf-8 -*-
2
1
  # frozen_string_literal: true
3
- module JSON::LD::VERSION
4
- VERSION_FILE = File.join(File.expand_path(File.dirname(__FILE__)), "..", "..", "..", "VERSION")
5
- MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chomp.split(".")
6
2
 
7
- STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
3
+ module JSON
4
+ module LD
5
+ module VERSION
6
+ VERSION_FILE = File.join(__dir__, "..", "..", "..", "VERSION")
7
+ MAJOR, MINOR, TINY, EXTRA = File.read(VERSION_FILE).chomp.split(".")
8
8
 
9
- ##
10
- # @return [String]
11
- def self.to_s() STRING end
9
+ STRING = [MAJOR, MINOR, TINY, EXTRA].compact.join('.')
12
10
 
13
- ##
14
- # @return [String]
15
- def self.to_str() STRING end
11
+ ##
12
+ # @return [String]
13
+ def self.to_s
14
+ STRING
15
+ end
16
16
 
17
- ##
18
- # @return [Array(Integer, Integer, Integer)]
19
- def self.to_a() STRING.split(".") end
17
+ ##
18
+ # @return [String]
19
+ def self.to_str
20
+ STRING
21
+ end
22
+
23
+ ##
24
+ # @return [Array(Integer, Integer, Integer)]
25
+ def self.to_a
26
+ STRING.split(".")
27
+ end
28
+ end
29
+ end
20
30
  end