rdf 2.1.1 → 2.2.0.pre.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -182,15 +182,14 @@ module RDF::NTriples
182
182
  #
183
183
  # @param [IO, File] output
184
184
  # the output stream
185
- # @param [Hash{Symbol => Object}] options = ({})
186
- # any additional options. See {RDF::Writer#initialize}
187
- # @option options [Boolean] :validate (true)
185
+ # @param [Boolean] validate (true)
188
186
  # whether to validate terms when serializing
187
+ # @param [Hash{Symbol => Object}] options ({})
188
+ # any additional options. See {RDF::Writer#initialize}
189
189
  # @yield [writer] `self`
190
190
  # @yieldparam [RDF::Writer] writer
191
191
  # @yieldreturn [void]
192
- def initialize(output = $stdout, options = {}, &block)
193
- options = {validate: true}.merge(options)
192
+ def initialize(output = $stdout, validate: true, **options, &block)
194
193
  super
195
194
  end
196
195
 
@@ -218,10 +217,10 @@ module RDF::NTriples
218
217
  # Returns the N-Triples representation of a statement.
219
218
  #
220
219
  # @param [RDF::Statement] statement
221
- # @param [Hash{Symbol => Object}] options = ({})
220
+ # @param [Hash{Symbol => Object}] options ({})
222
221
  # @return [String]
223
- def format_statement(statement, options = {})
224
- format_triple(*statement.to_triple, options)
222
+ def format_statement(statement, **options)
223
+ format_triple(*statement.to_triple, **options)
225
224
  end
226
225
 
227
226
  ##
@@ -230,31 +229,31 @@ module RDF::NTriples
230
229
  # @param [RDF::Resource] subject
231
230
  # @param [RDF::URI] predicate
232
231
  # @param [RDF::Term] object
233
- # @param [Hash{Symbol => Object}] options = ({})
232
+ # @param [Hash{Symbol => Object}] options ({})
234
233
  # @return [String]
235
- def format_triple(subject, predicate, object, options = {})
236
- "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value, options) }
234
+ def format_triple(subject, predicate, object, **options)
235
+ "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value, **options) }
237
236
  end
238
237
 
239
238
  ##
240
239
  # Returns the N-Triples representation of a blank node.
241
240
  #
242
241
  # @param [RDF::Node] node
243
- # @param [Hash{Symbol => Object}] options = ({})
244
- # @option options [Boolean] :unique_bnodes (false)
242
+ # @param [Boolean] unique_bnodes (false)
245
243
  # Serialize node using unique identifier, rather than any used to create the node.
244
+ # @param [Hash{Symbol => Object}] options ({})
246
245
  # @return [String]
247
- def format_node(node, options = {})
248
- options[:unique_bnodes] ? node.to_unique_base : node.to_s
246
+ def format_node(node, unique_bnodes: false, **options)
247
+ unique_bnodes ? node.to_unique_base : node.to_s
249
248
  end
250
249
 
251
250
  ##
252
251
  # Returns the N-Triples representation of a URI reference using write encoding.
253
252
  #
254
253
  # @param [RDF::URI] uri
255
- # @param [Hash{Symbol => Object}] options = ({})
254
+ # @param [Hash{Symbol => Object}] options ({})
256
255
  # @return [String]
257
- def format_uri(uri, options = {})
256
+ def format_uri(uri, **options)
258
257
  string = uri.to_s
259
258
  iriref = case
260
259
  when string =~ ESCAPE_PLAIN_U # a shortcut for the simple case
@@ -296,9 +295,9 @@ module RDF::NTriples
296
295
  # Returns the N-Triples representation of a literal.
297
296
  #
298
297
  # @param [RDF::Literal, String, #to_s] literal
299
- # @param [Hash{Symbol => Object}] options = ({})
298
+ # @param [Hash{Symbol => Object}] options ({})
300
299
  # @return [String]
301
- def format_literal(literal, options = {})
300
+ def format_literal(literal, **options)
302
301
  case literal
