rdf 0.3.0.pre → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -30,8 +30,9 @@ Features
30
30
  Examples
31
31
  --------
32
32
 
33
- require 'rubygems'
34
33
  require 'rdf'
34
+
35
+ include RDF
35
36
 
36
37
  ### Writing RDF data using the N-Triples format
37
38
 
@@ -53,10 +54,25 @@ Examples
53
54
  end
54
55
  end
55
56
 
56
- ### Using pre-defined RDF vocabularies
57
+ ### Querying RDF data using basic graph patterns (BGPs)
57
58
 
58
- include RDF
59
+ require 'rdf/ntriples'
59
60
 
61
+ graph = RDF::Graph.load("http://rdf.rubyforge.org/doap.nt")
62
+ query = RDF::Query.new({
63
+ :person => {
64
+ RDF.type => FOAF.Person,
65
+ FOAF.name => :name,
66
+ FOAF.mbox => :email,
67
+ }
68
+ })
69
+
70
+ query.execute(graph).each do |solution|
71
+ puts "name=#{solution.name} email=#{solution.email}"
72
+ end
73
+
74
+ ### Using pre-defined RDF vocabularies
75
+
60
76
  DC.title #=> RDF::URI("http://purl.org/dc/terms/title")
61
77
  FOAF.knows #=> RDF::URI("http://xmlns.com/foaf/0.1/knows")
62
78
  RDF.type #=> RDF::URI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
@@ -82,11 +98,12 @@ Documentation
82
98
  <http://blog.datagraph.org/2010/03/rdf-for-ruby>
83
99
 
84
100
  * {RDF::Value}
85
- * {RDF::Literal}
86
- * {RDF::Resource}
87
- * {RDF::Node}
88
- * {RDF::URI}
89
- * {RDF::Graph}
101
+ * {RDF::Term}
102
+ * {RDF::Literal}
103
+ * {RDF::Resource}
104
+ * {RDF::Node}
105
+ * {RDF::URI}
106
+ * {RDF::Graph}
90
107
  * {RDF::Statement}
91
108
 
92
109
  ### RDF Serialization
@@ -100,10 +117,13 @@ Documentation
100
117
  ### RDF Serialization Formats
101
118
 
102
119
  * {RDF::NTriples}
