rdf 0.1.10 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
1
1
  module RDF
2
2
  ##
3
3
  module Durable
4
+ extend RDF::Util::Aliasing::LateBound
5
+
4
6
  ##
5
7
  # Returns `true` if `self` is durable.
6
8
  #
@@ -57,40 +57,19 @@ module RDF
57
57
  # @see RDF::Graph
58
58
  # @see RDF::Repository
59
59
  module Enumerable
60
+ extend RDF::Util::Aliasing::LateBound
61
+ include RDF::Countable
60
62
  include ::Enumerable
61
63
 
62
- ##
63
- # Returns `true` if `self` contains no RDF statements.
64
- #
65
- # @return [Boolean]
66
- def empty?
67
- empty = true
68
- each_statement { empty = false; break }
69
- empty
70
- end
71
-
72
- ##
73
- # Returns the number of RDF statements in `self`.
74
- #
75
- # @return [Integer]
76
- def count
77
- count = 0
78
- each_statement { count += 1 }
79
- count
80
- end
81
-
82
- alias_method :size, :count
83
- alias_method :length, :count
84
-
85
64
  ##
86
65
  # Returns all RDF statements.
87
66
  #
88
67
  # @param [Hash{Symbol => Boolean}] options
89
- # @return [Array<Statement>]
68
+ # @return [Enumerator<Statement>]
90
69
  # @see #each_statement
91
70
  # @see #enum_statement
92
71
  def statements(options = {})
93
- enum_statement.to_a
72
+ enum_statement
94
73
  end
95
74
 
96
75
  ##
@@ -99,9 +78,11 @@ module RDF
99
78
  # @param [Statement] statement
100
79
  # @return [Boolean]
101
80
  def has_statement?(statement)
102
- enum_statement.include?(statement)
81
+ !enum_statement.find { |s| s.eql?(statement) }.nil?
103
82
  end
104
83
 
84
+ alias_method :include?, :has_statement?
85
+
105
86
  ##
106
87
  # Iterates the given block for each RDF statement.
107
88
  #
@@ -134,7 +115,7 @@ module RDF
134
115
  # @return [Enumerator]
135
116
  # @see #each_statement
136
117
  def enum_statement
137
- Enumerator.new(self, :each_statement)
118
+ enum_for(:each_statement).extend(RDF::Queryable, RDF::Enumerable)
138
119
  end
139
120
 
140
121
  alias_method :enum_statements, :enum_statement
@@ -143,11 +124,11 @@ module RDF
143
124
  # Returns all RDF triples.
144
125
  #
145
126
  # @param [Hash{Symbol => Boolean}] options
146
- # @return [Array<Array(Resource, URI, Value)>]
127
+ # @return [Enumerator<Array(Resource, URI, Value)>]
147
128
  # @see #each_triple
148
129
  # @see #enum_triple
149
130
  def triples(options = {})
150
- enum_statement.map { |statement| statement.to_triple }
131
+ enum_statement.map(&:to_triple).to_enum # TODO: optimize
151
132
  end
152
133
 
153
134
  ##
@@ -194,7 +175,7 @@ module RDF
194
175
  # @return [Enumerator]
195
176
  # @see #each_triple
196
177
  def enum_triple
197
- Enumerator.new(self, :each_triple)
178
+ enum_for(:each_triple)
198
179
  end
199
180
 
200
181
  alias_method :enum_triples, :enum_triple
@@ -203,11 +184,11 @@ module RDF
203
184
  # Returns all RDF quads.
204
185
  #
205
186
  # @param [Hash{Symbol => Boolean}] options
206
- # @return [Array<Array(Resource, URI, Value, Resource)>]
187
+ # @return [Enumerator<Array(Resource, URI, Value, Resource)>]
207
188
  # @see #each_quad
208
189
  # @see #enum_quad
209
190
  def quads(options = {})
210
- enum_statement.map { |statement| statement.to_quad }
191
+ enum_statement.map(&:to_quad).to_enum # TODO: optimize
211
192
  end