303
302
  when RDF::Literal
304
303
  # Note, escaping here is more robust than in Term
data/lib/rdf/query.rb CHANGED
@@ -109,7 +109,7 @@ module RDF
109
109
  # @param [Array<Solution>] args
110
110
  # @return [Solutions] returns new solutions including the arguments, which must each be a {Solution}
111
111
  def self.Solutions(*args)
112
- if args.length == 1
112
+ if args.length == 1
113
113
  return args[0] if args[0].is_a?(Solutions)
114
114
  args = args[0] if args[0].is_a?(Array)
115
115
  end
@@ -143,7 +143,7 @@ module RDF
143
143
  ##
144
144
  # Initializes a new basic graph pattern query.
145
145
  #
146
- # @overload initialize(patterns = [], options = {})
146
+ # @overload initialize(patterns = [], **options)
147
147
  # @param [Array<RDF::Query::Pattern>] patterns
148
148
  # ...
149
149
  # @param [Hash{Symbol => Object}] options
@@ -162,32 +162,31 @@ module RDF
162
162
  # @yieldparam [RDF::Query] query
163
163
  # @yieldreturn [void] ignored
164
164
  #
165
- # @overload initialize(patterns, options = {})
165
+ # @overload initialize(patterns, **options)
166
166
  # @param [Hash{Object => Object}] patterns
167
167
  # ...
168
- # @param [Hash{Symbol => Object}] options
169
- # any additional keyword options
170
- # @option options [RDF::Query::Solutions] :solutions (Solutions.new)
171
- # @option options [RDF::Resource, RDF::Query::Variable, false] :graph_name (nil)
168
+ # @param [RDF::Query::Solutions] solutions (Solutions.new)
169
+ # @param [RDF::Resource, RDF::Query::Variable, false] graph_name (false)
172
170
  # Default graph name for matching against queryable.
173
171
  # Named queries either match against a specifically named
174
172
  # graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
175
173
  # Names that are against unbound variables match either default
176
174
  # or named graphs.
177
175
  # The name of `false` will only match against the default graph.
178
- # @option options [RDF::Resource, RDF::Query::Variable, false] :name (nil)
176
+ # @param [RDF::Resource, RDF::Query::Variable, false] name (false)
179
177
  # Alias for `:graph_name`.
178
+ # @param [Hash{Symbol => Object}] options
179
+ # any additional keyword options
180
180
  # @yield [query]
181
181
  # @yieldparam [RDF::Query] query
182
182
  # @yieldreturn [void] ignored
183
- def initialize(*patterns, &block)
184
- @options = patterns.last.is_a?(Hash) ? patterns.pop.dup : {}
185
- patterns << @options if patterns.empty?
183
+ def initialize(*patterns, solutions: nil, graph_name: nil, name: nil, **options, &block)
184
+ @options = options.dup
186
185
  @variables = {}
187
- @solutions = Query::Solutions(@options.delete(:solutions))
188
- graph_name = @options.fetch(:graph_name, @options.fetch(:name, nil))
189
- @options.delete(:graph_name)
190
- @options.delete(:name)
186
+ @solutions = Query::Solutions(solutions)
187
+ graph_name = name if graph_name.nil?
188
+
189
+ patterns << @options if patterns.empty?
191
190
 
192
191
  @patterns = case patterns.first
193
192
  when Hash then compile_hash_patterns(HashPatternNormalizer.normalize!(patterns.first.dup, @options))
@@ -226,7 +225,7 @@ module RDF
226
225
  # @option options [Boolean] :optional (false)
227
226
  # whether this is an optional pattern
228
227
  # @return [void] self
229
- def pattern(pattern, options = {})
228
+ def pattern(pattern, **options)
230
229
  @patterns << Pattern.from(pattern, options)
231
230
  self
232
231
  end
@@ -238,7 +237,7 @@ module RDF
238
237
  # any additional options for optimization
239
238
  # @return [RDF::Query] a copy of `self`
240
239
  # @since 0.3.0