103
- * [`RDF::JSON`](http://rdf.rubyforge.org/json/) (plugin)
104
- * [`RDF::Trix`](http://rdf.rubyforge.org/trix/) (plugin)
105
- * [`RDF::Raptor::RDFXML`](http://rdf.rubyforge.org/raptor/) (plugin)
106
- * [`RDF::Raptor::Turtle`](http://rdf.rubyforge.org/raptor/) (plugin)
120
+ * [RDF::JSON](http://rdf.rubyforge.org/json/) (plugin)
121
+ * [RDF::N3](http://rdf.rubyforge.org/n3/) (plugin)
122
+ * [RDF::Raptor::RDFXML](http://rdf.rubyforge.org/raptor/) (plugin)
123
+ * [RDF::Raptor::Turtle](http://rdf.rubyforge.org/raptor/) (plugin)
124
+ * [RDF::RDFa](http://rdf.rubyforge.org/rdfa/) (plugin)
125
+ * [RDF::RDFXML](http://rdf.rubyforge.org/rdfxml/) (plugin)
126
+ * [RDF::Trix](http://rdf.rubyforge.org/trix/) (plugin)
107
127
 
108
128
  ### RDF Storage
109
129
 
@@ -117,8 +137,9 @@ Documentation
117
137
  * {RDF::Queryable}
118
138
  * {RDF::Mutable}
119
139
  * {RDF::Durable}
120
- * [`RDF::DataObjects`](http://rdf.rubyforge.org/do/) (plugin)
121
- * [`RDF::Sesame`](http://rdf.rubyforge.org/sesame/) (plugin)
140
+ * {RDF::Transaction}
141
+ * [RDF::DataObjects](http://rdf.rubyforge.org/do/) (plugin)
142
+ * [RDF::Sesame](http://rdf.rubyforge.org/sesame/) (plugin)
122
143
 
123
144
  ### RDF Querying
124
145
 
@@ -172,8 +193,8 @@ To get a local working copy of the development repository, do:
172
193
 
173
194
  % git clone git://github.com/bendiken/rdf.git
174
195
 
175
- Alternatively, you can download the latest development version as a tarball
176
- as follows:
196
+ Alternatively, download the latest development version as a tarball as
197
+ follows:
177
198
 
178
199
  % wget http://github.com/bendiken/rdf/tarball/master
179
200
 
@@ -230,11 +251,11 @@ Contributing
230
251
  License
231
252
  -------
232
253
 
233
- RDF.rb is free and unencumbered public domain software. For more
234
- information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
254
+ This is free and unencumbered public domain software. For more information,
255
+ see <http://unlicense.org/> or the accompanying {file:UNLICENSE} file.
235
256
 
236
257
  [RDF]: http://www.w3.org/RDF/
237
258
  [YARD]: http://yardoc.org/
238
- [YARD-GS]: http://yardoc.org/docs/yard/file:docs/GettingStarted.md
259
+ [YARD-GS]: http://rubydoc.info/docs/yard/file/docs/GettingStarted.md
239
260
  [PDD]: http://lists.w3.org/Archives/Public/public-rdf-ruby/2010May/0013.html
240
261
  [Backports]: http://rubygems.org/gems/backports
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0.pre
1
+ 0.3.0
@@ -37,11 +37,11 @@ module RDF
37
37
  # puts [subject, predicate, object, context].inspect
38
38
  # end
39
39
  #
40
- # @example Enumerating all values
41
- # enumerable.each_subject { |value| puts value.inspect }
42
- # enumerable.each_predicate { |value| puts value.inspect }
43
- # enumerable.each_object { |value| puts value.inspect }
44
- # enumerable.each_context { |value| puts value.inspect }
40
+ # @example Enumerating all terms
41
+ # enumerable.each_subject { |term| puts term.inspect }
42
+ # enumerable.each_predicate { |term| puts term.inspect }
43
+ # enumerable.each_object { |term| puts term.inspect }
44
+ # enumerable.each_context { |term| puts term.inspect }
45
45
  #
46
46
  # @example Obtaining all statements
47
47
  # enumerable.statements #=> [RDF::Statement(subject1, predicate1, object1), ...]
@@ -58,8 +58,8 @@ module RDF
58
58
  # @see RDF::Repository
59
59
  module Enumerable
60
60
  extend RDF::Util::Aliasing::LateBound
61
- include RDF::Countable
62
61
  include ::Enumerable
62
+ include RDF::Countable # NOTE: must come after ::Enumerable
63
63
 
64
64
  ##
65
65
  # Returns all RDF statements.
@@ -123,7 +123,7 @@ module RDF
123
123
  # Returns all RDF triples.
124
124
  #
125
125
  # @param [Hash{Symbol => Boolean}] options
126
- # @return [Enumerator<Array(RDF::Resource, RDF::URI, RDF::Value)>]
126
+ # @return [Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term)>]
127
127
  # @see #each_triple
128
128
  # @see #enum_triple
129
129
  def triples(options = {})
@@ -133,7 +133,7 @@ module RDF
133
133
  ##
134
134
  # Returns `true` if `self` contains the given RDF triple.
135
135
  #
136
- # @param [Array(RDF::Resource, RDF::URI, RDF::Value)] triple
136
+ # @param [Array(RDF::Resource, RDF::URI, RDF::Term)] triple
137
137
  # @return [Boolean]
138
138
  def has_triple?(triple)
139
139
  enum_triple.include?(triple)
@@ -151,7 +151,7 @@ module RDF
151
151
  # each triple
152
152
  # @yieldparam [RDF::Resource] subject
153
153
  # @yieldparam [RDF::URI] predicate
154
- # @yieldparam [RDF::Value] object
154
+ # @yieldparam [RDF::Term] object
155
155
  # @yieldreturn [void] ignored
156
156
  # @return [void]
157
157
  #
@@ -183,7 +183,7 @@ module RDF
183
183
  # Returns all RDF quads.
184
184
  #
185
185
  # @param [Hash{Symbol => Boolean}] options
186
- # @return [Enumerator<Array(RDF::Resource, RDF::URI, RDF::Value, RDF::Resource)>]
186
+ # @return [Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)>]
187
187
  # @see #each_quad
188
188
  # @see #enum_quad
189
189
  def quads(options = {})
@@ -193,7 +193,7 @@ module RDF
193
193
  ##
194
194
  # Returns `true` if `self` contains the given RDF quad.
195
195
  #
196
- # @param [Array(RDF::Resource, RDF::URI, RDF::Value, RDF::Resource)] quad
196
+ # @param [Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)] quad
197
197
  # @return [Boolean]
198
198
  def has_quad?(quad)
199
199
  enum_quad.include?(quad)
@@ -211,7 +211,7 @@ module RDF
211
211
  # each quad
212
212
  # @yieldparam [RDF::Resource] subject
213
213
  # @yieldparam [RDF::URI] predicate
214
- # @yieldparam [RDF::Value] object
214
+ # @yieldparam [RDF::Term] object
215
215
  # @yieldparam [RDF::Resource] context
216
216
  # @yieldreturn [void] ignored
217
217
  # @return [void]
@@ -381,7 +381,7 @@ module RDF
381
381
  #
382
382
  # @param [Hash{Symbol => Boolean}] options
383
383
  # @option options [Boolean] :unique (true)
384
- # @return [Enumerator<RDF::Value>]
384
+ # @return [Enumerator<RDF::Term>]
385
385
  # @see #each_object
386
386
  # @see #enum_object
387
387
  def objects(options = {})
@@ -395,7 +395,7 @@ module RDF
395
395
  ##
396
396
  # Returns `true` if `self` contains the given RDF object term.
397
397
  #
398
- # @param [RDF::Value] value
398
+ # @param [RDF::Term] value
399
399
  # @return [Boolean]
400
400
  def has_object?(value)
401
401
  enum_object.include?(value)
@@ -411,7 +411,7 @@ module RDF
411
411
  # @overload each_object
412
412
  # @yield [object]
413
413
  # each object term
414
- # @yieldparam [RDF::Value] object
414
+ # @yieldparam [RDF::Term] object
415
415
  # @yieldreturn [void] ignored
416
416
  # @return [void]
417
417
  #
@@ -20,9 +20,11 @@ module RDF
20
20
  # queryable.query([nil, RDF::DOAP.developer, nil])
21
21
  # queryable.query(:predicate => RDF::DOAP.developer)
22
22
  #
23
- # @param [Query, Statement, Array(Value), Hash] pattern
23
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
24
24
  # @yield [statement]
25
- # @yieldparam [RDF::Statement] statement
25
+ # each matching statement
26
+ # @yieldparam [RDF::Statement] statement
27
+ # @yieldreturn [void] ignored
26
28
  # @return [Enumerator]
27
29
  # @see RDF::Queryable#query_pattern
28
30
  def query(pattern, &block)
@@ -32,26 +34,43 @@ module RDF
32
34
  # A basic graph pattern (BGP) query:
33
35
  when Query
34
36
  if block_given?
37
+ before_query(pattern) if respond_to?(:before_query)
35
38
  query_execute(pattern, &block)
36
- return self
37
- else
38
- return enum_for(:query_execute, pattern)
39
+ after_query(pattern) if respond_to?(:after_query)
39
40
  end
41
+ enum_for(:query_execute, pattern)
40
42
 
41
- # A simple triple pattern query:
43
+ # A simple triple/quad pattern query:
42
44
  else
43
45
  pattern = Query::Pattern.from(pattern)
44
- if block_given?
45
- query_pattern(pattern, &block)
46
- return self
47
- else
48
- enum = enum_for(:query_pattern, pattern)
49
- enum.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable)
50
- def enum.to_a
51
- super.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable)
52
- end
53
- return enum
46
+ before_query(pattern) if block_given? && respond_to?(:before_query)
47
+ enum = case
48
+ # Blank triple/quad patterns are equivalent to iterating over
49
+ # every statement, so as a minor optimization we'll just do that
50
+ # directly instead of bothering with `#query_pattern`:
51
+ when pattern.blank?
52
+ each(&block) if block_given?
53
+ enum_for(:each)
54
+
55
+ # Constant triple/quad patterns are equivalent to looking up a
56
+ # particular statement, so as a minor optimization we'll just do
57
+ # that directly instead of bothering with `#query_pattern`:
58
+ when pattern.constant?
59
+ statement = Statement.from(pattern)
60
+ block.call(statement) if block_given? && include?(statement)
61
+ enum_for(:query, pattern)
62
+
63
+ # Otherwise, we delegate to `#query_pattern`:
64
+ else # pattern.variable?
65
+ query_pattern(pattern, &block) if block_given?
66
+ enum_for(:query_pattern, pattern)
67
+ end
68
+ after_query(pattern) if block_given? && respond_to?(:after_query)
69
+ enum.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable)
70
+ def enum.to_a
71
+ super.extend(RDF::Queryable, RDF::Enumerable, RDF::Countable)
54
72
  end
73
+ enum
55
74
  end
56
75
  end
57
76
 
@@ -77,7 +96,7 @@ module RDF
77
96
  # query execution by breaking down the query into its constituent
78
97
  # triple patterns and invoking `RDF::Query::Pattern#execute` on each
79
98
  # pattern.
80
- query.execute(self, &block)
99
+ query.execute(self).each(&block)
81
100
  end
82
101
  protected :query_execute
83
102
 
@@ -117,7 +136,7 @@ module RDF
117
136
  # @return [RDF::Statement]
118
137
  #
119
138
  # @overload first(pattern)
120
- # @param [Query, Statement, Array(Value), Hash] pattern
139
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
121
140
  # @return [RDF::Statement]
122
141
  #
123
142
  # @return [RDF::Statement]
@@ -143,7 +162,7 @@ module RDF
143
162
  # @return [RDF::Resource]
144
163
  #
145
164
  # @overload first_subject(pattern)
146
- # @param [Query, Statement, Array(Value), Hash] pattern
165
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
147
166
  # @return [RDF::Resource]
148
167
  #
149
168
  # @return [RDF::Resource]
@@ -165,7 +184,7 @@ module RDF
165
184
  # @return [RDF::URI]
166
185
  #
167
186
  # @overload first_predicate(pattern)
168
- # @param [Query, Statement, Array(Value), Hash] pattern
187
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
169
188
  # @return [RDF::URI]
170
189
  #
171
190
  # @return [RDF::URI]
@@ -184,13 +203,13 @@ module RDF
184
203
  # Returns `nil` if no statements match `pattern`.
185
204
  #
186
205
  # @overload first_object
187
- # @return [RDF::Value]
206
+ # @return [RDF::Term]
188
207
  #
189
208
  # @overload first_object(pattern)
190
- # @param [Query, Statement, Array(Value), Hash] pattern
191
- # @return [RDF::Value]
209
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
210
+ # @return [RDF::Term]
192
211
  #
193
- # @return [RDF::Value]
212
+ # @return [RDF::Term]
194
213
  # @since 0.1.9
195
214
  def first_object(pattern = nil)
196
215
  __send__(*(pattern ? [:query, pattern] : [:each])) do |statement|
@@ -210,7 +229,7 @@ module RDF
210
229
  # @return [RDF::Literal]
211
230
  #
212
231
  # @overload first_literal(pattern)
213
- # @param [Query, Statement, Array(Value), Hash] pattern
232
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
214
233
  # @return [RDF::Literal]
215
234
  #
216
235
  # @return [RDF::Literal]
@@ -233,7 +252,7 @@ module RDF
233
252
  # @return [Object]
234
253
  #
235
254
  # @overload first_value(pattern)
236
- # @param [Query, Statement, Array(Value), Hash] pattern
255
+ # @param [RDF::Query, RDF::Statement, Array(RDF::Term), Hash] pattern
237
256
  # @return [Object]
238
257
  #
239
258
  # @return [Object]
@@ -241,5 +260,5 @@ module RDF
241
260
  def first_value(pattern = nil)
242
261
  (literal = first_literal(pattern)) ? literal.value : nil
243
262
  end
244
- end
245
- end
263
+ end # Queryable
264
+ end # RDF
@@ -23,7 +23,7 @@ module RDF
23
23
  # RDF::List["foo", "bar"]
24
24
  # RDF::List["a", 1, "b", 2, "c", 3]
25
25
  #
26
- # @param [Array<RDF::Value>] values
26
+ # @param [Array<RDF::Term>] values
27
27
  # @return [RDF::List]
28
28
  def self.[](*values)
29
29
  self.new(nil, nil, values)
@@ -34,7 +34,7 @@ module RDF
34
34
  #
35
35
  # @param [RDF::Resource] subject
36
36
  # @param [RDF::Graph] graph
37
- # @param [Array<RDF::Value>] values
37
+ # @param [Array<RDF::Term>] values
38
38
  # @yield [list]
39
39
  # @yieldparam [RDF::List] list
40
40
  def initialize(subject = nil, graph = nil, values = nil, &block)
@@ -171,7 +171,7 @@ module RDF
171
171
  # RDF::List[1, 2, 3][0] #=> 1
172
172
  #
173
173
  # @param [Integer] index
174
- # @return [RDF::Value]
174
+ # @return [RDF::Term]
175
175
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000417
176
176
  def [](index)
177
177
  at(index)
@@ -183,14 +183,14 @@ module RDF
183
183
  # @example
184
184
  # RDF::List[] << 1 << 2 << 3 #=> RDF::List[1, 2, 3]
185
185
  #
186
- # @param [RDF::Value] value
186
+ # @param [RDF::Term] value
187
187
  # @return [RDF::List]
188
188
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000424
189
189
  def <<(value)
190
190
  value = case value
191
- when nil then RDF.nil
192
- when RDF::Value then value
193
- when Array then RDF::List.new(nil, graph, value)
191
+ when nil then RDF.nil
192
+ when RDF::Value then value
193
+ when Array then RDF::List.new(nil, graph, value)
194
194
  else value
195
195
  end
196
196
 
@@ -260,7 +260,7 @@ module RDF
260
260
  # RDF::List['a', 'b', 'c'].index('a') #=> 0
261
261
  # RDF::List['a', 'b', 'c'].index('d') #=> nil
262
262
  #
263
- # @param [RDF::Value] value
263
+ # @param [RDF::Term] value
264
264
  # @return [Integer]
265
265
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000436
266
266
  def index(value)
@@ -276,7 +276,7 @@ module RDF
276
276
  # @example
277
277
  # RDF::List[1, 2, 3].at(0) #=> 1
278
278
  #
279
- # @return [RDF::Value]
279
+ # @return [RDF::Term]
280
280
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000462
281
281
  def slice(*args)
282
282
  case argc = args.size
@@ -311,7 +311,7 @@ module RDF
311
311
  # RDF::List[1, 2, 3].fetch(4, nil) #=> nil
312
312
  # RDF::List[1, 2, 3].fetch(4) { |n| n*n } #=> 16
313
313
  #
314
- # @return [RDF::Value]
314
+ # @return [RDF::Term]
315
315
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000420
316
316
  def fetch(index, default = UNSET, &block)
317
317
  each.with_index do |v, i|
@@ -331,7 +331,7 @@ module RDF
331
331
  # @example
332
332
  # RDF::List[1, 2, 3].at(0) #=> 1
333
333
  #
334
- # @return [RDF::Value]
334
+ # @return [RDF::Term]
335
335
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000419
336
336
  def at(index)
337
337
  each.with_index do |v, i|
@@ -348,7 +348,7 @@ module RDF
348
348
  # @example
349
349
  # RDF::List[*(1..10)].first #=> 1
350
350
  #
351
- # @return [RDF::Value]
351
+ # @return [RDF::Term]
352
352
  def first
353
353
  graph.first_object(:subject => first_subject, :predicate => RDF.first)
354
354
  end
@@ -359,7 +359,7 @@ module RDF
359
359
  # @example
360
360
  # RDF::List[*(1..10)].second #=> 2
361
361
  #
362
- # @return [RDF::Value]
362
+ # @return [RDF::Term]
363
363
  def second
364
364
  at(1)
365
365
  end
@@ -370,7 +370,7 @@ module RDF
370
370
  # @example
371
371
  # RDF::List[*(1..10)].third #=> 3
372
372
  #
373
- # @return [RDF::Value]
373
+ # @return [RDF::Term]
374
374
  def third
375
375
  at(2)
376
376
  end
@@ -381,7 +381,7 @@ module RDF
381
381
  # @example
382
382
  # RDF::List[*(1..10)].fourth #=> 4
383
383
  #
384
- # @return [RDF::Value]
384
+ # @return [RDF::Term]
385
385
  def fourth
386
386
  at(3)
387
387
  end
@@ -392,7 +392,7 @@ module RDF
392
392
  # @example
393
393
  # RDF::List[*(1..10)].fifth #=> 5
394
394
  #
395
- # @return [RDF::Value]
395
+ # @return [RDF::Term]
396
396
  def fifth
397
397
  at(4)
398
398
  end
@@ -403,7 +403,7 @@ module RDF
403
403
  # @example
404
404
  # RDF::List[*(1..10)].sixth #=> 6
405
405
  #
406
- # @return [RDF::Value]
406
+ # @return [RDF::Term]
407
407
  def sixth
408
408
  at(5)
409
409
  end
@@ -414,7 +414,7 @@ module RDF
414
414
  # @example
415
415
  # RDF::List[*(1..10)].seventh #=> 7
416
416
  #
417
- # @return [RDF::Value]
417
+ # @return [RDF::Term]
418
418
  def seventh
419
419
  at(6)
420
420
  end
@@ -425,7 +425,7 @@ module RDF
425
425
  # @example
426
426
  # RDF::List[*(1..10)].eighth #=> 8
427
427
  #
428
- # @return [RDF::Value]
428
+ # @return [RDF::Term]
429
429
  def eighth
430
430
  at(7)
431
431
  end
@@ -436,7 +436,7 @@ module RDF
436
436
  # @example
437
437
  # RDF::List[*(1..10)].ninth #=> 9
438
438
  #
439
- # @return [RDF::Value]
439
+ # @return [RDF::Term]
440
440
  def ninth
441
441
  at(8)
442
442
  end
@@ -447,7 +447,7 @@ module RDF
447
447
  # @example
448
448
  # RDF::List[*(1..10)].tenth #=> 10
449
449
  #
450
- # @return [RDF::Value]
450
+ # @return [RDF::Term]
451
451
  def tenth
452
452
  at(9)
453
453
  end
@@ -458,7 +458,7 @@ module RDF
458
458
  # @example
459
459
  # RDF::List[1, 2, 3].last #=> 3
460
460
  #
461
- # @return [RDF::Value]
461
+ # @return [RDF::Term]
462
462
  # @see http://ruby-doc.org/core-1.9/classes/Array.html#M000422
463
463
  def last
464
464
  graph.first_object(:subject => last_subject, :predicate => RDF.first)
@@ -59,7 +59,7 @@ module RDF; class Literal
59
59
  # @return [Boolean] `true` or `false`
60
60
  # @since 0.3.0
61
61
  def ==(other)
62
- (self <=> other).zero?
62
+ (cmp = (self <=> other)) ? cmp.zero? : false
63
63
  end
64
64
  alias_method :===, :==
65
65
 
@@ -95,5 +95,17 @@ module RDF; class Literal
95
95
  def false?
96
96
  @object.equal?(false)
97
97
  end
98
+
99
+ ##
100
+ # Returns a developer-friendly representation of `self`.
101
+ #
102
+ # @return [String]
103
+ def inspect
104
+ case
105
+ when self.equal?(RDF::Literal::TRUE) then 'RDF::Literal::TRUE'
106
+ when self.equal?(RDF::Literal::FALSE) then 'RDF::Literal::FALSE'
107
+ else super
108
+ end
109
+ end
98
110
  end # Boolean
99
111
  end; end # RDF::Literal
@@ -14,6 +14,8 @@ module RDF; class Literal
14
14
  DATATYPE = XSD.decimal
15
15
  GRAMMAR = /^[\+\-]?\d+(\.\d*)?$/.freeze
16
16
 
17
+ include RDF::Literal::Numeric
18
+
17
19
  ##
18
20
  # @param [BigDecimal] value
19
21
  # @option options [String] :lexical (nil)
@@ -43,6 +45,7 @@ module RDF; class Literal
43
45
  f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point
44
46
  "#{i}.#{f}"
45
47
  end
48
+ @object = BigDecimal(@string) unless @object.nil?
46
49
  self
47
50
  end
48
51
 
@@ -54,7 +57,7 @@ module RDF; class Literal
54
57
  # @since 0.3.0
55
58
  def <=>(other)
56
59
  case other
57
- when Numeric
60
+ when ::Numeric
58
61
  to_d <=> other
59
62
  when RDF::Literal::Decimal, RDF::Literal::Double
60
63
  to_d <=> other.to_d
@@ -69,7 +72,7 @@ module RDF; class Literal
69
72
  # @return [Boolean] `true` or `false`
70
73
  # @since 0.3.0
71
74
  def ==(other)
72
- (self <=> other).zero?
75
+ (cmp = (self <=> other)) ? cmp.zero? : false
73
76
  end
74
77
  alias_method :===, :==
75
78
 
@@ -14,6 +14,8 @@ module RDF; class Literal
14
14
  DATATYPE = XSD.double
15
15
  GRAMMAR = /^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/.freeze # FIXME: support 'INF', '-INF' and 'NaN'
16
16
 
17
+ include RDF::Literal::Numeric
18
+
17
19
  ##
18
20
  # @param [Float, #to_f] value
19
21
  # @option options [String] :lexical (nil)
@@ -22,10 +24,15 @@ module RDF; class Literal
22
24
  @string = options[:lexical] if options.has_key?(:lexical)
23
25
  @string = value if !defined?(@string) && value.is_a?(String)
24
26
  @object = case
25
- when value.is_a?(::String) then Float(value) rescue nil
27
+ when value.is_a?(::String) then case value
28
+ when 'INF' then 1/0.0
29
+ when '-INF' then -1/0.0
30
+ when 'NaN' then 0/0.0
31
+ else Float(value) rescue nil
32
+ end
26
33
  when value.is_a?(::Float) then value
27
34
  when value.respond_to?(:to_f) then value.to_f
28
- else Float(value.to_s) rescue nil
35
+ else Float(value.to_s) rescue nil # FIXME
29
36
  end
30
37
  end
31
38
 
@@ -47,7 +54,8 @@ module RDF; class Literal
47
54
  f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point
48
55
  e.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes
49
56
  "#{i}.#{f}E#{e}"
50
- end unless @object.nil?
57
+ end
58
+ @object = Float(@string) unless @object.nil?
51
59
  self
52
60
  end
53
61
 
@@ -59,7 +67,7 @@ module RDF; class Literal
59
67
  # @since 0.3.0
60
68
  def <=>(other)
61
69
  case other
62
- when Numeric
70
+ when ::Numeric
63
71
  to_d <=> other
64
72
  when RDF::Literal::Decimal, RDF::Literal::Double
65
73
  to_d <=> other.to_d
@@ -74,7 +82,7 @@ module RDF; class Literal
74
82
  # @return [Boolean] `true` or `false`
75
83
  # @since 0.3.0
76
84
  def ==(other)
77
- (self <=> other).zero?
85
+ (cmp = (self <=> other)) ? cmp.zero? : false
78
86
  end
79
87
  alias_method :===, :==
80
88
 
@@ -194,7 +202,7 @@ module RDF; class Literal
194
202
  # @return [RDF::Literal]
195
203
  # @since 0.2.3
196
204
  def -@
197
- RDF::Literal(-to_f) # unary minus
205
+ RDF::Literal(-to_f, :datatype => datatype) # unary minus
198
206
  end
199
207
 
200
208
  ##
@@ -14,6 +14,8 @@ module RDF; class Literal
14
14
  DATATYPE = XSD.integer
15
15
  GRAMMAR = /^[\+\-]?\d+$/.freeze
16
16
 
17
+ include RDF::Literal::Numeric
18
+
17
19
  ##
18
20
  # @param [Integer, #to_i] value
19
21
  # @option options [String] :lexical (nil)
@@ -0,0 +1,9 @@
1
+ module RDF; class Literal
2
+ ##
3
+ # Shared methods for numeric literal classes.
4
+ #
5
+ # @since 0.3.0
6
+ module Numeric
7
+ # TODO
8
+ end # Numeric
9
+ end; end # RDF::Literal