rdf 0.1.8 → 0.1.9

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.8
1
+ 0.1.9
@@ -87,8 +87,8 @@ module RDF
87
87
  #
88
88
  # @param [Hash{Symbol => Boolean}] options
89
89
  # @return [Array<Statement>]
90
- # @see #each_statement
91
- # @see #enum_statement
90
+ # @see #each_statement
91
+ # @see #enum_statement
92
92
  def statements(options = {})
93
93
  enum_statement.to_a
94
94
  end
@@ -116,7 +116,7 @@ module RDF
116
116
  # @return [Enumerator]
117
117
  #
118
118
  # @return [void]
119
- # @see #enum_statement
119
+ # @see #enum_statement
120
120
  def each_statement(&block)
121
121
  if block_given?
122
122
  # Invoke {#each} in the containing class:
@@ -130,7 +130,7 @@ module RDF
130
130
  # Returns an enumerator for {#each_statement}.
131
131
  #
132
132
  # @return [Enumerator]
133
- # @see #each_statement
133
+ # @see #each_statement
134
134
  def enum_statement
135
135
  Enumerator.new(self, :each_statement)
136
136
  end
@@ -142,8 +142,8 @@ module RDF
142
142
  #
143
143
  # @param [Hash{Symbol => Boolean}] options
144
144
  # @return [Array<Array(Resource, URI, Value)>]
145
- # @see #each_triple
146
- # @see #enum_triple
145
+ # @see #each_triple
146
+ # @see #enum_triple
147
147
  def triples(options = {})
148
148
  enum_statement.map { |statement| statement.to_triple }
149
149
  end
@@ -173,7 +173,7 @@ module RDF
173
173
  # @return [Enumerator]
174
174
  #
175
175
  # @return [void]
176
- # @see #enum_triple
176
+ # @see #enum_triple
177
177
  def each_triple(&block)
178
178
  if block_given?
179
179
  each_statement do |statement|
@@ -188,7 +188,7 @@ module RDF
188
188
  # Returns an enumerator for {#each_triple}.
189
189
  #
190
190
  # @return [Enumerator]
191
- # @see #each_triple
191
+ # @see #each_triple
192
192
  def enum_triple
193
193
  Enumerator.new(self, :each_triple)
194
194
  end
@@ -200,8 +200,8 @@ module RDF
200
200
  #
201
201
  # @param [Hash{Symbol => Boolean}] options
202
202
  # @return [Array<Array(Resource, URI, Value, Resource)>]
203
- # @see #each_quad
204
- # @see #enum_quad
203
+ # @see #each_quad
204
+ # @see #enum_quad
205
205
  def quads(options = {})
206
206
  enum_statement.map { |statement| statement.to_quad }
207
207
  end
@@ -232,7 +232,7 @@ module RDF
232
232
  # @return [Enumerator]
233
233
  #
234
234
  # @return [void]
235
- # @see #enum_quad
235
+ # @see #enum_quad
236
236
  def each_quad(&block)
237
237
  if block_given?
238
238
  each_statement do |statement|
@@ -247,7 +247,7 @@ module RDF
247
247
  # Returns an enumerator for {#each_quad}.
248
248
  #
249
249
  # @return [Enumerator]
250
- # @see #each_quad
250
+ # @see #each_quad
251
251
  def enum_quad
252
252
  Enumerator.new(self, :each_quad)
253
253
  end
@@ -260,8 +260,8 @@ module RDF
260
260
  # @param [Hash{Symbol => Boolean}] options
261
261
  # @option options [Boolean] :unique (true)
262
262
  # @return [Array<Resource>]
263
- # @see #each_subject
264
- # @see #enum_subject
263
+ # @see #each_subject
264
+ # @see #enum_subject
265
265
  def subjects(options = {})
266
266
  if options[:unique] == false
267
267
  enum_statement.map { |statement| statement.subject }
@@ -293,7 +293,7 @@ module RDF
293
293
  # @return [Enumerator]
294
294
  #
295
295
  # @return [void]
296
- # @see #enum_subject
296
+ # @see #enum_subject
297
297
  def each_subject(&block)
298
298
  if block_given?
299
299
  values = {}
@@ -313,7 +313,7 @@ module RDF
313
313
  # Returns an enumerator for {#each_subject}.
314
314
  #
315
315
  # @return [Enumerator]
316
- # @see #each_subject
316
+ # @see #each_subject
317
317
  def enum_subject
318
318
  Enumerator.new(self, :each_subject)
319
319
  end
@@ -326,8 +326,8 @@ module RDF
326
326
  # @param [Hash{Symbol => Boolean}] options
327
327
  # @option options [Boolean] :unique (true)