241
- def optimize(options = {})
240
+ def optimize(**options)
242
241
  self.dup.optimize!(options)
243
242
  end
244
243
 
@@ -251,7 +250,7 @@ module RDF
251
250
  # @return [self]
252
251
  # @see RDF::Query::Pattern#cost
253
252
  # @since 0.3.0
254
- def optimize!(options = {})
253
+ def optimize!(**options)
255
254
  @patterns.sort! do |a, b|
256
255
  (a.cost || 0) <=> (b.cost || 0)
257
256
  end
@@ -274,15 +273,20 @@ module RDF
274
273
  #
275
274
  # @param [RDF::Queryable] queryable
276
275
  # the graph or repository to query
276
+ # @param [RDF::Query::Solutions] solutions (Solutions.new)
277
+ # @param [RDF::Resource, RDF::Query::Variable, false] graph_name (nil)
278
+ # Default graph name for matching against queryable.
279
+ # Named queries either match against a specifically named
280
+ # graphs if the name is an {RDF::Resource} or bound {RDF::Query::Variable}.
281
+ # Names that are against unbound variables match either default
282
+ # or named graphs.
283
+ # The name of `false` will only match against the default graph.
284
+ # @param [RDF::Resource, RDF::Query::Variable, false] name (nil)
285
+ # Alias for `:graph_name`.
277
286
  # @param [Hash{Symbol => Object}] options
278
287
  # any additional keyword options
279
288
  # @option options [Hash{Symbol => RDF::Term}] bindings
280
289
  # optional variable bindings to use
281
- # @option options [RDF::Resource, RDF::Query::Variable, false] graph_name (nil)
282
- # Specific graph name for matching against queryable;
283
- # overrides default graph defined on query.
284
- # @option options [RDF::Resource, RDF::Query::Variable, false] name (nil)
285
- # Alias for `:graph_name`.
286
290
  # @option options [RDF::Query::Solutions] solutions
287
291
  # optional initial solutions for chained queries
288
292
  # @yield [solution]
@@ -293,17 +297,14 @@ module RDF
293
297
  # the resulting solution sequence
294
298
  # @see http://www.holygoat.co.uk/blog/entry/2005-10-25-1
295
299
  # @see http://www.w3.org/TR/sparql11-query/#emptyGroupPattern
296
- def execute(queryable, options = {}, &block)
300
+ def execute(queryable, solutions: Solution.new, graph_name: nil, name: nil, **options, &block)
297
301
  validate!
298
- options = options.dup
299
-
300
- # just so we can call #keys below without worrying
301
- options[:bindings] ||= {}
302
+ options = {bindings: {}}.merge(options)
302
303
 
303
304
  # Use provided solutions to allow for query chaining
304
305
  # Otherwise, a quick empty solution simplifies the logic below; no special case for
305
306
  # the first pattern
306
- @solutions = Query::Solutions(options[:solutions] || Solution.new)
307
+ @solutions = Query::Solutions(solutions)
307
308
 
308
309
  # If there are no patterns, just return the empty solution
309
310
  if empty?
@@ -312,7 +313,9 @@ module RDF
312
313
  end
313
314
 
314
315
  patterns = @patterns
315
- graph_name = options.fetch(:graph_name, options.fetch(:name, self.graph_name))
316
+ graph_name = name if graph_name.nil?
317
+ graph_name = self.graph_name if graph_name.nil?
318
+ options[:graph_name] = graph_name unless graph_name.nil?
316
319
 
317
320
  # Add graph_name to pattern, if necessary
318
321
  unless graph_name.nil?
@@ -173,7 +173,7 @@ module RDF; class Query
173
173
  # any additional normalization options.
174
174
  # @option options [String] :anonymous_subject_format ("__%s__")
175
175
  # the string format for anonymous subjects.
176
- def initialize(options = {})
176
+ def initialize(**options)
177
177
  @options = options.dup
178
178
  end
179
179
 
@@ -184,7 +184,7 @@ module RDF; class Query
184
184
  # the query pattern as a hash.