212
193
 
213
194
  ##
@@ -255,7 +236,7 @@ module RDF
255
236
  # @return [Enumerator]
256
237
  # @see #each_quad
257
238
  def enum_quad
258
- Enumerator.new(self, :each_quad)
239
+ enum_for(:each_quad)
259
240
  end
260
241
 
261
242
  alias_method :enum_quads, :enum_quad
@@ -265,14 +246,14 @@ module RDF
265
246
  #
266
247
  # @param [Hash{Symbol => Boolean}] options
267
248
  # @option options [Boolean] :unique (true)
268
- # @return [Array<Resource>]
249
+ # @return [Enumerator<Resource>]
269
250
  # @see #each_subject
270
251
  # @see #enum_subject
271
252
  def subjects(options = {})
272
253
  if options[:unique] == false
273
- enum_statement.map { |statement| statement.subject }
254
+ enum_statement.map(&:subject).to_enum # TODO: optimize
274
255
  else
275
- enum_subject.to_a
256
+ enum_subject
276
257
  end
277
258
  end
278
259
 
@@ -323,7 +304,7 @@ module RDF
323
304
  # @return [Enumerator]
324
305
  # @see #each_subject
325
306
  def enum_subject
326
- Enumerator.new(self, :each_subject)
307
+ enum_for(:each_subject)
327
308
  end
328
309
 
329
310
  alias_method :enum_subjects, :enum_subject
@@ -333,14 +314,14 @@ module RDF
333
314
  #
334
315
  # @param [Hash{Symbol => Boolean}] options
335
316
  # @option options [Boolean] :unique (true)
336
- # @return [Array<URI>]
317
+ # @return [Enumerator<URI>]
337
318
  # @see #each_predicate
338
319
  # @see #enum_predicate
339
320
  def predicates(options = {})
340
321
  if options[:unique] == false
341
- enum_statement.map { |statement| statement.predicate }
322
+ enum_statement.map(&:predicate).to_enum # TODO: optimize
342
323
  else
343
- enum_predicate.to_a
324
+ enum_predicate
344
325
  end
345
326
  end
346
327
 
@@ -391,7 +372,7 @@ module RDF
391
372
  # @return [Enumerator]
392
373
  # @see #each_predicate
393
374
  def enum_predicate
394
- Enumerator.new(self, :each_predicate)
375
+ enum_for(:each_predicate)
395
376
  end
396
377
 
397
378
  alias_method :enum_predicates, :enum_predicate
@@ -401,14 +382,14 @@ module RDF
401
382
  #
402
383
  # @param [Hash{Symbol => Boolean}] options
403
384
  # @option options [Boolean] :unique (true)
404
- # @return [Array<Value>]
385
+ # @return [Enumerator<Value>]
405
386
  # @see #each_object
406
387
  # @see #enum_object
407
388
  def objects(options = {})
408
389
  if options[:unique] == false
409
- enum_statement.map { |statement| statement.object }
390
+ enum_statement.map(&:object).to_enum # TODO: optimize
410
391
  else
411
- enum_object.to_a
392
+ enum_object
412
393
  end
413
394
  end
414
395
 
@@ -459,7 +440,7 @@ module RDF
459
440
  # @return [Enumerator]
460
441
  # @see #each_object
461
442
  def enum_object
462
- Enumerator.new(self, :each_object)
443
+ enum_for(:each_object)
463
444
  end
464
445
 
465
446
  alias_method :enum_objects, :enum_object
@@ -469,14 +450,14 @@ module RDF
469
450
  #
470
451
  # @param [Hash{Symbol => Boolean}] options
471
452
  # @option options [Boolean] :unique (true)
472
- # @return [Array<Resource>]
453
+ # @return [Enumerator<Resource>]
473
454
  # @see #each_context
474
455
  # @see #enum_context
475
456
  def contexts(options = {})
476
457
  if options[:unique] == false
477
- enum_statement.map { |statement| statement.context }
458
+ enum_statement.map(&:context).to_enum # TODO: optimize
478
459
  else
479
- enum_context.to_a
460
+ enum_context
480
461
  end
481
462
  end
482
463
 
@@ -527,7 +508,7 @@ module RDF
527
508
  # @return [Enumerator]
528
509
  # @see #each_context
529
510
  def enum_context
530
- Enumerator.new(self, :each_context)
511
+ enum_for(:each_context)
531
512
  end
532
513
 
533
514
  alias_method :enum_contexts, :enum_context
@@ -568,11 +549,34 @@ module RDF
568
549
  # @see #each_graph
569
550
  # @since 0.1.9
570
551
  def enum_graph
571
- Enumerator.new(self, :each_graph)
552
+ enum_for(:each_graph)
572
553
  end
573
554
 
574
555
  alias_method :enum_graphs, :enum_graph
575
556
 
557
+ ##
558
+ # Returns all RDF statements in `self` as an array.
559
+ #
560
+ # Mixes in `RDF::Enumerable` into the returned object.
561
+ #
562
+ # @return [Array]
563
+ # @since 0.2.0
564
+ def to_a
565
+ super.extend(RDF::Enumerable)
566
+ end
567
+
568
+ ##
569
+ # Returns all RDF statements in `self` as a set.
570
+ #
571
+ # Mixes in `RDF::Enumerable` into the returned object.
572
+ #
573
+ # @return [Set]
574
+ # @since 0.2.0
575
+ def to_set
576
+ require 'set' unless defined?(::Set)
577
+ super.extend(RDF::Enumerable)
578
+ end
579
+
576
580
  ##
577
581
  # Returns all RDF objects indexed by their subjects and predicates.
578
582
  #
@@ -591,5 +595,23 @@ module RDF
591
595
  end
592
596
  result
593
597
  end
598
+
599
+ ##
600
+ # Returns a serialized string representation of `self`.
601
+ #
602
+ # Before calling this method you may need to explicitly require a
603
+ # serialization plugin for the specified format.
604
+ #
605
+ # @example Serializing into N-Triples format
606
+ # require 'rdf/ntriples'
607
+ # ntriples = enumerable.dump(:ntriples)
608
+ #
609
+ # @param [Array<Object>] args
610
+ # @return [String]
611
+ # @see RDF::Writer.dump
612
+ # @since 0.2.0
613
+ def dump(*args)
614
+ RDF::Writer.for(*args).dump(self)
615
+ end
594
616
  end
595
617
  end
@@ -1,6 +1,5 @@
1
1
  module RDF
2
2
  ##
3
- module Inferable
4
- # TODO
5
- end
3
+ # @see http://github.com/bendiken/rdfs
4
+ module Inferable; end
6
5
  end
@@ -6,6 +6,7 @@ module RDF
6
6
  # @see RDF::Graph
7
7
  # @see RDF::Repository
8
8
  module Mutable
9
+ extend RDF::Util::Aliasing::LateBound
9
10
  include RDF::Readable
10
11
  include RDF::Writable
11
12
 
@@ -32,11 +33,11 @@ module RDF
32
33
  #
33
34
  # @param [String, #to_s] filename
34
35
  # @param [Hash{Symbol => Object}] options
35
- # @return [Integer] the number of inserted RDF statements
36
+ # @return [void]
36
37
  def load(filename, options = {})
37
38
  raise TypeError.new("#{self} is immutable") if immutable?
38
39
 
39
- Reader.open(filename, options) do |reader|
40
+ Reader.open(filename, {:base_uri => filename}.merge(options)) do |reader|
40
41
  if options[:context]
41
42
  statements = []
42
43
  reader.each_statement do |statement|
@@ -47,7 +48,7 @@ module RDF
47
48
  statements.size
48
49
  else
49
50
  insert_statements(reader)
50
- nil # FIXME
51
+ nil
51
52
  end
52
53
  end
53
54
  end
@@ -55,50 +56,58 @@ module RDF
55
56
  alias_method :load!, :load
56
57
 
57
58
  ##
58
- # Inserts an RDF statement into `self`.
59
+ # Inserts RDF data into `self`.
59
60
  #
60
- # @param [RDF::Statement, Array<RDF::Value>, #to_a] statement
61
+ # @param [RDF::Enumerable, RDF::Statement, #to_rdf] data
62
+ # @raise [TypeError] if `self` is immutable
61
63
  # @return [Mutable]
62
- def <<(statement)
64
+ # @see RDF::Writable#<<
65
+ def <<(data)
63
66
  raise TypeError.new("#{self} is immutable") if immutable?
64
67
 
65
- insert_statement(create_statement(statement))
66
-
67
- return self
68
+ super # RDF::Writable#<<
68
69
  end
69
70
 
70
71
  ##
71
72
  # Inserts RDF statements into `self`.
72
73
  #
73
- # @param [Enumerable<RDF::Statement>] statements
74
+ # @param [Array<RDF::Statement>] statements
74
75
  # @raise [TypeError] if `self` is immutable
75
76
  # @return [Mutable]
77
+ # @see RDF::Writable#insert
76
78
  def insert(*statements)
77
79
  raise TypeError.new("#{self} is immutable") if immutable?
78
80
 
79
- statements.map! do |value|
80
- case
81
- when value.respond_to?(:each_statement)
82
- insert_statements(value)
83
- nil
84
- when (statement = create_statement(value)).valid?
85
- statement
86
- else
87
- raise ArgumentError.new("not a valid statement: #{value.inspect}")
81
+ super # RDF::Writable#insert
82
+ end
83
+
84
+ ##
85
+ # Updates RDF statements in `self`.
86
+ #
87
+ # `#update([subject, predicate, object])` is equivalent to
88
+ # `#delete([subject, predicate, nil])` followed by
89
+ # `#insert([subject, predicate, object])` unless `object` is `nil`.
90
+ #
91
+ # @param [Enumerable<RDF::Statement>] statements
92
+ # @raise [TypeError] if `self` is immutable
93
+ # @return [Mutable]
94
+ def update(*statements)
95
+ raise TypeError.new("#{self} is immutable") if immutable?
96
+
97
+ statements.each do |statement|
98
+ if (statement = create_statement(statement))
99
+ delete([statement.subject, statement.predicate, nil])
100
+ insert(statement) if statement.has_object?
88
101
  end
89
102
  end
90
- statements.compact!
91
- insert_statements(statements) unless statements.empty?
92
-
93
- return self
94
103
  end
95
104
 
96
- alias_method :insert!, :insert
105
+ alias_method :update!, :update
97
106
 
98
107
  ##
99
108
  # Deletes RDF statements from `self`.
100
109
  #
101
- # @param [Enumerable<Statement>] statements
110
+ # @param [Enumerable<RDF::Statement>] statements
102
111
  # @raise [TypeError] if `self` is immutable
103
112
  # @return [Mutable]
104
113
  def delete(*statements)
@@ -124,32 +133,10 @@ module RDF
124
133
 
125
134
  alias_method :delete!, :delete
126
135
 
127
- ##
128
- # Updates RDF statements in `self`.
129
- #
130
- # `#update([subject, predicate, object])` is equivalent to
131
- # `#delete([subject, predicate, nil])` followed by
132
- # `#insert([subject, predicate, object])` unless `object` is `nil`.
133
- #
134
- # @param [Enumerable<RDF::Statement>] statements
135
- # @raise [TypeError] if `self` is immutable
136
- # @return [Mutable]
137
- def update(*statements)
138
- raise TypeError.new("#{self} is immutable") if immutable?
139
-
140
- statements.each do |statement|
141
- if (statement = create_statement(statement))
142
- delete([statement.subject, statement.predicate, nil])
143
- insert(statement) if statement.has_object?
144
- end
145
- end
146
- end
147
-
148
- alias_method :update!, :update
149
-
150
136
  ##
151
137
  # Deletes all RDF statements from `self`.
152
138
  #