328
328
  # @return [Array<URI>]
329
- # @see #each_predicate
330
- # @see #enum_predicate
329
+ # @see #each_predicate
330
+ # @see #enum_predicate
331
331
  def predicates(options = {})
332
332
  if options[:unique] == false
333
333
  enum_statement.map { |statement| statement.predicate }
@@ -359,7 +359,7 @@ module RDF
359
359
  # @return [Enumerator]
360
360
  #
361
361
  # @return [void]
362
- # @see #enum_predicate
362
+ # @see #enum_predicate
363
363
  def each_predicate(&block)
364
364
  if block_given?
365
365
  values = {}
@@ -379,7 +379,7 @@ module RDF
379
379
  # Returns an enumerator for {#each_predicate}.
380
380
  #
381
381
  # @return [Enumerator]
382
- # @see #each_predicate
382
+ # @see #each_predicate
383
383
  def enum_predicate
384
384
  Enumerator.new(self, :each_predicate)
385
385
  end
@@ -392,8 +392,8 @@ module RDF
392
392
  # @param [Hash{Symbol => Boolean}] options
393
393
  # @option options [Boolean] :unique (true)
394
394
  # @return [Array<Value>]
395
- # @see #each_object
396
- # @see #enum_object
395
+ # @see #each_object
396
+ # @see #enum_object
397
397
  def objects(options = {})
398
398
  if options[:unique] == false
399
399
  enum_statement.map { |statement| statement.object }
@@ -425,7 +425,7 @@ module RDF
425
425
  # @return [Enumerator]
426
426
  #
427
427
  # @return [void]
428
- # @see #enum_object
428
+ # @see #enum_object
429
429
  def each_object(&block) # FIXME: deduplication
430
430
  if block_given?
431
431
  values = {}
@@ -445,7 +445,7 @@ module RDF
445
445
  # Returns an enumerator for {#each_object}.
446
446
  #
447
447
  # @return [Enumerator]
448
- # @see #each_object
448
+ # @see #each_object
449
449
  def enum_object
450
450
  Enumerator.new(self, :each_object)
451
451
  end
@@ -458,8 +458,8 @@ module RDF
458
458
  # @param [Hash{Symbol => Boolean}] options
459
459
  # @option options [Boolean] :unique (true)
460
460
  # @return [Array<Resource>]
461
- # @see #each_context
462
- # @see #enum_context
461
+ # @see #each_context
462
+ # @see #enum_context
463
463
  def contexts(options = {})
464
464
  if options[:unique] == false
465
465
  enum_statement.map { |statement| statement.context }
@@ -491,7 +491,7 @@ module RDF
491
491
  # @return [Enumerator]
492
492
  #
493
493
  # @return [void]
494
- # @see #enum_context
494
+ # @see #enum_context
495
495
  def each_context(&block)
496
496
  if block_given?
497
497
  values = {}
@@ -511,13 +511,52 @@ module RDF
511
511
  # Returns an enumerator for {#each_context}.
512
512
  #
513
513
  # @return [Enumerator]
514
- # @see #each_context
514
+ # @see #each_context
515
515
  def enum_context
516
516
  Enumerator.new(self, :each_context)
517
517
  end
518
518
 
519
519
  alias_method :enum_contexts, :enum_context
520
520
 
521
+ ##
522
+ # Iterates the given block for each RDF graph in `self`.
523
+ #
524
+ # If no block was given, returns an enumerator.
525
+ #
526
+ # @overload each_graph
527
+ # @yield [graph]
528
+ # @yieldparam [RDF::Graph] graph
529
+ # @return [void]
530
+ #
531
+ # @overload each_graph
532
+ # @return [Enumerator]
533
+ #
534
+ # @return [void]
535
+ # @see #enum_graph
536
+ # @since 0.1.19
537
+ def each_graph(&block)
538
+ if block_given?
539
+ block.call(RDF::Graph.new(nil, :data => self))
540
+ each_context do |context|
541
+ block.call(RDF::Graph.new(context, :data => self))
542
+ end
543
+ else
544
+ enum_graph
545
+ end
546
+ end
547
+
548
+ ##
549
+ # Returns an enumerator for {#each_graph}.
550
+ #
551
+ # @return [Enumerator]
552
+ # @see #each_graph
553
+ # @since 0.1.19
554
+ def enum_graph
555
+ Enumerator.new(self, :each_graph)
556
+ end
557
+
558
+ alias_method :enum_graphs, :enum_graph
559
+
521
560
  ##
522
561
  # Returns all RDF objects indexed by their subjects and predicates.
523
562
  #
@@ -11,7 +11,7 @@ module RDF
11
11
  include ::Enumerable
12
12
 
