rdf 3.1.8 → 3.1.13

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.
@@ -57,11 +57,21 @@ module RDF
57
57
  end
58
58
 
59
59
  ##
60
- # Returns `true` if `self` is a {RDF::Term}.
60
+ # @overload term?
61
+ # Returns `true` if `self` is a {RDF::Term}.
61
62
  #
62
- # @return [Boolean]
63
- def term?
64
- true
63
+ # @return [Boolean]
64
+ # @overload term?(name)
65
+ # Returns `true` if `self` contains the given RDF subject term.
66
+ #
67
+ # @param [RDF::Resource] value
68
+ # @return [Boolean]
69
+ def term?(*args)
70
+ case args.length
71
+ when 0 then true
72
+ when 1 then false
73
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
74
+ end
65
75
  end
66
76
 
67
77
  ##
@@ -72,6 +82,14 @@ module RDF
72
82
  self
73
83
  end
74
84
 
85
+ ##
86
+ # Returns an array including just itself.
87
+ #
88
+ # @return [Array<RDF::Value>]
89
+ def terms
90
+ [self]
91
+ end
92
+
75
93
  ##
76
94
  # Returns the base representation of this term.
77
95
  #
data/lib/rdf/model/uri.rb CHANGED
@@ -226,13 +226,13 @@ module RDF
226
226
  @value.dup.force_encoding(Encoding::UTF_8) if @value.encoding != Encoding::UTF_8
227
227
  @value.freeze
228
228
  else
229
- %w(
229
+ %i(
230
230
  scheme
231
231
  user password userinfo
232
232
  host port authority
233
233
  path query fragment
234
- ).map(&:to_sym).each do |meth|
235
- if options.has_key?(meth)
234
+ ).each do |meth|
235
+ if options.key?(meth)
236
236
  self.send("#{meth}=".to_sym, options[meth])
237
237
  else
238
238
  self.send(meth)
@@ -416,7 +416,7 @@ module RDF
416
416
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.2
417
417
  # @see http://tools.ietf.org/html/rfc3986#section-5.2.3
418
418
  def join(*uris)
419
- joined_parts = object.dup.delete_if {|k, v| [:user, :password, :host, :port].include?(k)}
419
+ joined_parts = object.dup.delete_if {|k, v| %i(user password host port).include?(k)}
420
420
 
421
421
  uris.each do |uri|
422
422
  uri = RDF::URI.new(uri) unless uri.is_a?(RDF::URI)
@@ -580,7 +580,7 @@ module RDF
580
580
  else
581
581
  RDF::URI.new(
582
582
  **object.merge(path: '/').
583
- keep_if {|k, v| [:scheme, :authority, :path].include?(k)})
583
+ keep_if {|k, v| %i(scheme authority path).include?(k)})
584
584
  end
585
585
  end
586
586
 
@@ -588,13 +588,14 @@ module RDF
588
588
  # Returns `true` if this URI is hierarchical and it's path component isn't equal to `/`.
589
589
  #
590
590
  # @example
591
- # RDF::URI('http://example.org/').has_parent? #=> false
592
- # RDF::URI('http://example.org/path/').has_parent? #=> true
591
+ # RDF::URI('http://example.org/').parent? #=> false
592
+ # RDF::URI('http://example.org/path/').parent? #=> true
593
593
  #
594
594
  # @return [Boolean] `true` or `false`
595
- def has_parent?
595
+ def parent?
596
596
  !root?
597
597
  end
598
+ alias_method :has_parent?, :parent?
598
599
 
599
600
  ##
600
601
  # Returns a copy of this URI with the path component ascended to the
@@ -1122,7 +1123,7 @@ module RDF
1122
1123
  # @param [String, #to_s] value
1123
1124
  # @return [RDF::URI] self
1124
1125
  def authority=(value)
1125
- object.delete_if {|k, v| [:user, :password, :host, :port, :userinfo].include?(k)}
1126
+ object.delete_if {|k, v| %i(user password host port userinfo).include?(k)}
1126
1127
  object[:authority] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1127
1128
  user; password; userinfo; host; port
1128
1129
  @value = nil
@@ -1152,7 +1153,7 @@ module RDF
1152
1153
  # @param [String, #to_s] value
1153
1154
  # @return [RDF::URI] self
1154
1155
  def userinfo=(value)
1155
- object.delete_if {|k, v| [:user, :password, :authority].include?(k)}
1156
+ object.delete_if {|k, v| %i(user password authority).include?(k)}
1156
1157
  object[:userinfo] = (value.to_s.dup.force_encoding(Encoding::UTF_8) if value)
1157
1158
  user; password; authority
1158
1159
  @value = nil
@@ -29,19 +29,38 @@ module RDF
29
29
  # @see RDF::Statement
30
30
  module Value
31
31
  ##
32
- # Returns `true` if `self` is a {RDF::Graph}.
32
+ # @overload graph?
33
+ # Returns `true` if `self` is a {RDF::Graph}.
33
34
  #
34
- # @return [Boolean]
35
- def graph?
36
- false
35
+ # @return [Boolean]
36
+ # @overload graph?(name)
37
+ # Returns `true` if `self` contains the given RDF graph_name.
38
+ #
39
+ # @param [RDF::Resource, false] graph_name
40
+ # Use value `false` to query for the default graph_name
41
+ # @return [Boolean]
42
+ def graph?(*args)
43
+ case args.length
44
+ when 0, 1 then false
45
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
46
+ end
37
47
  end
38
48
 
39
49
  ##
40
- # Is this a {RDF::Statement}?
50
+ # @overload statement?
51
+ # Returns `true` if `self` is a {RDF::Statement}.
41
52
  #
42
- # @return [Boolean]
43
- def statement?
44
- false
53
+ # @return [Boolean]
54
+ # @overload statement?(statement)
55
+ # Returns `true` if `self` contains the given {RDF::Statement}.
56
+ #
57
+ # @param [RDF::Statement] statement
58
+ # @return [Boolean]
59
+ def statement?(*args)
60
+ case args.length
61
+ when 0, 1 then false
62
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
63
+ end
45
64
  end
46
65
 
47
66
  ##
@@ -53,11 +72,20 @@ module RDF
53
72
  end
54
73
 
55
74
  ##
56
- # Is this a {RDF::Term}?
75
+ # @overload term?
76
+ # Returns `true` if `self` is a {RDF::Term}.
57
77
  #
58
- # @return [Boolean]
59
- def term?
60
- false
78
+ # @return [Boolean]
79
+ # @overload term?(name)
80
+ # Returns `true` if `self` contains the given RDF subject term.
81
+ #
82
+ # @param [RDF::Resource] value
83
+ # @return [Boolean]
84
+ def term?(*args)
85
+ case args.length
86
+ when 0, 1 then false
87
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
88
+ end
61
89
  end
62
90
 
63
91
  ##
@@ -103,12 +131,21 @@ module RDF
103
131
  end
104
132
 
105
133
  ##
106
- # Is this a {RDF::Query::Variable}, or does it contain a variable?
134
+ # @overload variable?
135
+ # Returns `true` if `self` is a {RDF::Query::Variable}, or does it contain a variable?
107
136
  #
108
- # @return [Boolean]
137
+ # @return [Boolean]
138
+ # @overload variable?(variable)
139
+ # Returns `true` if `self` contains the given variable.
140
+ #
141
+ # @param [RDF::Resource] value
142
+ # @return [Boolean]
109
143
  # @since 0.1.7
110
- def variable?
111
- false
144
+ def variable?(*args)
145
+ case args.length
146
+ when 0, 1 then false
147
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
148
+ end
112
149
  end
113
150
 
114
151
  ##
@@ -309,8 +309,8 @@ module RDF::NTriples
309
309
  when RDF::Literal
310
310
  # Note, escaping here is more robust than in Term
311
311
  text = quoted(escaped(literal.value))
312
- text << "@#{literal.language}" if literal.has_language?
313
- text << "^^<#{uri_for(literal.datatype)}>" if literal.has_datatype?
312
+ text << "@#{literal.language}" if literal.language?
313
+ text << "^^<#{uri_for(literal.datatype)}>" if literal.datatype?
314
314
  text
315
315
  else
316
316
  quoted(escaped(literal.to_s))
data/lib/rdf/query.rb CHANGED
@@ -443,11 +443,22 @@ module RDF
443
443
  end
444
444
 
445
445
  ##
446
- # Returns `true` if any pattern contains a variable.
446
+ # @overload variable?
447
+ # Returns `true` if any pattern contains a variable.
447
448
  #
448
- # @return [Boolean]
449
- def variable?
450
- !variables.empty?
449
+ # @return [Boolean]
450
+ # @overload variable?(variables)
451
+ # Returns `true` if any pattern contains any of the variables.
452
+ #
453
+ # @param [Array<Symbol, #to_sym>] variables
454
+ # @return [Boolean]
455
+ def variable?(*args)
456
+ case args.length
457
+ when 0 then !variables.empty?
458
+ when 1
459
+ patterns.any? {|p| p.variable?(*args)}
460
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
461
+ end
451
462
  end
452
463
  alias_method :variables?, :variable?
453
464
  alias_method :has_variables?, :variable?
@@ -85,13 +85,13 @@ module RDF; class Query
85
85
  #
86
86
  # @return [Boolean] `true` or `false`
87
87
  # @since 0.3.0
88
- def has_variables?
88
+ def variables?
89
89
  subject && subject.variable? ||
90
90
  predicate && predicate.variable? ||
91
91
  object && object.variable? ||
92
92
  graph_name && graph_name.variable?
93
93
  end
94
- alias_method :variables?, :has_variables?
94
+ alias_method :has_variables?, :variables?
95
95
 
96
96
  ##
97
97
  # Returns `true` if this is an optional pattern.
@@ -111,10 +111,10 @@ module RDF; class Query
111
111
  #
112
112
  # @return [Boolean] `true` or `false`
113
113
  def valid?
114
- (has_subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
115
- (has_predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
116
- (has_object? ? (object.term? || object.variable?) && object.valid? : true) &&
117
- (has_graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true)
114
+ (subject? ? (subject.resource? || subject.variable?) && subject.valid? : true) &&
115
+ (predicate? ? (predicate.uri? || predicate.variable?) && predicate.valid? : true) &&
116
+ (object? ? (object.term? || object.variable?) && object.valid? : true) &&
117
+ (graph? ? (graph_name.resource? || graph_name.variable?) && graph_name.valid? : true)
118
118
  rescue NoMethodError
119
119
  false
120
120
  end
@@ -248,7 +248,7 @@ module RDF; class Query
248
248
  # @param [RDF::Statement] statement
249
249
  # @return [Array<RDF::Term>]
250
250
  def var_values(var, statement)
251
- [:subject, :predicate, :object, :graph_name].map do |position|
251
+ %i(subject predicate object graph_name).map do |position|
252
252
  po = self.send(position)
253
253
  so = statement.send(position)
254
254
  po.var_values(var, so) if po.respond_to?(:var_values)
@@ -23,10 +23,13 @@ class RDF::Query
23
23
  class Solution
24
24
  # Undefine all superfluous instance methods:
25
25
  alias_method :__send, :send
26
+
27
+ # Temporarily remember instance method for deprecation message in `method_missing`.
28
+ INSTANCE_METHODS = instance_methods
26
29
  undef_method(*instance_methods.
27
30
  map(&:to_s).
28
31
  select {|m| m.match?(/^\w+$/)}.
29
- reject {|m| %w(object_id dup instance_eval inspect to_s private_methods class should should_not pretty_print).include?(m) || m[0,2] == '__'}.
32
+ reject {|m| %w(object_id dup instance_eval inspect to_s private_methods public_methods class method pretty_print).include?(m) || m[0,2] == '__'}.
30
33
  map(&:to_sym))
31
34
 
32
35
  include Enumerable
@@ -114,16 +117,27 @@ class RDF::Query
114
117
  end
115
118
 
116
119
  ##
117
- # Returns `true` if this solution contains bindings for any of the given
120
+ # @overload variable?
121
+ # Returns `false`.
122
+ #
123
+ # @return [Boolean]
124
+ # @overload variable?(variables)
125
+ # Returns `true` if this solution contains bindings for any of the given
118
126
  # `variables`.
119
127
  #
120
- # @param [Array<Symbol, #to_sym>] variables
121
- # an array of variables to check
122
- # @return [Boolean] `true` or `false`
128
+ # @param [Array<Symbol, #to_sym>] variables
129
+ # @return [Boolean]
123
130
  # @since 0.3.0
124
- def has_variables?(variables)
125
- variables.any? { |variable| bound?(variable) }
131
+ def variable?(*args)
132
+ case args.length
133
+ when 0 then false
134
+ when 1
135
+ args.first.any? { |variable| bound?(variable) }
136
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
137
+ end
126
138
  end
139
+ alias_method :variables?, :variable?
140
+ alias_method :has_variables?, :variable?
127
141
 
128
142
  ##
129
143
  # Enumerates over every variable in this solution.
@@ -253,7 +267,7 @@ class RDF::Query
253
267
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algCompatibleMapping
254
268
  def compatible?(other)
255
269
  @bindings.all? do |k, v|
256
- !other.to_h.has_key?(k) || other[k].eql?(v)
270
+ !other.to_h.key?(k) || other[k].eql?(v)
257
271
  end
258
272
  end
259
273
 
@@ -267,7 +281,7 @@ class RDF::Query
267
281
  # @see http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#defn_algMinus
268
282
  def disjoint?(other)
269
283
  @bindings.none? do |k, v|
270
- v && other.to_h.has_key?(k) && other[k].eql?(v)
284
+ v && other.to_h.key?(k) && other[k].eql?(v)
271
285
  end
272
286
  end
273
287
 
@@ -281,7 +295,7 @@ class RDF::Query
281
295
  # @return [Boolean]
282
296
  def isomorphic_with?(other)
283
297
  @bindings.all? do |k, v|
284
- !other.to_h.has_key?(k) || other[k].eql?(v)
298
+ !other.to_h.key?(k) || other[k].eql?(v)
285
299
  end
286
300
  end
287
301
 
@@ -332,7 +346,13 @@ class RDF::Query
332
346
  # @param [Symbol] name
333
347
  # @return [RDF::Term]
334
348
  def method_missing(name, *args, &block)
335
- if args.empty? && @bindings.has_key?(name.to_sym)
349
+ if args.empty? && @bindings.key?(name.to_sym)
350
+ if INSTANCE_METHODS.include?(name)
351
+ warn "[DEPRECATION] RDF::Query::Solution##{name} is an overridden instance method.\n" +
352
+ "Its use as a solution accessor is deprecated and will be removed in a future version.\n" +
353
+ "Use #[] for safe access.\n" +
354
+ "Called from #{Gem.location_of_caller.join(':')}"
355
+ end
336
356
  @bindings[name.to_sym]
337
357
  else
338
358
  super # raises NoMethodError
@@ -342,7 +362,7 @@ class RDF::Query
342
362
  ##
343
363
  # @return [Boolean]
344
364
  def respond_to_missing?(name, include_private = false)
345
- @bindings.has_key?(name.to_sym) || super
365
+ @bindings.key?(name.to_sym) || super
346
366
  end
347
367
 
348
368
  ##
@@ -78,17 +78,28 @@ module RDF; class Query
78
78
  end
79
79
 
80
80
  ##
81
- # Returns `true` if this solution sequence contains bindings for any of
81
+ # @overload variable?
82
+ # Returns `false`.
83
+ #
84
+ # @return [Boolean]
85
+ # @overload variable?(variables)
86
+ # Returns `true` if this solution sequence contains bindings for any of
82
87
  # the given `variables`.
83
88
  #
84
- # @param [Array<Symbol, #to_sym>] variables
85
- # an array of variables to check
86
- # @return [Boolean] `true` or `false`
87
- # @see RDF::Query::Solution#has_variables?
89
+ # @param [Array<Symbol, #to_sym>] variables
90
+ # @return [Boolean]
91
+ # @see RDF::Query::Solution#variable?
88
92
  # @see RDF::Query#execute
89
- def have_variables?(variables)
90
- self.any? { |solution| solution.has_variables?(variables) }
93
+ def variable?(*args)
94
+ case args.length
95
+ when 0 then false
96
+ when 1
97
+ self.any? { |solution| solution.variables?(args.first) }
98
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
99
+ end
91
100
  end
101
+ alias_method :variables?, :variable?
102
+ alias_method :have_variables?, :variable?
92
103
  alias_method :has_variables?, :have_variables?
93
104
 
94
105
  ##
@@ -87,13 +87,27 @@ class RDF::Query
87
87
  end
88
88
 
89
89
  ##
90
- # Returns `true`.
90
+ # @overload variable?
91
+ # Returns `true` if `self` is a {RDF::Query::Variable}, or does it contain a variable?
91
92
  #
92
- # @return [Boolean]
93
- # @see RDF::Term#variable?
93
+ # @return [Boolean]
94
+ # @overload variable?(variable)
95
+ # Returns `true` if `self` contains the given variable.
96
+ #
97
+ # @param [RDF::Resource] value
98
+ # @return [Boolean]
94
99
  # @since 0.1.7
95
- def variable?
96
- true
100
+ def variable?(*args)
101
+ case args.length
102
+ when 0 then true
103
+ when 1
104
+ case variable = args.first
105
+ when RDF::Query::Variable then self == variable
106
+ when Symbol then to_sym == variable
107
+ else false
108
+ end
109
+ else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)")
110
+ end
97
111
  end
98
112
 
99
113
  ##