rdf 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,9 +14,9 @@ module RDF; class Literal
14
14
  # @param [Boolean] value
15
15
  # @option options [String] :lexical (nil)
16
16
  def initialize(value, options = {})
17
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
17
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
18
18
  @string = options[:lexical] if options.has_key?(:lexical)
19
- @string = value if !defined?(@string) && value.is_a?(String)
19
+ @string ||= value if value.is_a?(String)
20
20
  @object = case
21
21
  when true.equal?(value) then true
22
22
  when false.equal?(value) then false
@@ -59,10 +59,20 @@ module RDF; class Literal
59
59
  # @return [Boolean] `true` or `false`
60
60
  # @since 0.3.0
61
61
  def ==(other)
62
- (cmp = (self <=> other)) ? cmp.zero? : false
63
- end
64
- alias_method :===, :==
62
+ # If lexically invalid, use regular literal testing
63
+ return super unless self.valid?
64
+
65
+ other = Literal::Boolean.new(other) if other.class == TrueClass || other.class == FalseClass
65
66
 
67
+ case other
68
+ when Literal::Boolean
69
+ return super unless other.valid?
70
+ (cmp = (self <=> other)) ? cmp.zero? : false
71
+ else
72
+ super
73
+ end
74
+ end
75
+
66
76
  ##
67
77
  # Returns the value as a string.
68
78
  #
@@ -7,19 +7,20 @@ module RDF; class Literal
7
7
  class Date < Literal
8
8
  DATATYPE = XSD.date
9
9
  GRAMMAR = %r(\A-?\d{4}-\d{2}-\d{2}(([\+\-]\d{2}:\d{2})|UTC|Z)?\Z).freeze
10
+ FORMAT = '%Y-%m-%d%Z'.freeze
10
11
 
11
12
  ##
12
13
  # @param [Date] value
13
14
  # @option options [String] :lexical (nil)
14
15
  def initialize(value, options = {})
15
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
16
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
16
17
  @string = options[:lexical] if options.has_key?(:lexical)
17
- @string = value if !defined?(@string) && value.is_a?(String)
18
+ @string ||= value if value.is_a?(String)
18
19
  @object = case
19
20
  when value.is_a?(::Date) then value
20
21
  when value.respond_to?(:to_date) then value.to_date # Ruby 1.9+
21
22
  else ::Date.parse(value.to_s)
22
- end
23
+ end rescue nil
23
24
  end
24
25
 
25
26
  ##
@@ -28,7 +29,7 @@ module RDF; class Literal
28
29
  # @return [RDF::Literal] `self`
29
30
  # @see http://www.w3.org/TR/xmlschema-2/#date
30
31
  def canonicalize!
31
- @string = @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
32
+ @string = @object.strftime(FORMAT).sub(/\+00:00|UTC/, 'Z')
32
33
  self
33
34
  end
34
35
 
@@ -41,7 +42,7 @@ module RDF; class Literal
41
42
  # @return [Boolean]
42
43
  # @since 0.2.1
43
44
  def valid?
44
- !!(value =~ GRAMMAR) && value !~ %r(\A0000)
45
+ super && value !~ %r(\A0000)
45
46
  end
46
47
 
47
48
  ##
@@ -49,7 +50,24 @@ module RDF; class Literal
49
50
  #
50
51
  # @return [String]
51
52
  def to_s
52
- @string || @object.strftime('%Y-%m-%d%Z').sub(/\+00:00|UTC/, 'Z')
53
+ @string || @object.strftime(FORMAT).sub(/\+00:00|UTC/, 'Z')
54
+ end
55
+
56
+ ##
57
+ # Equal compares as Date objects
58
+ def ==(other)
59
+ # If lexically invalid, use regular literal testing
60
+ return super unless self.valid?
61
+
62
+ case other
63
+ when Literal::Date
64
+ return super unless other.valid?
65
+ self.object == other.object
66
+ when Literal::Time, Literal::DateTime
67
+ false
68
+ else
69
+ super
70
+ end
53
71
  end
54
72
  end # Date
55
73
  end; end # RDF::Literal
@@ -12,14 +12,14 @@ module RDF; class Literal
12
12
  # @param [DateTime] value
13
13
  # @option options [String] :lexical (nil)
14
14
  def initialize(value, options = {})
15
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
15
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
16
16
  @string = options[:lexical] if options.has_key?(:lexical)
17
- @string = value if !defined?(@string) && value.is_a?(String)
17
+ @string ||= value if value.is_a?(String)
18
18
  @object = case
19
19
  when value.is_a?(::DateTime) then value
20
20
  when value.respond_to?(:to_datetime) then value.to_datetime # Ruby 1.9+
21
21
  else ::DateTime.parse(value.to_s)
22
- end
22
+ end rescue nil
23
23
  end
24
24
 
25
25
  ##
@@ -41,7 +41,7 @@ module RDF; class Literal
41
41
  # @return [Boolean]
42
42
  # @since 0.2.1
43
43
  def valid?
44
- !!(value =~ GRAMMAR) && value !~ %r(\A0000)
44
+ super && value !~ %r(\A0000)
45
45
  end