13
13
  ##
14
- # Queries `self` for RDF statements matching the given pattern.
14
+ # Queries `self` for RDF statements matching the given `pattern`.
15
15
  #
16
16
  # @example
17
17
  # queryable.query([nil, RDF::DOAP.developer, nil])
@@ -22,7 +22,7 @@ module RDF
22
22
  # @yieldparam [Statement]
23
23
  # @return [Array<Statement>]
24
24
  def query(pattern, &block)
25
- raise TypeError.new("#{self} is not readable") if respond_to?(:readable) && !readable?
25
+ raise TypeError.new("#{self} is not readable") if respond_to?(:readable?) && !readable?
26
26
 
27
27
  case pattern
28
28
  when Query
@@ -41,5 +41,140 @@ module RDF
41
41
  raise ArgumentError.new("expected RDF::Query, RDF::Pattern, Array or Hash, but got #{pattern.inspect}")
42
42
  end
43
43
  end
44
+
45
+ ##
46
+ # Queries `self` for an RDF statement matching the given `pattern` and
47
+ # returns that statement if found.
48
+ #
49
+ # Returns `nil` if no statements match `pattern`.
50
+ #
51
+ # @overload first
52
+ # @return [RDF::Statement]
53
+ #
54
+ # @overload first(pattern)
55
+ # @param [Query, Statement, Array(Value), Hash] pattern
56
+ # @return [RDF::Statement]
57
+ #
58
+ # @return [RDF::Statement]
59
+ # @since 0.1.19
60
+ def first(pattern = nil)
61
+ if pattern
62
+ query(pattern) do |statement|
63
+ return statement
64
+ end
65
+ return nil
66
+ else
67
+ super()
68
+ end
69
+ end
70
+
71
+ ##
72
+ # Queries `self` for an RDF statement matching the given `pattern` and
73
+ # returns the statement's subject term.
74
+ #
75
+ # Returns `nil` if no statements match `pattern`.
76
+ #
77
+ # @overload first_subject
78
+ # @return [RDF::Resource]
79
+ #
80
+ # @overload first_subject(pattern)
81
+ # @param [Query, Statement, Array(Value), Hash] pattern
82
+ # @return [RDF::Resource]
83
+ #
84
+ # @return [RDF::Resource]
85
+ # @since 0.1.19
86
+ def first_subject(pattern = nil)
87
+ __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
88
+ return statement.subject
89
+ end
90
+ return nil
91
+ end
92
+
93
+ ##
94
+ # Queries `self` for an RDF statement matching the given `pattern` and
95
+ # returns the statement's predicate term.
96
+ #
97
+ # Returns `nil` if no statements match `pattern`.
98
+ #
99
+ # @overload first_predicate
100
+ # @return [RDF::URI]
101
+ #
102
+ # @overload first_predicate(pattern)
103
+ # @param [Query, Statement, Array(Value), Hash] pattern
104
+ # @return [RDF::URI]
105
+ #
106
+ # @return [RDF::URI]
107
+ # @since 0.1.19
108
+ def first_predicate(pattern = nil)
109
+ __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
110
+ return statement.predicate
111
+ end
112
+ return nil
113
+ end
114
+
115
+ ##
116
+ # Queries `self` for an RDF statement matching the given `pattern` and
117
+ # returns the statement's object term.
118
+ #
119
+ # Returns `nil` if no statements match `pattern`.
120
+ #
121
+ # @overload first_object
122
+ # @return [RDF::Value]
123
+ #
124
+ # @overload first_object(pattern)
125
+ # @param [Query, Statement, Array(Value), Hash] pattern
126
+ # @return [RDF::Value]
127
+ #
128
+ # @return [RDF::Value]
129
+ # @since 0.1.19
130
+ def first_object(pattern = nil)
131
+ __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
132
+ return statement.object
133
+ end
134
+ return nil
135
+ end
136
+
137
+ ##
138
+ # Queries `self` for RDF statements matching the given `pattern` and
139
+ # returns the first found object literal.
140
+ #
141
+ # Returns `nil` if no statements match `pattern` or if none of the found
142
+ # statements have a literal as their object term.
143
+ #
144
+ # @overload first_literal
145
+ # @return [RDF::Literal]
146
+ #
147
+ # @overload first_literal(pattern)
148
+ # @param [Query, Statement, Array(Value), Hash] pattern
149
+ # @return [RDF::Literal]
150
+ #
151
+ # @return [RDF::Literal]
152
+ # @since 0.1.19
153
+ def first_literal(pattern = nil)
154
+ __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
155
+ return statement.object if statement.object.is_a?(RDF::Literal)
156
+ end
157
+ return nil
158
+ end
159
+
160
+ ##
161
+ # Queries `self` for RDF statements matching the given `pattern` and
162
+ # returns the value of the first found object literal.
163
+ #
164
+ # Returns `nil` if no statements match `pattern` or if none of the found
165
+ # statements have a literal as their object term.
166
+ #
167
+ # @overload first_value
168
+ # @return [Object]
169
+ #
170
+ # @overload first_value(pattern)
171
+ # @param [Query, Statement, Array(Value), Hash] pattern
172
+ # @return [Object]
173
+ #
174
+ # @return [Object]
175
+ # @since 0.1.19
176
+ def first_value(pattern = nil)
177
+ (literal = first_literal(pattern)) ? literal.value : nil
178
+ end
44
179
  end
