rdf 0.1.9 → 0.1.10

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.
data/README CHANGED
@@ -6,6 +6,7 @@ This is a pure-Ruby library for working with [Resource Description Framework
6
6
 
7
7
  * <http://github.com/bendiken/rdf>
8
8
  * <http://blog.datagraph.org/2010/03/rdf-for-ruby>
9
+ * <http://blog.datagraph.org/2010/04/parsing-rdf-with-ruby>
9
10
  * <http://blog.datagraph.org/2010/04/rdf-repository-howto>
10
11
 
11
12
  Features
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.9
1
+ 0.1.10
@@ -107,6 +107,8 @@ module RDF
107
107
  #
108
108
  # If no block was given, returns an enumerator.
109
109
  #
110
+ # The order in which statements are yielded is undefined.
111
+ #
110
112
  # @overload each_statement
111
113
  # @yield [statement]
112
114
  # @yieldparam [Statement] statement
@@ -162,6 +164,8 @@ module RDF
162
164
  #
163
165
  # If no block was given, returns an enumerator.
164
166
  #
167
+ # The order in which triples are yielded is undefined.
168
+ #
165
169
  # @overload each_triple
166
170
  # @yield [subject, predicate, object]
167
171
  # @yieldparam [Resource] subject
@@ -220,6 +224,8 @@ module RDF
220
224
  #
221
225
  # If no block was given, returns an enumerator.
222
226
  #
227
+ # The order in which quads are yielded is undefined.
228
+ #
223
229
  # @overload each_quad
224
230
  # @yield [subject, predicate, object, context]
225
231
  # @yieldparam [Resource] subject
@@ -284,6 +290,8 @@ module RDF
284
290
  #
285
291
  # If no block was given, returns an enumerator.
286
292
  #
293
+ # The order in which values are yielded is undefined.
294
+ #
287
295
  # @overload each_subject
288
296
  # @yield [subject]
289
297
  # @yieldparam [Resource] subject
@@ -350,6 +358,8 @@ module RDF
350
358
  #
351
359
  # If no block was given, returns an enumerator.
352
360
  #
361
+ # The order in which values are yielded is undefined.
362
+ #
353
363
  # @overload each_predicate
354
364
  # @yield [predicate]
355
365
  # @yieldparam [URI] predicate
@@ -416,6 +426,8 @@ module RDF
416
426
  #
417
427
  # If no block was given, returns an enumerator.
418
428
  #
429
+ # The order in which values are yielded is undefined.
430
+ #
419
431
  # @overload each_object
420
432
  # @yield [object]
421
433
  # @yieldparam [Value] object
@@ -482,6 +494,8 @@ module RDF
482
494
  #
483
495
  # If no block was given, returns an enumerator.
484
496
  #
497
+ # The order in which values are yielded is undefined.
498
+ #
485
499
  # @overload each_context
486
500
  # @yield [context]
487
501
  # @yieldparam [Resource] context
@@ -523,6 +537,8 @@ module RDF
523
537
  #
524
538
  # If no block was given, returns an enumerator.
525
539
  #
540
+ # The order in which graphs are yielded is undefined.
541
+ #
526
542
  # @overload each_graph
527
543
  # @yield [graph]
528
544
  # @yieldparam [RDF::Graph] graph
@@ -533,7 +549,7 @@ module RDF
533
549
  #
534
550
  # @return [void]
535
551
  # @see #enum_graph
536
- # @since 0.1.19
552
+ # @since 0.1.9
537
553
  def each_graph(&block)
538
554
  if block_given?
539
555
  block.call(RDF::Graph.new(nil, :data => self))
@@ -550,7 +566,7 @@ module RDF
550
566
  #
551
567
  # @return [Enumerator]
552
568
  # @see #each_graph
553
- # @since 0.1.19
569
+ # @since 0.1.9
554
570
  def enum_graph
555
571
  Enumerator.new(self, :each_graph)
556
572
  end
@@ -56,7 +56,7 @@ module RDF
56
56
  # @return [RDF::Statement]
57
57
  #
58
58
  # @return [RDF::Statement]
59
- # @since 0.1.19
59
+ # @since 0.1.9
60
60
  def first(pattern = nil)
61
61
  if pattern
62
62
  query(pattern) do |statement|
@@ -82,7 +82,7 @@ module RDF
82
82
  # @return [RDF::Resource]
83
83
  #
84
84
  # @return [RDF::Resource]
85
- # @since 0.1.19
85
+ # @since 0.1.9
86
86
  def first_subject(pattern = nil)
87
87
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
88
88
  return statement.subject
@@ -104,7 +104,7 @@ module RDF
104
104
  # @return [RDF::URI]
105
105
  #
106
106
  # @return [RDF::URI]
107
- # @since 0.1.19
107
+ # @since 0.1.9
108
108
  def first_predicate(pattern = nil)
109
109
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
110
110
  return statement.predicate
@@ -126,7 +126,7 @@ module RDF
126
126
  # @return [RDF::Value]
127
127
  #
128
128
  # @return [RDF::Value]
129
- # @since 0.1.19
129
+ # @since 0.1.9
130
130
  def first_object(pattern = nil)
131
131
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
132
132
  return statement.object
@@ -149,7 +149,7 @@ module RDF
149
149
  # @return [RDF::Literal]
150
150
  #
151
151
  # @return [RDF::Literal]
152
- # @since 0.1.19
152
+ # @since 0.1.9
153
153
  def first_literal(pattern = nil)
154
154
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
155
155
  return statement.object if statement.object.is_a?(RDF::Literal)
@@ -172,7 +172,7 @@ module RDF
172
172
  # @return [Object]
173
173
  #
174
174
  # @return [Object]
175
- # @since 0.1.19
175
+ # @since 0.1.9
176
176
  def first_value(pattern = nil)
177
177
  (literal = first_literal(pattern)) ? literal.value : nil
178
178
  end
@@ -41,7 +41,7 @@ module RDF
41
41
  # @yield [graph]
42
42
  # @yieldparam [Graph] graph
43
43
  # @return [Graph]
44
- # @since 0.1.17
44
+ # @since 0.1.7
45
45
  def self.load(url, options = {}, &block)
46
46
  self.new(url, options) do |graph|
47
47
  graph.load! unless graph.unnamed?
@@ -83,7 +83,7 @@ module RDF
83
83
  # Returns `true` if this value is a query variable.
84
84
  #
85
85
  # @return [Boolean]
86
- # @since 0.1.17
86
+ # @since 0.1.7
87
87
  def variable?
88
88
  false
89
89
  end
@@ -38,6 +38,7 @@ module RDF
38
38
  #
39
39
  # @author [Arto Bendiken](http://ar.to/)
40
40
  module NTriples
41
+ require 'iconv' # needed on Ruby 1.8.x
41
42
  require 'rdf/ntriples/format'
42
43
  autoload :Reader, 'rdf/ntriples/reader'
43
44
  autoload :Writer, 'rdf/ntriples/writer'
@@ -42,6 +42,15 @@ module RDF::NTriples
42
42
  PREDICATE = Regexp.union(URIREF).freeze
43
43
  OBJECT = Regexp.union(URIREF, NODEID, LITERAL).freeze
44
44
 
45
+ # @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
46
+ ESCAPE_CHARS = ["\t", "\n", "\r", "\"", "\\"].freeze
47
+ ESCAPE_CHAR4 = /\\u([0-9A-Fa-f]{4,4})/.freeze
48
+ ESCAPE_CHAR8 = /\\U([0-9A-Fa-f]{8,8})/.freeze
49
+ ESCAPE_CHAR = Regexp.union(ESCAPE_CHAR4, ESCAPE_CHAR8).freeze
50
+ ESCAPE_SURROGATE = /\\u([0-9A-Fa-f]{4,4})\\u([0-9A-Fa-f]{4,4})/.freeze
51
+ ESCAPE_SURROGATE1 = (0xD800..0xDBFF).freeze
52
+ ESCAPE_SURROGATE2 = (0xDC00..0xDFFF).freeze
53
+
45
54
  ##
46
55
  # Reconstructs an RDF value from its serialized N-Triples
47
56
  # representation.
@@ -112,12 +121,35 @@ module RDF::NTriples
112
121
  # @param [String] string
113
122
  # @return [String]
114
123
  # @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
124
+ # @see http://blog.grayproductions.net/articles/understanding_m17n
115
125
  def self.unescape(string)
116
- ["\t", "\n", "\r", "\"", "\\"].each do |escape|
117
- string.gsub!(escape.inspect[1...-1], escape)
126
+ string.force_encoding(Encoding::ASCII_8BIT) if string.respond_to?(:force_encoding)
127
+
128
+ # Decode \t|\n|\r|\"|\\ character escapes:
129
+ ESCAPE_CHARS.each { |escape| string.gsub!(escape.inspect[1...-1], escape) }
130
+
131
+ # Decode \uXXXX\uXXXX surrogate pairs:
132
+ while
133
+ (string.sub!(ESCAPE_SURROGATE) do
134
+ if ESCAPE_SURROGATE1.include?($1.hex) && ESCAPE_SURROGATE2.include?($2.hex)
135
+ s = [$1, $2].pack('H*H*')
136
+ s = s.respond_to?(:force_encoding) ?
137
+ s.force_encoding(Encoding::UTF_16BE).encode!(Encoding::UTF_8) : # for Ruby 1.9+
138
+ Iconv.conv('UTF-8', 'UTF-16BE', s) # for Ruby 1.8.x
139
+ else
140
+ s = [$1.hex].pack('U*') << '\u' << $2
141
+ end
142
+ s.respond_to?(:force_encoding) ? s.force_encoding(Encoding::ASCII_8BIT) : s
143
+ end)
118
144
  end
119
- string.gsub!(/\\u([0-9A-Fa-f]{4,4})/u) { [$1.hex].pack('U*') }
120
- string.gsub!(/\\U([0-9A-Fa-f]{8,8})/u) { [$1.hex].pack('U*') }
145
+
146
+ # Decode \uXXXX and \UXXXXXXXX code points:
147
+ string.gsub!(ESCAPE_CHAR) do
148
+ s = [($1 || $2).hex].pack('U*')
149
+ s.respond_to?(:force_encoding) ? s.force_encoding(Encoding::ASCII_8BIT) : s
150
+ end
151
+
152
+ string.force_encoding(Encoding::UTF_8) if string.respond_to?(:force_encoding)
121
153
  string
122
154
  end
123
155
 
@@ -59,7 +59,7 @@ class RDF::Query
59
59
  #
60
60
  # @return [Boolean]
61
61
  # @see RDF::Value#variable?
62
- # @since 0.1.17
62
+ # @since 0.1.7
63
63
  def variable?
64
64
  true
65
65
  end
@@ -193,27 +193,48 @@ module RDF
193
193
 
194
194
  @@subclasses = [] # @private
195
195
 
196
- def self.inherited(child) #:nodoc:
196
+ ##
197
+ # @private
198
+ def self.inherited(child)
197
199
  @@subclasses << child
198
200
  super
199
201
  end
200
202
 
203
+ ##
204
+ # @return [Integer]
201
205
  def lineno
202
206
  @input.lineno
203
207
  end
204
208
 
209
+ ##
210
+ # @return [String]
205
211
  def readline
206
212
  @line = @input.readline.chomp
213
+ @line.force_encoding(encoding) if @line.respond_to?(:force_encoding) # for Ruby 1.9+
214
+ @line
207
215
  end
208
216
 
217
+ ##
218
+ # @return [Encoding]
219
+ def encoding
220
+ @encoding ||= ::Encoding::UTF_8
221
+ end
222
+
223
+ ##
224
+ # @return [void]
209
225
  def strip!
210
226
  @line.strip!
211
227
  end
212
228
 
229
+ ##
230
+ # @return [Boolean]
213
231
  def blank?
214
232
  @line.nil? || @line.empty?
215
233
  end
216
234
 
235
+ ##
236
+ # @param [Regexp] pattern
237
+ # @return [Object]
217
238
  def match(pattern)
218
239
  if @line =~ pattern
219
240
  result, @line = $1, $'.lstrip
@@ -128,6 +128,20 @@ module RDF
128
128
  ##
129
129
  # @see RDF::Repository
130
130
  module Implementation
131
+ ##
132
+ # Returns `true` if this repository supports `feature`.
133
+ #
134
+ # @param [Symbol, #to_sym] feature
135
+ # @return [Boolean]
136
+ # @since 0.1.10
137
+ def supports?(feature)
138
+ case feature.to_sym
139
+ when :context then true # statement contexts / named graphs
140
+ when :inference then false # forward-chaining inference
141
+ else false
142
+ end
143
+ end
144
+
131
145
  ##
132
146
  # Returns `false` to indicate that this repository is nondurable.
133
147
  #
@@ -2,7 +2,7 @@ module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 9
5
+ TINY = 10
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -295,12 +295,13 @@ module RDF
295
295
  # @param [Resource] uriref
296
296
  # @return [String]
297
297
  def uri_for(uriref)
298
- if uriref.respond_to?(:anonymous?) && uriref.anonymous?
299
- @nodes[uriref]
300
- elsif uriref.respond_to?(:to_uri)
301
- uriref.to_uri.to_s
302
- else
303
- uriref.to_s
298
+ case
299
+ when uriref.is_a?(RDF::Node)
300
+ @nodes[uriref]
301
+ when uriref.respond_to?(:to_uri)
302
+ uriref.to_uri.to_s
303
+ else
304
+ uriref.to_s
304
305
  end
305
306
  end
306
307
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 9
9
- version: 0.1.9
8
+ - 10
9
+ version: 0.1.10
10
10
  platform: ruby
11
11
  authors:
12
12
  - Arto Bendiken
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-21 00:00:00 +02:00
18
+ date: 2010-05-20 00:00:00 +02:00
19
19
  default_executable: rdf
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -28,8 +28,8 @@ dependencies:
28
28
  segments:
29
29
  - 0
30
30
  - 1
31
- - 8
32
- version: 0.1.8
31
+ - 10
32
+ version: 0.1.10
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -56,8 +56,8 @@ dependencies:
56
56
  segments:
57
57
  - 0
58
58
  - 5
59
- - 3
60
- version: 0.5.3
59
+ - 4
60
+ version: 0.5.4
61
61
  type: :development
62
62
  version_requirements: *id003
63
63
  - !ruby/object:Gem::Dependency