rdf 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,6 +46,7 @@ module RDF
46
46
  autoload :Date, 'rdf/model/literal/date'
47
47
  autoload :DateTime, 'rdf/model/literal/datetime'
48
48
  autoload :Time, 'rdf/model/literal/time'
49
+ autoload :Token, 'rdf/model/literal/token'
49
50
  autoload :XML, 'rdf/model/literal/xml'
50
51
 
51
52
  include RDF::Value
@@ -78,6 +79,8 @@ module RDF
78
79
  RDF::Literal::Integer
79
80
  when XSD.unsignedLong, XSD.unsignedInt, XSD.unsignedShort, XSD.unsignedByte
80
81
  RDF::Literal::Integer
82
+ when XSD.token, XSD.language
83
+ RDF::Literal::Token
81
84
  when RDF.XMLLiteral
82
85
  RDF::Literal::XML
83
86
  else self
@@ -90,7 +93,8 @@ module RDF
90
93
  when ::BigDecimal then RDF::Literal::Decimal
91
94
  when ::DateTime then RDF::Literal::DateTime
92
95
  when ::Date then RDF::Literal::Date
93
- when ::Time then RDF::Literal::Time
96
+ when ::Time then RDF::Literal::Time # FIXME: Ruby's Time class can represent datetimes as well
97
+ when ::Symbol then RDF::Literal::Token
94
98
  else self
95
99
  end
96
100
  end
@@ -171,6 +175,14 @@ module RDF
171
175
  false
172
176
  end
173
177
 
178
+ ##
179
+ # Returns a hash code for this literal.
180
+ #
181
+ # @return [Fixnum]
182
+ def hash
183
+ to_s.hash
184
+ end
185
+
174
186
  ##
175
187
  # @return [Boolean]
176
188
  def eql?(other)
@@ -2,6 +2,12 @@ module RDF; class Literal
2
2
  ##
3
3
  # A decimal literal.
4
4
  #
5
+ # @example Arithmetic with decimal literals
6
+ # RDF::Literal(BigDecimal('1.0')) + 0.5 #=> RDF::Literal(BigDecimal('1.5'))
7
+ # RDF::Literal(BigDecimal('1.0')) - 0.5 #=> RDF::Literal(BigDecimal('0.5'))
8
+ # RDF::Literal(BigDecimal('1.0')) * 0.5 #=> RDF::Literal(BigDecimal('0.5'))
9
+ # RDF::Literal(BigDecimal('1.0')) / 0.5 #=> RDF::Literal(BigDecimal('2.0'))
10
+ #
5
11
  # @see http://www.w3.org/TR/xmlschema-2/#decimal
6
12
  # @since 0.2.1
7
13
  class Decimal < Literal
@@ -40,6 +46,91 @@ module RDF; class Literal
40
46
  self
41
47
  end
42
48
 
49
+ ##
50
+ # Returns the absolute value of `self`.
51
+ #
52
+ # @return [RDF::Literal]
53
+ # @since 0.2.3
54
+ def abs
55
+ (d = to_d) && d > 0 ? self : RDF::Literal(d.abs)
56
+ end
57
+
58
+ ##
59
+ # Returns `true` if the value is zero.
60
+ #
61
+ # @return [Boolean]
62
+ # @since 0.2.3
63
+ def zero?
64
+ to_d.zero?
65
+ end
66
+
67
+ ##
68
+ # Returns `self` if the value is not zero, `nil` otherwise.
69
+ #
70
+ # @return [Boolean]
71
+ # @since 0.2.3
72
+ def nonzero?
73
+ to_d.nonzero? ? self : nil
74
+ end
75
+
76
+ ##
77
+ # Returns `self`.
78
+ #
79
+ # @return [RDF::Literal]
80
+ # @since 0.2.3
81
+ def +@
82
+ self # unary plus
83
+ end
84
+
85
+ ##
86
+ # Returns `self` negated.
87
+ #
88
+ # @return [RDF::Literal]
89
+ # @since 0.2.3
90
+ def -@
91
+ RDF::Literal(-to_d) # unary minus
92
+ end
93
+
94
+ ##
95
+ # Returns the sum of `self` plus `other`.
96
+ #
97
+ # @param [#to_d] other
98
+ # @return [RDF::Literal]
99
+ # @since 0.2.3
100
+ def +(other)
101
+ RDF::Literal(to_d + (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
102
+ end
103
+
104
+ ##
105
+ # Returns the difference of `self` minus `other`.
106
+ #
107
+ # @param [#to_d] other
108
+ # @return [RDF::Literal]
109
+ # @since 0.2.3
110
+ def -(other)
111
+ RDF::Literal(to_d - (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
112
+ end
113
+
114
+ ##
115
+ # Returns the product of `self` times `other`.
116
+ #
117
+ # @param [#to_d] other
118
+ # @return [RDF::Literal]
119
+ # @since 0.2.3
120
+ def *(other)
121
+ RDF::Literal(to_d * (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s)))
122
+ end
123
+
124
+ ##
125
+ # Returns the quotient of `self` divided by `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
+
43
134
  ##
44
135
  # Returns the value as a string.
45
136
  #
@@ -2,6 +2,12 @@ module RDF; class Literal
2
2
  ##
3
3
  # An floating point number literal.
4
4
  #
5
+ # @example Arithmetic with floating point literals
6
+ # RDF::Literal(1.0) + 0.5 #=> RDF::Literal(1.5)
7
+ # RDF::Literal(3.0) - 6 #=> RDF::Literal(-3.0)
8
+ # RDF::Literal(Math::PI) * 2 #=> RDF::Literal(Math::PI * 2)
9
+ # RDF::Literal(Math::PI) / 2 #=> RDF::Literal(Math::PI / 2)
10
+ #
5
11
  # @see http://www.w3.org/TR/xmlschema-2/#double
6
12
  # @since 0.2.1
7
13
  class Double < Literal
@@ -45,6 +51,165 @@ module RDF; class Literal
45
51
  self
46
52
  end
47
53
 
54
+ ##
55
+ # Returns `true` if the value is an invalid IEEE floating point number.
56
+ #
57
+ # @example
58
+ # RDF::Literal(-1.0).nan? #=> false
59
+ # RDF::Literal(1.0/0.0).nan? #=> false
60
+ # RDF::Literal(0.0/0.0).nan? #=> true
61
+ #
62
+ # @return [Boolean]
63
+ # @since 0.2.3
64
+ def nan?
65
+ to_f.nan?
66
+ end
67
+
68
+ ##
69
+ # Returns `true` if the value is a valid IEEE floating point number (it
70
+ # is not infinite, and `nan?` is `false`).
71
+ #
72
+ # @example
73
+ # RDF::Literal(-1.0).finite? #=> true
74
+ # RDF::Literal(1.0/0.0).finite? #=> false
75
+ # RDF::Literal(0.0/0.0).finite? #=> false
76
+ #
77
+ # @return [Boolean]
78
+ # @since 0.2.3
79
+ def finite?
80
+ to_f.finite?
81
+ end
82
+
83
+ ##
84
+ # Returns `nil`, `-1`, or `+1` depending on whether the value is finite,
85
+ # `-INF`, or `+INF`.
86
+ #
87
+ # @example
88
+ # RDF::Literal(0.0/0.0).infinite? #=> nil
89
+ # RDF::Literal(-1.0/0.0).infinite? #=> -1
90
+ # RDF::Literal(+1.0/0.0).infinite? #=> 1
91
+ #
92
+ # @return [Integer]
93
+ # @since 0.2.3
94
+ def infinite?
95
+ to_f.infinite?
96
+ end
97
+
98
+ ##
99
+ # Returns the smallest integer greater than or equal to `self`.
100
+ #
101
+ # @example
102
+ # RDF::Literal(1.2).ceil #=> RDF::Literal(2)
103
+ # RDF::Literal(-1.2).ceil #=> RDF::Literal(-1)
104
+ # RDF::Literal(2.0).ceil #=> RDF::Literal(2)
105
+ # RDF::Literal(-2.0).ceil #=> RDF::Literal(-2)
106
+ #
107
+ # @return [RDF::Literal]
108
+ # @since 0.2.3
109
+ def ceil
110
+ RDF::Literal(to_f.ceil)
111
+ end
112
+
113
+ ##
114
+ # Returns the largest integer less than or equal to `self`.
115
+ #
116
+ # @example
117
+ # RDF::Literal(1.2).floor #=> RDF::Literal(1)
118
+ # RDF::Literal(-1.2).floor #=> RDF::Literal(-2)
119
+ # RDF::Literal(2.0).floor #=> RDF::Literal(2)
120
+ # RDF::Literal(-2.0).floor #=> RDF::Literal(-2)
121
+ #
122
+ # @return [RDF::Literal]
123
+ # @since 0.2.3
124
+ def floor
125
+ RDF::Literal(to_f.floor)
126
+ end
127
+
128
+ ##
129
+ # Returns the absolute value of `self`.
130
+ #
131
+ # @return [RDF::Literal]
132
+ # @since 0.2.3
133
+ def abs
134
+ (f = to_f) && f > 0 ? self : RDF::Literal(f.abs)
135
+ end
136
+
137
+ ##
138
+ # Returns `true` if the value is zero.
139
+ #
140
+ # @return [Boolean]
141
+ # @since 0.2.3
142
+ def zero?
143
+ to_f.zero?
144
+ end
145
+
146
+ ##
147
+ # Returns `self` if the value is not zero, `nil` otherwise.
148
+ #
149
+ # @return [Boolean]
150
+ # @since 0.2.3
151
+ def nonzero?
152
+ to_f.nonzero? ? self : nil
153
+ end
154
+
155
+ ##
156
+ # Returns `self`.
157
+ #
158
+ # @return [RDF::Literal]
159
+ # @since 0.2.3
160
+ def +@
161
+ self # unary plus
162
+ end
163
+
164
+ ##
165
+ # Returns `self` negated.
166
+ #
167
+ # @return [RDF::Literal]
168
+ # @since 0.2.3
169
+ def -@
170
+ RDF::Literal(-to_f) # unary minus
171
+ end
172
+
173
+ ##
174
+ # Returns the sum of `self` plus `other`.
175
+ #
176
+ # @param [#to_f] other
177
+ # @return [RDF::Literal]
178
+ # @since 0.2.3
179
+ def +(other)
180
+ RDF::Literal(to_f + other.to_f)
181
+ end
182
+
183
+ ##
184
+ # Returns the difference of `self` minus `other`.
185
+ #
186
+ # @param [#to_f] other
187
+ # @return [RDF::Literal]
188
+ # @since 0.2.3
189
+ def -(other)
190
+ RDF::Literal(to_f - other.to_f)
191
+ end
192
+
193
+ ##
194
+ # Returns the product of `self` times `other`.
195
+ #
196
+ # @param [#to_f] other
197
+ # @return [RDF::Literal]
198
+ # @since 0.2.3
199
+ def *(other)
200
+ RDF::Literal(to_f * other.to_f)
201
+ end
202
+
203
+ ##
204
+ # Returns the quotient of `self` divided by `other`.
205
+ #
206
+ # @param [#to_f] other
207
+ # @return [RDF::Literal]
208
+ # @since 0.2.3
209
+ def /(other)
210
+ RDF::Literal(to_f / other.to_f)
211
+ end
212
+
48
213
  ##
49
214
  # Returns the value as a string.
50
215
  #
@@ -2,6 +2,12 @@ module RDF; class Literal
2
2
  ##
3
3
  # An integer literal.
4
4
  #
5
+ # @example Arithmetic with integer literals
6
+ # RDF::Literal(40) + 2 #=> RDF::Literal(42)
7
+ # RDF::Literal(45) - 3 #=> RDF::Literal(42)
8
+ # RDF::Literal(6) * 7 #=> RDF::Literal(42)
9
+ # RDF::Literal(84) / 2 #=> RDF::Literal(42)
10
+ #
5
11
  # @see http://www.w3.org/TR/xmlschema-2/#integer
6
12
  # @since 0.2.1
7
13
  class Integer < Decimal
@@ -33,6 +39,129 @@ module RDF; class Literal
33
39
  self
34
40
  end
35
41
 
42
+ ##
43
+ # Returns the successor value of `self`.
44
+ #
45
+ # @return [RDF::Literal]
46
+ # @since 0.2.3
47
+ def pred
48
+ RDF::Literal(to_i.pred)
49
+ end
50
+
51
+ ##
52
+ # Returns the predecessor value of `self`.
53
+ #
54
+ # @return [RDF::Literal]
55
+ # @since 0.2.3
56
+ def succ
57
+ RDF::Literal(to_i.succ)
58
+ end
59
+ alias_method :next, :succ
60
+
61
+ ##
62
+ # Returns `true` if the value is even.
63
+ #
64
+ # @return [Boolean]
65
+ # @since 0.2.3
66
+ def even?
67
+ to_i.even?
68
+ end
69
+
70
+ ##
71
+ # Returns `true` if the value is odd.
72
+ #
73
+ # @return [Boolean]
74
+ # @since 0.2.3
75
+ def odd?
76
+ to_i.odd?
77
+ end
78
+
79
+ ##
80
+ # Returns the absolute value of `self`.
81
+ #
82
+ # @return [RDF::Literal]
83
+ # @since 0.2.3
84
+ def abs
85
+ (n = to_i) && n > 0 ? self : RDF::Literal(n.abs)
86
+ end
87
+
88
+ ##
89
+ # Returns `true` if the value is zero.
90
+ #
91
+ # @return [Boolean]
92
+ # @since 0.2.3
93
+ def zero?
94
+ to_i.zero?
95
+ end
96
+
97
+ ##
98
+ # Returns `self` if the value is not zero, `nil` otherwise.
99
+ #
100
+ # @return [Boolean]
101
+ # @since 0.2.3
102
+ def nonzero?
103
+ to_i.nonzero? ? self : nil
104
+ end
105
+
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_i) # unary minus
122
+ end
123
+
124
+ ##
125
+ # Returns the sum of `self` plus `other`.
126
+ #
127
+ # @param [#to_i] other
128
+ # @return [RDF::Literal]
129
+ # @since 0.2.3
130
+ def +(other)
131
+ RDF::Literal(to_i + other.to_i)
132
+ end
133
+
134
+ ##
135
+ # Returns the difference of `self` minus `other`.
136
+ #
137
+ # @param [#to_i] other
138
+ # @return [RDF::Literal]
139
+ # @since 0.2.3
140
+ def -(other)
141
+ RDF::Literal(to_i - other.to_i)
142
+ end
143
+
144
+ ##
145
+ # Returns the product of `self` times `other`.
146
+ #
147
+ # @param [#to_i] other
148
+ # @return [RDF::Literal]
149
+ # @since 0.2.3
150
+ def *(other)
151
+ RDF::Literal(to_i * other.to_i)
152
+ end
153
+
154
+ ##
155
+ # Returns the quotient of `self` divided by `other`.
156
+ #
157
+ # @param [#to_i] other
158
+ # @return [RDF::Literal]
159
+ # @raise [ZeroDivisionError] if divided by zero
160
+ # @since 0.2.3
161
+ def /(other)
162
+ RDF::Literal(to_i / other.to_i)
163
+ end
164
+
36
165
  ##
37
166
  # Returns the value as a string.
38
167
  #
@@ -48,6 +177,8 @@ module RDF; class Literal
48
177
  def to_i
49
178
  @object.to_i
50
179
  end
180
+ alias_method :to_int, :to_i
181
+ alias_method :ord, :to_i
51
182
 
52
183
  ##
53
184
  # Returns the value as a floating point number.