46
46
 
47
47
  ##
@@ -51,5 +51,22 @@ module RDF; class Literal
51
51
  def to_s
52
52
  @string || @object.strftime('%Y-%m-%dT%H:%M:%S%Z').sub(/\+00:00|UTC/, 'Z')
53
53
  end
54
+
55
+ ##
56
+ # Equal compares as DateTime objects
57
+ def ==(other)
58
+ # If lexically invalid, use regular literal testing
59
+ return super unless self.valid?
60
+
61
+ case other
62
+ when Literal::DateTime
63
+ return super unless other.valid?
64
+ self.object == other.object
65
+ when Literal::Time, Literal::Date
66
+ false
67
+ else
68
+ super
69
+ end
70
+ end
54
71
  end # DateTime
55
72
  end; end # RDF::Literal
@@ -10,19 +10,17 @@ module RDF; class Literal
10
10
  #
11
11
  # @see http://www.w3.org/TR/xmlschema-2/#decimal
12
12
  # @since 0.2.1
13
- class Decimal < Literal
13
+ class Decimal < Numeric
14
14
  DATATYPE = XSD.decimal
15
15
  GRAMMAR = /^[\+\-]?\d+(\.\d*)?$/.freeze
16
16
 
17
- include RDF::Literal::Numeric
18
-
19
17
  ##
20
18
  # @param [BigDecimal] value
21
19
  # @option options [String] :lexical (nil)
22
20
  def initialize(value, options = {})
23
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
21
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
24
22
  @string = options[:lexical] if options.has_key?(:lexical)
25
- @string = value if !defined?(@string) && value.is_a?(String)
23
+ @string ||= value if value.is_a?(String)
26
24
  @object = case
27
25
  when value.is_a?(BigDecimal) then value
28
26
  else BigDecimal(value.to_s)
@@ -49,33 +47,6 @@ module RDF; class Literal
49
47
  self
50
48
  end
51
49
 
52
- ##
53
- # Compares this literal to `other` for sorting purposes.
54
- #
55
- # @param [Object] other
56
- # @return [Integer] `-1`, `0`, or `1`
57
- # @since 0.3.0
58
- def <=>(other)
59
- case other
60
- when ::Numeric
61
- to_d <=> other
62
- when RDF::Literal::Decimal, RDF::Literal::Double
63
- to_d <=> other.to_d
64
- else super
65
- end
66
- end
67
-
68
- ##
69
- # Returns `true` if this literal is equivalent to `other`.
70
- #
71
- # @param [Object] other
72
- # @return [Boolean] `true` or `false`
73
- # @since 0.3.0
74
- def ==(other)
75
- (cmp = (self <=> other)) ? cmp.zero? : false
76
- end
77
- alias_method :===, :==
78
-
79
50
  ##
80
51
  # Returns the absolute value of `self`.
81
52
  #
@@ -103,64 +74,6 @@ module RDF; class Literal
103
74
  to_d.nonzero? ? self : nil
104
75
  end
105
76
 
106
- ##
107
- # Returns `self`.
108
- #
109
- # @return [RDF::Literal]
110
- # @since 0.2.3
111
- def +@
112
- self # unary plus
113
- end
114
-
115
- ##
116
- # Returns `self` negated.
117
- #
118
- # @return [RDF::Literal]
119
- # @since 0.2.3
120
- def -@
121
- RDF::Literal(-to_d) # unary minus
122
- end
123
-
124
- ##
125
- # Returns the sum of `self` plus `other`.
126
- #
127
- # @param [#to_d] other
128
- # @return [RDF::Literal]
129
- # @since 0.2.3
130
- def +(other)
131
- RDF::Literal(to_d + (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
132
- end
133
-
134
- ##
135
- # Returns the difference of `self` minus `other`.
136
- #
137
- # @param [#to_d] other
138
- # @return [RDF::Literal]
139
- # @since 0.2.3
140
- def -(other)
141
- RDF::Literal(to_d - (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
142
- end
143
-
144
- ##
145
- # Returns the product of `self` times `other`.
146
- #
147
- # @param [#to_d] other
148
- # @return [RDF::Literal]
149
- # @since 0.2.3
150
- def *(other)
151
- RDF::Literal(to_d * (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
152
- end
153
-
154
- ##
155
- # Returns the quotient of `self` divided by `other`.
156
- #
157
- # @param [#to_d] other
158
- # @return [RDF::Literal]
159
- # @since 0.2.3
160
- def /(other)
161
- RDF::Literal(to_d / (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
162
- end
163
-
164
77
  ##
165
78
  # Returns the value as a string.
166
79
  #
@@ -169,43 +82,5 @@ module RDF; class Literal
169
82
  def to_s
170
83
  @string || @object.to_s('F')
171
84
  end
172
-
173
- ##
174
- # Returns the value as an integer.
175
- #
176
- # @return [Integer]
177
- # @see BigDecimal#to_i
178
- def to_i
179
- @object.to_i
180
- end
181
-
182
- ##
183
- # Returns the value as a floating point number.
184
- #
185
- # The usual accuracy limits and errors of binary float arithmetic apply.
186
- #
187
- # @return [Float]
188
- # @see BigDecimal#to_f
189
- def to_f
190
- @object.to_f
191
- end
192
-
193
- ##
194
- # Returns the value as a decimal number.
195
- #
196
- # @return [BigDecimal]
197
- # @see BigDecimal#to_d
198
- def to_d
199
- @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
200
- end
201
-
202
- ##
203
- # Returns the value as a rational number.
204
- #
205
- # @return [Rational]
206
- # @see BigDecimal#to_r
207
- def to_r
208
- @object.to_r # only available on Ruby 1.9+
209
- end
210
85
  end # Decimal
211
86
  end; end # RDF::Literal
@@ -10,19 +10,17 @@ module RDF; class Literal
10
10
  #
11
11
  # @see http://www.w3.org/TR/xmlschema-2/#double
12
12
  # @since 0.2.1
13
- class Double < Literal
13
+ class Double < Numeric
14
14
  DATATYPE = XSD.double
15
- GRAMMAR = /^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/.freeze # FIXME: support 'INF', '-INF' and 'NaN'
16
-
17
- include RDF::Literal::Numeric
15
+ GRAMMAR = /^NaN|(?:[\+\-]?(?:INF|(?:\d+(\.\d*)?([eE][\+\-]?\d+)?)))$/.freeze
18
16
 
19
17
  ##
20
18
  # @param [Float, #to_f] value
21
19
  # @option options [String] :lexical (nil)
22
20
  def initialize(value, options = {})
23
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
21
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
24
22
  @string = options[:lexical] if options.has_key?(:lexical)
25
- @string = value if !defined?(@string) && value.is_a?(String)
23
+ @string ||= value if value.is_a?(String)
26
24
  @object = case
27
25
  when value.is_a?(::String) then case value
28
26
  when 'INF' then 1/0.0
@@ -75,17 +73,6 @@ module RDF; class Literal
75
73
  end
76
74
  end
77
75
 
78
- ##
79
- # Returns `true` if this literal is equivalent to `other`.
80
- #
81
- # @param [Object] other
82
- # @return [Boolean] `true` or `false`
83
- # @since 0.3.0
84
- def ==(other)
85
- (cmp = (self <=> other)) ? cmp.zero? : false
86
- end
87
- alias_method :===, :==
88
-
89
76
  ##
90
77
  # Returns `true` if the value is an invalid IEEE floating point number.
91
78
  #
@@ -187,64 +174,6 @@ module RDF; class Literal
187
174
  to_f.nonzero? ? self : nil
188
175
  end
189
176
 
190
- ##
191
- # Returns `self`.
192
- #
193
- # @return [RDF::Literal]
194
- # @since 0.2.3
195
- def +@
196
- self # unary plus
197
- end
198
-
199
- ##
200
- # Returns `self` negated.
201
- #
202
- # @return [RDF::Literal]
203
- # @since 0.2.3
204
- def -@
205
- RDF::Literal(-to_f, :datatype => datatype) # unary minus
206
- end
207
-
208
- ##
209
- # Returns the sum of `self` plus `other`.
210
- #
211
- # @param [#to_f] other
212
- # @return [RDF::Literal]
213
- # @since 0.2.3
214
- def +(other)
215
- RDF::Literal(to_f + other.to_f)
216
- end
217
-
218
- ##
219
- # Returns the difference of `self` minus `other`.
220
- #
221
- # @param [#to_f] other
222
- # @return [RDF::Literal]
223
- # @since 0.2.3
224
- def -(other)
225
- RDF::Literal(to_f - other.to_f)
226
- end
227
-
228
- ##
229
- # Returns the product of `self` times `other`.
230
- #
231
- # @param [#to_f] other
232
- # @return [RDF::Literal]
233
- # @since 0.2.3
234
- def *(other)
235
- RDF::Literal(to_f * other.to_f)
236
- end
237
-
238
- ##
239
- # Returns the quotient of `self` divided by `other`.
240
- #
241
- # @param [#to_f] other
242
- # @return [RDF::Literal]
243
- # @since 0.2.3
244
- def /(other)
245
- RDF::Literal(to_f / other.to_f)
246
- end
247
-
248
177
  ##
249
178
  # Returns the value as a string.
250
179
  #
@@ -256,37 +185,5 @@ module RDF; class Literal
256
185
  else @object.to_s
257
186
  end
258
187
  end
259
-
260
- ##
261
- # Returns the value as an integer.
262
- #
263
- # @return [Integer]
264
- def to_i
265
- @object.to_i
266
- end
267
-
268
- ##
269
- # Returns the value as a floating point number.
270
- #
271
- # @return [Float]
272
- def to_f
273
- @object.to_f
274
- end
275
-
276
- ##
277
- # Returns the value as a decimal number.
278
- #
279
- # @return [BigDecimal]
280
- def to_d
281
- @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
282
- end
283
-
284
- ##
285
- # Returns the value as a rational number.
286
- #
287
- # @return [Rational]
288
- def to_r
289
- @object.to_r # only available on Ruby 1.9+
290
- end
291
188
  end # Double
292
189
  end; end # RDF::Literal