185
185
  # @return [Hash{Symbol => Object}]
186
186
  # the resulting query pattern as a normalized hash.
187
- def normalize!(hash_pattern = {})
187
+ def normalize!(**hash_pattern)
188
188
  self.class.normalize!(hash_pattern, @options)
189
189
  end
190
190
  end # RDF::Query::HashPatternNormalizer
@@ -5,18 +5,19 @@ module RDF; class Query
5
5
  ##
6
6
  # @private
7
7
  # @since 0.2.2
8
- def self.from(pattern, options = {})
8
+ def self.from(pattern, graph_name: nil, **options)
9
9
  case pattern
10
10
  when Pattern then pattern
11
11
  when Array, Statement
12
- self.new(pattern[0], pattern[1], pattern[2], options.merge(graph_name: pattern[3]))
12
+ graph_name ||= pattern[3]
13
+ self.new(pattern[0], pattern[1], pattern[2], graph_name: graph_name, **options)
13
14
  when Hash then self.new(options.merge(pattern))
14
15
  else raise ArgumentError, "expected RDF::Query::Pattern, RDF::Statement, Hash, or Array, but got #{pattern.inspect}"
15
16
  end
16
17
  end
17
18
 
18
19
  ##
19
- # @overload initialize(options = {})
20
+ # @overload initialize(**options)
20
21
  # @param [Hash{Symbol => Object}] options
21
22
  # @option options [Variable, Resource, Symbol, nil] :subject (nil)
22
23
  # @option options [Variable, URI, Symbol, nil] :predicate (nil)
@@ -25,7 +26,7 @@ module RDF; class Query
25
26
  # A graph_name of nil matches any graph, a graph_name of false, matches only the default graph.
26
27
  # @option options [Boolean] :optional (false)
27
28
  #
28
- # @overload initialize(subject, predicate, object, options = {})
29
+ # @overload initialize(subject, predicate, object, **options)
29
30
  # @param [Variable, Resource, Symbol, nil] subject
30
31
  # @param [Variable, URI, Symbol, nil] predicate
31
32
  # @param [Variable, Termm, Symbol, nil] object
@@ -18,7 +18,7 @@ class RDF::Query
18
18
  # solution.mbox
19
19
  #
20
20
  # @example Retrieving all bindings in the solution as a `Hash`
21
- # solution.to_hash #=> {mbox: "jrhacker@example.org", ...}
21
+ # solution.to_h #=> {mbox: "jrhacker@example.org", ...}
22
22
  #
23
23
  class Solution
24
24
  # Undefine all superfluous instance methods:
@@ -152,12 +152,12 @@ class RDF::Query
152
152
  # Merges the bindings from the given `other` query solution into this
153
153
  # one, overwriting any existing ones having the same name.
154
154
  #
155
- # @param [RDF::Query::Solution, #to_hash] other
155
+ # @param [RDF::Query::Solution, #to_h] other
156
156
  # another query solution or hash bindings
157
157
  # @return [void] self
158
158
  # @since 0.3.0
159
159
  def merge!(other)
160
- @bindings.merge!(other.to_hash)
160
+ @bindings.merge!(other.to_h)
161
161
  self
162
162
  end
163
163
 
@@ -165,7 +165,7 @@ class RDF::Query
165
165
  # Merges the bindings from the given `other` query solution with a copy
166
166
  # of this one.
167
167
  #
168
- # @param [RDF::Query::Solution, #to_hash] other
168
+ # @param [RDF::Query::Solution, #to_h] other
169
169
  # another query solution or hash bindings
170
170
  # @return [RDF::Query::Solution]
171
171
  # @since 0.3.0
@@ -185,13 +185,13 @@ class RDF::Query
185
185
  #
186
186
  # Two solution mappings u1 and u2 are compatible if, for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
187
187
  #
188
- # @param [RDF::Query::Solution, #to_hash] other
188
+ # @param [RDF::Query::Solution, #to_h] other
189
189
  # another query solution or hash bindings
190
190
  # @return [Boolean]
191
191
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
192
192
  def compatible?(other)
193
193
  @bindings.all? do |k, v|
194
- !other.to_hash.has_key?(k) || other[k].eql?(v)
194
+ !other.to_h.has_key?(k) || other[k].eql?(v)
195
195
  end
196
196
  end
197
197
 
@@ -205,7 +205,7 @@ class RDF::Query
205
205
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
206
206
  def disjoint?(other)
207
207
  @bindings.none? do |k, v|
208
- v && other.to_hash.has_key?(k) && other[k].eql?(v)
208
+ v && other.to_h.has_key?(k) && other[k].eql?(v)
209
209
  end
210
210
  end
211
211
 
@@ -214,12 +214,12 @@ class RDF::Query
214
214
  # Two solution mappings u1 and u2 are isomorphic if,
215
215
  # for every variable v in dom(u1) and in dom(u2), u1(v) = u2(v).
216
216
  #
217
- # @param [RDF::Query::Solution, #to_hash] other
217
+ # @param [RDF::Query::Solution, #to_h] other
218
218
  # another query solution or hash bindings
219
219
  # @return [Boolean]
220
220
  def isomorphic_with?(other)
221
221
  @bindings.all? do |k, v|
222
- !other.to_hash.has_key?(k) || other[k].eql?(v)
222
+ !other.to_h.has_key?(k) || other[k].eql?(v)
223
223
  end
224
224
  end
225
225
 
@@ -231,7 +231,7 @@ class RDF::Query
231
231
 
232
232
  ##
233
233
  # @return [Hash{Symbol => RDF::Term}}
234
- def to_hash
234
+ def to_h
235
235
  @bindings.dup
236
236
  end
237
237
 
@@ -264,10 +264,22 @@ class RDF::Query
264
264
  protected
265
265
 
266
266
  ##
267
- # @param [Symbol] name
268
- # @return [RDF::Term]
267
+ # @overload #to_hash
268
+ # Returns object representation of this URI, broken into components
269
+ #
270
+ # @return (see #to_h)
271
+ # @deprecated Use {#to_h} instead.
272
+ #
273
+ # @overload binding(name)
274
+ # Return the binding for this name
275
+ #
276
+ # @param [Symbol] name
277
+ # @return [RDF::Term]
269
278
  def method_missing(name, *args, &block)
270
- if args.empty? && @bindings.has_key?(name.to_sym)
279
+ if name == :to_hash
280
+ warn "[DEPRECATION] Solution#to_hash is deprecated, use Solution#to_h instead. Called from #{Gem.location_of_caller.join(':')}"
281
+ self.to_h
282
+ elsif args.empty? && @bindings.has_key?(name.to_sym)
271
283
  @bindings[name.to_sym]
272
284
  else
273
285
  super # raises NoMethodError
@@ -151,7 +151,7 @@ class RDF::Query
151
151
  def variables
152
152
  {name => self}
153
153
  end
154
- alias_method :to_hash, :variables
154
+ alias_method :to_h, :variables
155
155
 
156
156
  ##
157
157
  # Returns this variable's bindings (if any) as a `Hash`.
@@ -164,7 +164,7 @@ class RDF::Query
164
164
  ##
165
165
  # Returns a hash code for this variable.
166
166
  #
167
- # @return [Fixnum]
167
+ # @return [Integer]
168
168
  # @since 0.3.0
169
169
  def hash
170
170
  @name.hash
@@ -219,5 +219,23 @@ class RDF::Query
219
219
  prefix = distinguished? ? '?' : "??"
220
220
  unbound? ? "#{prefix}#{name}" : "#{prefix}#{name}=#{value}"
221
221
  end