45
180
  end
@@ -1,6 +1,24 @@
1
1
  module RDF
2
2
  ##
3
3
  # An RDF graph.
4
+ #
5
+ # @example Creating an empty unnamed graph
6
+ # graph = Graph.new
7
+ #
8
+ # @example Creating an empty named graph
9
+ # graph = Graph.new("http://rubygems.org/")
10
+ #
11
+ # @example Loading graph data from a URL (1)
12
+ # require 'rdf/raptor' # for RDF/XML support
13
+ #
14
+ # graph = RDF::Graph.new("http://www.bbc.co.uk/programmes/b0081dq5.rdf")
15
+ # graph.load!
16
+ #
17
+ # @example Loading graph data from a URL (2)
18
+ # require 'rdf/raptor' # for RDF/XML support
19
+ #
20
+ # graph = RDF::Graph.load("http://www.bbc.co.uk/programmes/b0081dq5.rdf")
21
+ #
4
22
  class Graph < Resource
5
23
  include RDF::Enumerable
6
24
  include RDF::Mutable
@@ -18,19 +36,12 @@ module RDF
18
36
  # Creates a new `Graph` instance populated by the RDF data returned by
19
37
  # dereferencing the given context URL.
20
38
  #
21
- # @overload
22
- # @param [String, #to_s] url
23
- # @param [Hash{Symbol => Object}] options
24
- # @yield [graph]
25
- # @yieldparam [Graph] graph
26
- # @return [void]
27
- #
28
- # @overload
29
- # @param [String, #to_s] url
30
- # @param [Hash{Symbol => Object}] options
31
- # @return [Graph]
32
- #
33
- # @return [void]
39
+ # @param [String, #to_s] url
40
+ # @param [Hash{Symbol => Object}] options
41
+ # @yield [graph]
42
+ # @yieldparam [Graph] graph
43
+ # @return [Graph]
44
+ # @since 0.1.17
34
45
  def self.load(url, options = {}, &block)
35
46
  self.new(url, options) do |graph|
36
47
  graph.load! unless graph.unnamed?
@@ -83,6 +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
87
  def variable?
87
88
  false
88
89
  end
@@ -21,7 +21,8 @@ class RDF::Query
21
21
  # solution.to_hash #=> {:mbox => "jrhacker@example.org", ...}
22
22
  #
23
23
  class Solution
24
- undef_method *(instance_methods - %w(__id__ __send__ __class__ __eval__ instance_eval inspect class object_id is_a?))
24
+ # Undefine all superfluous instance methods:
25
+ undef_method *(instance_methods.map { |s| s.to_sym } - [:__id__, :__send__, :__class__, :__eval__, :object_id, :instance_eval, :inspect, :class, :is_a?])
25
26
 
26
27
  include Enumerable
27
28
 
@@ -59,6 +59,7 @@ class RDF::Query
59
59
  #
60
60
  # @return [Boolean]
61
61
  # @see RDF::Value#variable?
62
+ # @since 0.1.17
62
63
  def variable?
63
64
  true
64
65
  end
@@ -2,7 +2,7 @@ module RDF
2
2
  module VERSION
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 8
5
+ TINY = 9
6
6
  EXTRA = nil
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -117,7 +117,7 @@ module RDF
117
117
  end
118
118
 
119
119
  # Undefine all superfluous instance methods:
120
- undef_method *(instance_methods - %w(__id__ __send__ __class__ __eval__ instance_eval inspect class))
120
+ undef_method *(instance_methods.map { |s| s.to_sym } - [:__id__, :__send__, :__class__, :__eval__, :object_id, :instance_eval, :inspect, :class, :is_a?])
121
121
 
122
122
  ##
123
123
  # @param [URI, String, #to_s]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 8
9
- version: 0.1.8
8
+ - 9
9
+ version: 0.1.9
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-18 00:00:00 +02:00
18
+ date: 2010-04-21 00:00:00 +02:00
19
19
  default_executable: rdf
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency