openlogic-rdf 0.3.6

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.
Files changed (80) hide show
  1. data/AUTHORS +3 -0
  2. data/CREDITS +9 -0
  3. data/README +361 -0
  4. data/UNLICENSE +24 -0
  5. data/VERSION +1 -0
  6. data/bin/rdf +18 -0
  7. data/etc/doap.nt +62 -0
  8. data/lib/df.rb +1 -0
  9. data/lib/rdf/cli.rb +200 -0
  10. data/lib/rdf/format.rb +383 -0
  11. data/lib/rdf/mixin/countable.rb +39 -0
  12. data/lib/rdf/mixin/durable.rb +31 -0
  13. data/lib/rdf/mixin/enumerable.rb +637 -0
  14. data/lib/rdf/mixin/indexable.rb +26 -0
  15. data/lib/rdf/mixin/inferable.rb +5 -0
  16. data/lib/rdf/mixin/mutable.rb +191 -0
  17. data/lib/rdf/mixin/queryable.rb +265 -0
  18. data/lib/rdf/mixin/readable.rb +15 -0
  19. data/lib/rdf/mixin/type_check.rb +21 -0
  20. data/lib/rdf/mixin/writable.rb +152 -0
  21. data/lib/rdf/model/graph.rb +263 -0
  22. data/lib/rdf/model/list.rb +731 -0
  23. data/lib/rdf/model/literal/boolean.rb +121 -0
  24. data/lib/rdf/model/literal/date.rb +73 -0
  25. data/lib/rdf/model/literal/datetime.rb +72 -0
  26. data/lib/rdf/model/literal/decimal.rb +86 -0
  27. data/lib/rdf/model/literal/double.rb +189 -0
  28. data/lib/rdf/model/literal/integer.rb +126 -0
  29. data/lib/rdf/model/literal/numeric.rb +184 -0
  30. data/lib/rdf/model/literal/time.rb +87 -0
  31. data/lib/rdf/model/literal/token.rb +47 -0
  32. data/lib/rdf/model/literal/xml.rb +39 -0
  33. data/lib/rdf/model/literal.rb +373 -0
  34. data/lib/rdf/model/node.rb +156 -0
  35. data/lib/rdf/model/resource.rb +28 -0
  36. data/lib/rdf/model/statement.rb +296 -0
  37. data/lib/rdf/model/term.rb +77 -0
  38. data/lib/rdf/model/uri.rb +570 -0
  39. data/lib/rdf/model/value.rb +133 -0
  40. data/lib/rdf/nquads.rb +152 -0
  41. data/lib/rdf/ntriples/format.rb +48 -0
  42. data/lib/rdf/ntriples/reader.rb +239 -0
  43. data/lib/rdf/ntriples/writer.rb +219 -0
  44. data/lib/rdf/ntriples.rb +104 -0
  45. data/lib/rdf/query/pattern.rb +329 -0
  46. data/lib/rdf/query/solution.rb +252 -0
  47. data/lib/rdf/query/solutions.rb +237 -0
  48. data/lib/rdf/query/variable.rb +221 -0
  49. data/lib/rdf/query.rb +404 -0
  50. data/lib/rdf/reader.rb +511 -0
  51. data/lib/rdf/repository.rb +389 -0
  52. data/lib/rdf/transaction.rb +161 -0
  53. data/lib/rdf/util/aliasing.rb +63 -0
  54. data/lib/rdf/util/cache.rb +139 -0
  55. data/lib/rdf/util/file.rb +38 -0
  56. data/lib/rdf/util/uuid.rb +36 -0
  57. data/lib/rdf/util.rb +6 -0
  58. data/lib/rdf/version.rb +19 -0
  59. data/lib/rdf/vocab/cc.rb +18 -0
  60. data/lib/rdf/vocab/cert.rb +13 -0
  61. data/lib/rdf/vocab/dc.rb +63 -0
  62. data/lib/rdf/vocab/dc11.rb +23 -0
  63. data/lib/rdf/vocab/doap.rb +45 -0
  64. data/lib/rdf/vocab/exif.rb +168 -0
  65. data/lib/rdf/vocab/foaf.rb +69 -0
  66. data/lib/rdf/vocab/geo.rb +13 -0
  67. data/lib/rdf/vocab/http.rb +26 -0
  68. data/lib/rdf/vocab/owl.rb +59 -0
  69. data/lib/rdf/vocab/rdfs.rb +17 -0
  70. data/lib/rdf/vocab/rsa.rb +12 -0
  71. data/lib/rdf/vocab/rss.rb +14 -0
  72. data/lib/rdf/vocab/sioc.rb +93 -0
  73. data/lib/rdf/vocab/skos.rb +36 -0
  74. data/lib/rdf/vocab/wot.rb +21 -0
  75. data/lib/rdf/vocab/xhtml.rb +9 -0
  76. data/lib/rdf/vocab/xsd.rb +58 -0
  77. data/lib/rdf/vocab.rb +215 -0
  78. data/lib/rdf/writer.rb +475 -0
  79. data/lib/rdf.rb +192 -0
  80. metadata +173 -0
@@ -0,0 +1,570 @@
1
+ require 'addressable/uri'
2
+
3
+ module RDF
4
+ ##
5
+ # A Uniform Resource Identifier (URI).
6
+ #
7
+ # `RDF::URI` supports all the instance methods of `Addressable::URI`.
8
+ #
9
+ # @example Creating a URI reference (1)
10
+ # uri = RDF::URI.new("http://rdf.rubyforge.org/")
11
+ #
12
+ # @example Creating a URI reference (2)
13
+ # uri = RDF::URI.new(:scheme => 'http', :host => 'rdf.rubyforge.org', :path => '/')
14
+ #
15
+ # @example Creating an interned URI reference
16
+ # uri = RDF::URI.intern("http://rdf.rubyforge.org/")
17
+ #
18
+ # @example Getting the string representation of a URI
19
+ # uri.to_s #=> "http://rdf.rubyforge.org/"
20
+ #
21
+ # @see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
22
+ # @see http://addressable.rubyforge.org/
23
+ class URI
24
+ include RDF::Resource
25
+
26
+ ##
27
+ # Defines the maximum number of interned URI references that can be held
28
+ # cached in memory at any one time.
29
+ CACHE_SIZE = -1 # unlimited by default
30
+
31
+ ##
32
+ # @return [RDF::Util::Cache]
33
+ # @private
34
+ def self.cache
35
+ require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
36
+ @cache ||= RDF::Util::Cache.new(CACHE_SIZE)
37
+ end
38
+
39
+ ##
40
+ # Returns an interned `RDF::URI` instance based on the given `uri`
41
+ # string.
42
+ #
43
+ # The maximum number of cached interned URI references is given by the
44
+ # `CACHE_SIZE` constant. This value is unlimited by default, in which
45
+ # case an interned URI object will be purged only when the last strong
46
+ # reference to it is garbage collected (i.e., when its finalizer runs).
47
+ #
48
+ # Excepting special memory-limited circumstances, it should always be
49
+ # safe and preferred to construct new URI references using
50
+ # `RDF::URI.intern` instead of `RDF::URI.new`, since if an interned
51
+ # object can't be returned for some reason, this method will fall back
52
+ # to returning a freshly-allocated one.
53
+ #
54
+ # @param [String, #to_s] str
55
+ # @return [RDF::URI] an immutable, frozen URI object
56
+ def self.intern(str)
57
+ (cache[str = str.to_s] ||= self.new(str)).freeze
58
+ end
59
+
60
+ ##
61
+ # Creates a new `RDF::URI` instance based on the given `uri` string.
62
+ #
63
+ # This is just an alias for {#initialize RDF::URI.new} for compatibity
64
+ # with `Addressable::URI.parse`.
65
+ #
66
+ # @param [String, #to_s] str
67
+ # @return [RDF::URI]
68
+ def self.parse(str)
69
+ self.new(str)
70
+ end
71
+
72
+ ##
73
+ # @overload URI.new(uri)
74
+ # @param [RDF::URI, String, #to_s] uri
75
+ #
76
+ # @overload URI.new(options = {})
77
+ # @param [Hash{Symbol => Object} options
78
+ def initialize(uri_or_options)
79
+ case uri_or_options
80
+ when Hash
81
+ @uri = Addressable::URI.new(uri_or_options)
82
+ when Addressable::URI
83
+ @uri = uri_or_options
84
+ else
85
+ @uri = Addressable::URI.parse(uri_or_options.to_s)
86
+ end
87
+ end
88
+
89
+ ##
90
+ # Returns `false`.
91
+ #
92
+ # @return [Boolean] `true` or `false`
93
+ def anonymous?
94
+ false
95
+ end
96
+
97
+ ##
98
+ # Returns `true`.
99
+ #
100
+ # @return [Boolean] `true` or `false`
101
+ # @see http://en.wikipedia.org/wiki/Uniform_Resource_Identifier
102
+ def uri?
103
+ true
104
+ end
105
+
106
+ ##
107
+ # Returns `true` if this URI is a URN.
108
+ #
109
+ # @example
110
+ # RDF::URI('http://example.org/').urn? #=> false
111
+ #
112
+ # @return [Boolean] `true` or `false`
113
+ # @see http://en.wikipedia.org/wiki/Uniform_Resource_Name
114
+ # @since 0.2.0
115
+ def urn?
116
+ self.start_with?('urn:')
117
+ end
118
+
119
+ ##
120
+ # Returns `true` if this URI is a URL.
121
+ #
122
+ # @example
123
+ # RDF::URI('http://example.org/').url? #=> true
124
+ #
125
+ # @return [Boolean] `true` or `false`
126
+ # @see http://en.wikipedia.org/wiki/Uniform_Resource_Locator
127
+ # @since 0.2.0
128
+ def url?
129
+ !urn?
130
+ end
131
+
132
+ ##
133
+ # Returns the string length of this URI.
134
+ #
135
+ # @example
136
+ # RDF::URI('http://example.org/').length #=> 19
137
+ #
138
+ # @return [Integer]
139
+ # @since 0.3.0
140
+ def length
141
+ to_s.length
142
+ end
143
+ alias_method :size, :length
144
+
145
+ ##
146
+ # Validates this URI, raising an error if it is invalid.
147
+ #
148
+ # @return [RDF::URI] `self`
149
+ # @raise [ArgumentError] if the URI is invalid
150
+ # @since 0.3.0
151
+ def validate!
152
+ # TODO: raise error if the URI fails validation
153
+ self
154
+ end
155
+ alias_method :validate, :validate!
156
+
157
+ ##
158
+ # Returns a copy of this URI converted into its canonical lexical
159
+ # representation.
160
+ #
161
+ # @return [RDF::URI]
162
+ # @since 0.3.0
163
+ def canonicalize
164
+ self.dup.canonicalize!
165
+ end
166
+
167
+ ##
168
+ # Converts this URI into its canonical lexical representation.
169
+ #
170
+ # @return [RDF::URI] `self`
171
+ # @since 0.3.0
172
+ def canonicalize!
173
+ # TODO: canonicalize this URI
174
+ self
175
+ end
176
+
177
+ ##
178
+ # Joins several URIs together.
179
+ #
180
+ # This method conforms to join normalization semantics as per RFC3986,
181
+ # section 5.2. This method normalizes URIs, removes some duplicate path
182
+ # information, such as double slashes, and other behavior specified in the
183
+ # RFC.
184
+ #
185
+ # Other URI building methods are `#/` and `#+`.
186
+ #
187
+ # For an up-to-date list of edge case behavior, see the shared examples for
188
+ # RDF::URI in the rdf-spec project.
189
+ #
190
+ # @example Joining two URIs
191
+ # RDF::URI.new('http://example.org/foo/bar').join('/foo')
192
+ # #=> RDF::URI('http://example.org/foo')
193
+ # @see <http://github.com/bendiken/rdf-spec/blob/master/lib/rdf/spec/uri.rb>
194
+ # @see <http://tools.ietf.org/html/rfc3986#section-5.2>
195
+ # @see RDF::URI#/
196
+ # @see RDF::URI#+
197
+ # @param [Array<String, RDF::URI, #to_s>] uris
198
+ # @return [RDF::URI]
199
+ def join(*uris)
200
+ result = @uri.dup
201
+ uris.each do |uri|
202
+ result = result.join(uri)
203
+ end
204
+ self.class.new(result)
205
+ end
206
+
207
+ ##
208
+ # 'Smart separator' URI builder
209
+ #
210
+ # This method attempts to use some understanding of the most common use
211
+ # cases for URLs and URNs to create a simple method for building new URIs
212
+ # from fragments. This means that it will always insert a separator of
213
+ # some sort, will remove duplicate seperators, will always assume that a
214
+ # fragment argument represents a relative and not absolute path, and throws
215
+ # an exception when an absolute URI is received for a fragment argument.
216
+ #
217
+ # This is separate from the semantics for `#join`, which are well-defined by
218
+ # RFC3986 section 5.2 as part of the merging and normalization process;
219
+ # this method does not perform any normalization, removal of spurious
220
+ # paths, or removal of parent directory references `(/../)`.
221
+ #
222
+ # See also `#+`, which concatenates the string forms of two URIs without
223
+ # any sort of checking or processing.
224
+ #
225
+ # For an up-to-date list of edge case behavior, see the shared examples for
226
+ # RDF::URI in the rdf-spec project.
227
+ #
228
+ # @param [Any] fragment A URI fragment to be appended to this URI
229
+ # @return [RDF::URI]
230
+ # @see RDF::URI#+
231
+ # @see RDF::URI#join
232
+ # @see <http://tools.ietf.org/html/rfc3986#section-5.2>
233
+ # @see <http://github.com/bendiken/rdf-spec/blob/master/lib/rdf/spec/uri.rb>
234
+ # @example Building a HTTP URL
235
+ # RDF::URI.new('http://example.org') / 'jhacker' / 'foaf.ttl'
236
+ # #=> RDF::URI('http://example.org/jhacker/foaf.ttl')
237
+ # @example Building a HTTP URL
238
+ # RDF::URI.new('http://example.org/') / '/jhacker/' / '/foaf.ttl'
239
+ # #=> RDF::URI('http://example.org/jhacker/foaf.ttl')
240
+ # @example Using an anchored base URI
241
+ # RDF::URI.new('http://example.org/users#') / 'jhacker'
242
+ # #=> RDF::URI('http://example.org/users#jhacker')
243
+ # @example Building a URN
244
+ # RDF::URI.new('urn:isbn') / 125235111
245
+ # #=> RDF::URI('urn:isbn:125235111')
246
+ def /(fragment)
247
+ frag = fragment.respond_to?(:to_uri) ? fragment.to_uri : RDF::URI(fragment.to_s)
248
+ raise ArgumentError, "Non-absolute URI or string required, got #{frag}" unless frag.relative?
249
+ if urn?
250
+ RDF::URI.intern(to_s.sub(/:+$/,'') + ':' + fragment.to_s.sub(/^:+/,''))
251
+ else # !urn?
252
+ case to_s[-1].chr
253
+ when '#'
254
+ case fragment.to_s[0].chr
255
+ when '/' then # Base ending with '#', fragment beginning with '/'. The fragment wins, we use '/'.
256
+ RDF::URI.intern(to_s.sub(/#+$/,'') + '/' + fragment.to_s.sub(/^\/+/,''))
257
+ else
258
+ RDF::URI.intern(to_s.sub(/#+$/,'') + '#' + fragment.to_s.sub(/^#+/,''))
259
+ end
260
+ else # includes '/'. Results from bases ending in '/' are the same as if there were no trailing slash.
261
+ case fragment.to_s[0].chr
262
+ when '#' then # Base ending with '/', fragment beginning with '#'. The fragment wins, we use '#'.
263
+ RDF::URI.intern(to_s.sub(/\/+$/,'') + '#' + fragment.to_s.sub(/^#+/,''))
264
+ else
265
+ RDF::URI.intern(to_s.sub(/\/+$/,'') + '/' + fragment.to_s.sub(/^\/+/,''))
266
+ end
267
+ end
268
+ end
269
+ end
270
+
271
+ ##
272
+ # Simple concatenation operator. Returns a URI formed from concatenating
273
+ # the string form of two elements.
274
+ #
275
+ # For building URIs from fragments, you may want to use the smart
276
+ # separator, `#/`. `#join` implements another set of URI building
277
+ # semantics.
278
+ #
279
+ # @example Concatenating a string to a URI
280
+ # RDF::URI.new('http://example.org/test') + 'test'
281
+ # #=> RDF::URI('http://example.org/testtest')
282
+ # @example Concatenating two URIs
283
+ # RDF::URI.new('http://example.org/test') + RDF::URI.new('test')
284
+ # #=> RDF::URI('http://example.org/testtest')
285
+ # @see RDF::URI#/
286
+ # @see RDF::URI#join
287
+ # @param [Any]
288
+ # @return [RDF::URI]
289
+ def +(other)
290
+ RDF::URI.intern(self.to_s + other.to_s)
291
+ end
292
+
293
+ ##
294
+ # Returns `true` if this URI's path component is equal to `/`.
295
+ #
296
+ # @example
297
+ # RDF::URI('http://example.org/').root? #=> true
298
+ # RDF::URI('http://example.org/path/').root? #=> false
299
+ #
300
+ # @return [Boolean] `true` or `false`
301
+ def root?
302
+ self.path == '/' || self.path.empty?
303
+ end
304
+
305
+ ##
306
+ # Returns a copy of this URI with the path component set to `/`.
307
+ #
308
+ # @example
309
+ # RDF::URI('http://example.org/').root #=> RDF::URI('http://example.org/')
310
+ # RDF::URI('http://example.org/path/').root #=> RDF::URI('http://example.org/')
311
+ #
312
+ # @return [RDF::URI]
313
+ def root
314
+ if root?
315
+ self
316
+ else
317
+ uri = self.dup
318
+ uri.path = '/'
319
+ uri
320
+ end
321
+ end
322
+
323
+ ##
324
+ # Returns `true` if this URI's path component isn't equal to `/`.
325
+ #
326
+ # @example
327
+ # RDF::URI('http://example.org/').has_parent? #=> false
328
+ # RDF::URI('http://example.org/path/').has_parent? #=> true
329
+ #
330
+ # @return [Boolean] `true` or `false`
331
+ def has_parent?
332
+ !root?
333
+ end
334
+
335
+ ##
336
+ # Returns a copy of this URI with the path component ascended to the
337
+ # parent directory, if any.
338
+ #
339
+ # @example
340
+ # RDF::URI('http://example.org/').parent #=> nil
341
+ # RDF::URI('http://example.org/path/').parent #=> RDF::URI('http://example.org/')
342
+ #
343
+ # @return [RDF::URI]
344
+ def parent
345
+ case
346
+ when root? then nil
347
+ else
348
+ require 'pathname' unless defined?(Pathname)
349
+ if path = Pathname.new(self.path).parent
350
+ uri = self.dup
351
+ uri.path = path.to_s
352
+ uri.path << '/' unless uri.root?
353
+ uri
354
+ end
355
+ end
356
+ end
357
+
358
+ ##
359
+ # Returns a qualified name (QName) for this URI, if possible.
360
+ #
361
+ # @example
362
+ # RDF::URI('http://purl.org/dc/terms/').qname #=> [:dc, nil]
363
+ # RDF::URI('http://purl.org/dc/terms/title').qname #=> [:dc, :title]
364
+ # RDF::DC.title.qname #=> [:dc, :title]
365
+ #
366
+ # @return [Array(Symbol, Symbol)] or `nil` if no QName found
367
+ def qname
368
+ if self.to_s =~ %r([:/#]([^:/#]*)$)
369
+ local_name = $1
370
+ vocab_uri = local_name.empty? ? self.to_s : self.to_s[0...-(local_name.length)]
371
+ Vocabulary.each do |vocab|
372
+ if vocab.to_uri == vocab_uri
373
+ prefix = vocab.equal?(RDF) ? :rdf : vocab.__prefix__
374
+ return [prefix, local_name.empty? ? nil : local_name.to_sym]
375
+ end
376
+ end
377
+ else
378
+ Vocabulary.each do |vocab|
379
+ vocab_uri = vocab.to_uri
380
+ if self.start_with?(vocab_uri)
381
+ prefix = vocab.equal?(RDF) ? :rdf : vocab.__prefix__
382
+ local_name = self.to_s[vocab_uri.length..-1]
383
+ return [prefix, local_name.empty? ? nil : local_name.to_sym]
384
+ end
385
+ end
386
+ end
387
+ return nil # no QName found
388
+ end
389
+
390
+ ##
391
+ # Returns a duplicate copy of `self`.
392
+ #
393
+ # @return [RDF::URI]
394
+ def dup
395
+ self.class.new(@uri.dup)
396
+ end
397
+
398
+ ##
399
+ # @private
400
+ def freeze
401
+ @uri.freeze
402
+ super
403
+ end
404
+
405
+ ##
406
+ # Returns `true` if this URI starts with the given `string`.
407
+ #
408
+ # @example
409
+ # RDF::URI('http://example.org/').start_with?('http') #=> true
410
+ # RDF::URI('http://example.org/').start_with?('ftp') #=> false
411
+ #
412
+ # @param [String, #to_s] string
413
+ # @return [Boolean] `true` or `false`
414
+ # @see String#start_with?
415
+ # @since 0.3.0
416
+ def start_with?(string)
417
+ to_s.start_with?(string.to_s)
418
+ end
419
+ alias_method :starts_with?, :start_with?
420
+
421
+ ##
422
+ # Returns `true` if this URI ends with the given `string`.
423
+ #
424
+ # @example
425
+ # RDF::URI('http://example.org/').end_with?('/') #=> true
426
+ # RDF::URI('http://example.org/').end_with?('#') #=> false
427
+ #
428
+ # @param [String, #to_s] string
429
+ # @return [Boolean] `true` or `false`
430
+ # @see String#end_with?
431
+ # @since 0.3.0
432
+ def end_with?(string)
433
+ to_s.end_with?(string.to_s)
434
+ end
435
+ alias_method :ends_with?, :end_with?
436
+
437
+ ##
438
+ # Checks whether this URI the same term as `other'.
439
+ #
440
+ # @example
441
+ # RDF::URI('http://t.co/').eql?(RDF::URI('http://t.co/')) #=> true
442
+ # RDF::URI('http://t.co/').eql?('http://t.co/') #=> false
443
+ # RDF::URI('http://purl.org/dc/terms/').eql?(RDF::DC) #=> false
444
+ #
445
+ # @param [RDF::URI] other
446
+ # @return [Boolean] `true` or `false`
447
+ def eql?(other)
448
+ other.is_a?(URI) && self == other
449
+ end
450
+
451
+ ##
452
+ # Checks whether this URI is equal to `other` (type checking).
453
+ #
454
+ # Per SPARQL data-r2/expr-equal/eq-2-2, numeric can't be compared with other types
455
+ #
456
+ # @example
457
+ # RDF::URI('http://t.co/') == RDF::URI('http://t.co/') #=> true
458
+ # RDF::URI('http://t.co/') == 'http://t.co/' #=> true
459
+ # RDF::URI('http://purl.org/dc/terms/') == RDF::DC #=> true
460
+ #
461
+ # @param [Object] other
462
+ # @return [Boolean] `true` or `false`
463
+ # @see http://www.w3.org/TR/rdf-sparql-query/#func-RDFterm-equal
464
+ def ==(other)
465
+ case other
466
+ when Literal
467
+ # If other is a Literal, reverse test to consolodate complex type checking logic
468
+ other == self
469
+ when String then to_s == other
470
+ when URI, Addressable::URI then to_s == other.to_s
471
+ else other.respond_to?(:to_uri) && to_s == other.to_uri.to_s
472
+ end
473
+ end
474
+
475
+ ##
476
+ # Checks for case equality to the given `other` object.
477
+ #
478
+ # @example
479
+ # RDF::URI('http://example.org/') === /example/ #=> true
480
+ # RDF::URI('http://example.org/') === /foobar/ #=> false
481
+ # RDF::URI('http://t.co/') === RDF::URI('http://t.co/') #=> true
482
+ # RDF::URI('http://t.co/') === 'http://t.co/' #=> true
483
+ # RDF::URI('http://purl.org/dc/terms/') === RDF::DC #=> true
484
+ #
485
+ # @param [Object] other
486
+ # @return [Boolean] `true` or `false`
487
+ # @since 0.3.0
488
+ def ===(other)
489
+ case other
490
+ when Regexp then other === to_s
491
+ else self == other
492
+ end
493
+ end
494
+
495
+ ##
496
+ # Performs a pattern match using the given regular expression.
497
+ #
498
+ # @example
499
+ # RDF::URI('http://example.org/') =~ /example/ #=> 7
500
+ # RDF::URI('http://example.org/') =~ /foobar/ #=> nil
501
+ #
502
+ # @param [Regexp] pattern
503
+ # @return [Integer] the position the match starts
504
+ # @see String#=~
505
+ # @since 0.3.0
506
+ def =~(pattern)
507
+ case pattern
508
+ when Regexp then to_s =~ pattern
509
+ else super # `Object#=~` returns `false`
510
+ end
511
+ end
512
+
513
+ ##
514
+ # Returns `self`.
515
+ #
516
+ # @return [RDF::URI] `self`
517
+ def to_uri
518
+ self
519
+ end
520
+
521
+ ##
522
+ # Returns the string representation of this URI.
523
+ #
524
+ # @example
525
+ # RDF::URI('http://example.org/').to_str #=> 'http://example.org/'
526
+ #
527
+ # @return [String]
528
+ def to_str
529
+ @uri.to_s
530
+ end
531
+ alias_method :to_s, :to_str
532
+
533
+ ##
534
+ # Returns a hash code for this URI.
535
+ #
536
+ # @return [Fixnum]
537
+ def hash
538
+ @uri.hash
539
+ end
540
+
541
+ ##
542
+ # Returns `true` if this URI instance supports the `symbol` method.
543
+ #
544
+ # @param [Symbol, String, #to_s] symbol
545
+ # @return [Boolean] `true` or `false`
546
+ def respond_to?(symbol)
547
+ @uri.respond_to?(symbol) || super
548
+ end
549
+
550
+ protected
551
+
552
+ ##
553
+ # @param [Symbol, String, #to_s] symbol
554
+ # @param [Array<Object>] args
555
+ # @yield
556
+ # @return [Object]
557
+ # @private
558
+ def method_missing(symbol, *args, &block)
559
+ if @uri.respond_to?(symbol)
560
+ case result = @uri.send(symbol, *args, &block)
561
+ when Addressable::URI
562
+ self.class.new(result)
563
+ else result
564
+ end
565
+ else
566
+ super
567
+ end
568
+ end
569
+ end # URI
570
+ end # RDF
@@ -0,0 +1,133 @@
1
+ module RDF
2
+ ##
3
+ # An RDF value.
4
+ #
5
+ # This is the basis for the RDF.rb class hierarchy. Anything that can be a
6
+ # term of {RDF::Statement RDF statements} should directly or indirectly
7
+ # include this module.
8
+ #
9
+ # @example Checking if a value is a resource (blank node or URI reference)
10
+ # value.resource?
11
+ #
12
+ # @example Checking if a value is a blank node
13
+ # value.node?
14
+ #
15
+ # @example Checking if a value is a URI reference
16
+ # value.uri?
17
+ # value.iri?
18
+ #
19
+ # @example Checking if a value is a literal
20
+ # value.literal?
21
+ #
22
+ # @see RDF::Graph
23
+ # @see RDF::Literal
24
+ # @see RDF::Node
25
+ # @see RDF::Resource
26
+ # @see RDF::Statement
27
+ # @see RDF::URI
28
+ module Value
29
+ ##
30
+ # Returns `true` if `self` is a graph.
31
+ #
32
+ # @return [Boolean]
33
+ def graph?
34
+ false
35
+ end
36
+
37
+ ##
38
+ # Returns `true` if `self` is a literal.
39
+ #
40
+ # @return [Boolean]
41
+ def literal?
42
+ false
43
+ end
44
+
45
+ ##
46
+ # Returns `true` if `self` is a blank node.
47
+ #
48
+ # @return [Boolean]
49
+ def node?
50
+ false
51
+ end
52
+
53
+ ##
54
+ # Returns `true` if `self` is a resource.
55
+ #
56
+ # @return [Boolean]
57
+ def resource?
58
+ false
59
+ end
60
+
61
+ ##
62
+ # Returns `true` if `self` is a statement.
63
+ #
64
+ # @return [Boolean]
65
+ def statement?
66
+ false
67
+ end
68
+
69
+ ##
70
+ # Returns `true` if `self` is an IRI reference.
71
+ #
72
+ # By default this is simply an alias for {#uri?}.
73
+ #
74
+ # @return [Boolean]
75
+ def iri?
76
+ uri?
77
+ end
78
+
79
+ ##
80
+ # Returns `true` if `self` is a URI reference.
81
+ #
82
+ # @return [Boolean]
83
+ def uri?
84
+ false
85
+ end
86
+
87
+ ##
88
+ # Returns `true` if `self` is a query variable.
89
+ #
90
+ # @return [Boolean]
91
+ # @since 0.1.7
92
+ def variable?
93
+ false
94
+ end
95
+
96
+ ##
97
+ # Returns an `RDF::Value` representation of `self`.
98
+ #
99
+ # @return [RDF::Value]
100
+ def to_rdf
101
+ self
102
+ end
103
+
104
+ ##
105
+ # Returns a developer-friendly representation of `self`.
106
+ #
107
+ # The result will be of the format `#<RDF::Value::0x12345678(...)>`,
108
+ # where `...` is the string returned by `#to_s`.
109
+ #
110
+ # @return [String]
111
+ def inspect
112
+ sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s)
113
+ end
114
+
115
+ ##
116
+ # Outputs a developer-friendly representation of `self` to `stderr`.
117
+ #
118
+ # @return [void]
119
+ def inspect!
120
+ warn(inspect)
121
+ end
122
+
123
+ ##
124
+ # Default implementation of raise_error, which returns false.
125
+ # Classes including RDF::TypeCheck will raise TypeError
126
+ # instead.
127
+ #
128
+ # @return [false]
129
+ def type_error(message)
130
+ false
131
+ end
132
+ end # Value
133
+ end # RDF