222
+
223
+ protected
224
+ ##
225
+ # @overload #to_hash
226
+ # Returns object representation of this URI, broken into components
227
+ #
228
+ # @return (see #object)
229
+ # @deprecated Use {#to_h} instead.
230
+ def method_missing(name, *args, &block)
231
+ if name == :to_hash
232
+ warn "[DEPRECATION] Variable#to_hash is deprecated, use Variable#to_h instead. Called from #{Gem.location_of_caller.join(':')}"
233
+ self.to_h
234
+ elsif args.empty? && @bindings.has_key?(name.to_sym)
235
+ @bindings[name.to_sym]
236
+ else
237
+ super # raises NoMethodError
238
+ end
239
+ end
222
240
  end # Variable
223
241
  end # RDF::Query
data/lib/rdf/reader.rb CHANGED
@@ -219,31 +219,43 @@ module RDF
219
219
  #
220
220
  # @param [IO, File, String] input
221
221
  # the input stream to read
222
- # @param [Hash{Symbol => Object}] options
223
- # any additional options
224
- # @option options [Encoding] :encoding (Encoding::UTF_8)
222
+ # @param [Encoding] encoding (Encoding::UTF_8)
225
223
  # the encoding of the input stream
226
- # @option options [Boolean] :validate (false)
224
+ # @param [Boolean] validate (false)
227
225
  # whether to validate the parsed statements and values
228
- # @option options [Boolean] :canonicalize (false)
226
+ # @param [Boolean] canonicalize (false)
229
227
  # whether to canonicalize parsed literals
230
- # @option options [Boolean] :intern (true)
228
+ # @param [Boolean] intern (true)
231
229
  # whether to intern all parsed URIs
232
- # @option options [Hash] :prefixes (Hash.new)
230
+ # @param [Hash] prefixes (Hash.new)
233
231
  # the prefix mappings to use (not supported by all readers)
234
- # @option options [#to_s] :base_uri (nil)
232
+ # @param [#to_s] base_uri (nil)
235
233
  # the base URI to use when resolving relative URIs (not supported by
236
234
  # all readers)
235
+ # @param [Hash{Symbol => Object}] options
236
+ # any additional options
237
237
  # @yield [reader] `self`
238
238
  # @yieldparam [RDF::Reader] reader
239
239
  # @yieldreturn [void] ignored
240
- def initialize(input = $stdin, options = {}, &block)
241
- @options = options.dup
242
- @options[:validate] ||= false
243
- @options[:canonicalize] ||= false
244
- @options[:intern] ||= true
245
- @options[:prefixes] ||= Hash.new
246
- @options[:base_uri] ||= input.base_uri if input.respond_to?(:base_uri)
240
+ def initialize(input = $stdin,
241
+ encoding: Encoding::UTF_8,
242
+ validate: false,
243
+ canonicalize: false,
244
+ intern: true,
245
+ prefixes: Hash.new,
246
+ base_uri: nil,
247
+ **options,
248
+ &block)
249
+
250
+ base_uri ||= input.base_uri if input.respond_to?(:base_uri)
251
+ @options = options.merge({
252
+ encoding: encoding,
253
+ validate: validate,
254
+ canonicalize: canonicalize,
255
+ intern: intern,
256
+ prefixes: prefixes,
257
+ base_uri: base_uri
258
+ })
247
259
 
248
260
  @input = case input
249
261
  when String then StringIO.new(input)
@@ -619,13 +631,12 @@ module RDF
619
631
  ##
620
632
  # Initializes a new lexer error instance.
621
633
  #
622
- # @param [String, #to_s] message
623
- # @param [Hash{Symbol => Object}] options
624
- # @option options [String] :token (nil)
625
- # @option options [Integer] :lineno (nil)
626
- def initialize(message, options = {})
627
- @token = options[:token]
628
- @lineno = options[:lineno] || (@token.lineno if @token.respond_to?(:lineno))
634
+ # @param [String, #to_s] message
635
+ # @param [String] token (nil)
636
+ # @param [Integer] lineno (nil)
637
+ def initialize(message, token: nil, lineno: nil)
638
+ @token = token
639
+ @lineno = lineno || (token.lineno if token.respond_to?(:lineno))
629
640
  super(message.to_s)
630
641
  end
631
642
  end # ReaderError