139
+ # @raise [TypeError] if `self` is immutable
153
140
  # @return [Mutable]
154
141
  def clear
155
142
  raise TypeError.new("#{self} is immutable") if immutable?
@@ -157,91 +144,48 @@ module RDF
157
144
  if respond_to?(:clear_statements)
158
145
  clear_statements
159
146
  else
160
- each_statement do |statement|
161
- delete_statement(statement)
162
- end
147
+ delete_statements(self)
163
148
  end
164
- self
149
+
150
+ return self
165
151
  end
166
152
 
167
153
  alias_method :clear!, :clear
168
154
 
169
- ##
170
- # Transforms various input into an `RDF::Statement` instance.
171
- #
172
- # @param [RDF::Statement, Hash, Array, #to_a] statement
173
- # @return [RDF::Statement]
174
- def create_statement(statement)
175
- case statement
176
- when Statement then statement
177
- when Hash then Statement.new(statement)
178
- when Array then Statement.new(*statement)
179
- else raise ArgumentError.new # FIXME
180
- end
181
- end
155
+ protected
182
156
 
183
157
  ##
184
- # Inserts an RDF statement into the underlying storage.
158
+ # Deletes the given RDF statements from the underlying storage.
185
159
  #
186
- # Subclasses of {RDF::Repository} must implement this method (except in
187
- # case they are immutable).
160
+ # Defaults to invoking {#delete_statement} for each given statement.
188
161
  #
189
- # @param [RDF::Statement] statement
190
- # @return [void]
191
- # @abstract
192
- def insert_statement(statement)
193
- raise NotImplementedError
194
- end
195
-
196
- ##
197
- # Inserts a list of RDF statement into the underlying storage.
162
+ # Subclasses of {RDF::Repository} may wish to override this method if
163
+ # they are capable of more efficiently deleting multiple statements at
164
+ # once.
198
165
  #
199
- # Subclasses of {RDF::Repository} may implement this method if they can
200
- # efficiently insert multiple statements at once. This will otherwise
201
- # default to invoking {#insert_statement} for each given statement.
202
- #
203
- # @param [RDF::Enumerable, #each] statements
166
+ # @param [RDF::Enumerable] statements
204
167
  # @return [void]
205
- def insert_statements(statements)
168
+ def delete_statements(statements)
206
169
  each = statements.respond_to?(:each_statement) ? :each_statement : :each
207
170
  statements.__send__(each) do |statement|
208
- insert_statement(statement)
171
+ delete_statement(statement)
209
172
  end
210
173
  end
211
174
 
212
175
  ##
213
176
  # Deletes an RDF statement from the underlying storage.
214
177
  #
215
- # Subclasses of {RDF::Repository} must implement this method (except in
216
- # case they are immutable).
178
+ # Subclasses of {RDF::Repository} must implement this method, except if
179
+ # they are immutable.
217
180
  #
218
181
  # @param [RDF::Statement] statement
219
182
  # @return [void]
220
183
  # @abstract
221
184
  def delete_statement(statement)
222
- raise NotImplementedError
223
- end
224
-
225
- ##
226
- # Deletes a list of RDF statement from the underlying storage.
227
- #
228
- # Subclasses of {RDF::Repository} may implement this method if they can
229
- # efficiently delete multiple statements at once. This will otherwise
230
- # default to invoking {#delete_statement} for each given statement.
231
- #
232
- # @param [RDF::Enumerable, #each] statements
233
- # @return [void]
234
- def delete_statements(statements)
235
- each = statements.respond_to?(:each_statement) ? :each_statement : :each
236
- statements.__send__(each) do |statement|
237
- delete_statement(statement)
238
- end
185
+ raise NotImplementedError.new("#{self.class}#delete_statement")
239
186
  end
240
187
 
241
- protected :create_statement
242
- protected :insert_statement
243
- protected :insert_statements
244
- protected :delete_statement
245
188
  protected :delete_statements
189
+ protected :delete_statement
246
190
  end
247
191
  end