lib/rdf/model/literal/double.rb in rdf-0.3.3 vs lib/rdf/model/literal/double.rb in rdf-0.3.4
- old
+ new
@@ -8,23 +8,21 @@
# RDF::Literal(Math::PI) * 2 #=> RDF::Literal(Math::PI * 2)
# RDF::Literal(Math::PI) / 2 #=> RDF::Literal(Math::PI / 2)
#
# @see http://www.w3.org/TR/xmlschema-2/#double
# @since 0.2.1
- class Double < Literal
+ class Double < Numeric
DATATYPE = XSD.double
- GRAMMAR = /^[\+\-]?\d+(\.\d*([eE][\+\-]?\d+)?)?$/.freeze # FIXME: support 'INF', '-INF' and 'NaN'
+ GRAMMAR = /^NaN|(?:[\+\-]?(?:INF|(?:\d+(\.\d*)?([eE][\+\-]?\d+)?)))$/.freeze
- include RDF::Literal::Numeric
-
##
# @param [Float, #to_f] value
# @option options [String] :lexical (nil)
def initialize(value, options = {})
- @datatype = RDF::URI(options[:datatype] || DATATYPE)
+ @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
@string = options[:lexical] if options.has_key?(:lexical)
- @string = value if !defined?(@string) && value.is_a?(String)
+ @string ||= value if value.is_a?(String)
@object = case
when value.is_a?(::String) then case value
when 'INF' then 1/0.0
when '-INF' then -1/0.0
when 'NaN' then 0/0.0
@@ -74,21 +72,10 @@
else super
end
end
##
- # Returns `true` if this literal is equivalent to `other`.
- #
- # @param [Object] other
- # @return [Boolean] `true` or `false`
- # @since 0.3.0
- def ==(other)
- (cmp = (self <=> other)) ? cmp.zero? : false
- end
- alias_method :===, :==
-
- ##
# Returns `true` if the value is an invalid IEEE floating point number.
#
# @example
# RDF::Literal(-1.0).nan? #=> false
# RDF::Literal(1.0/0.0).nan? #=> false
@@ -186,107 +173,17 @@
def nonzero?
to_f.nonzero? ? self : nil
end
##
- # Returns `self`.
- #
- # @return [RDF::Literal]
- # @since 0.2.3
- def +@
- self # unary plus
- end
-
- ##
- # Returns `self` negated.
- #
- # @return [RDF::Literal]
- # @since 0.2.3
- def -@
- RDF::Literal(-to_f, :datatype => datatype) # unary minus
- end
-
- ##
- # Returns the sum of `self` plus `other`.
- #
- # @param [#to_f] other
- # @return [RDF::Literal]
- # @since 0.2.3
- def +(other)
- RDF::Literal(to_f + other.to_f)
- end
-
- ##
- # Returns the difference of `self` minus `other`.
- #
- # @param [#to_f] other
- # @return [RDF::Literal]
- # @since 0.2.3
- def -(other)
- RDF::Literal(to_f - other.to_f)
- end
-
- ##
- # Returns the product of `self` times `other`.
- #
- # @param [#to_f] other
- # @return [RDF::Literal]
- # @since 0.2.3
- def *(other)
- RDF::Literal(to_f * other.to_f)
- end
-
- ##
- # Returns the quotient of `self` divided by `other`.
- #
- # @param [#to_f] other
- # @return [RDF::Literal]
- # @since 0.2.3
- def /(other)
- RDF::Literal(to_f / other.to_f)
- end
-
- ##
# Returns the value as a string.
#
# @return [String]
def to_s
@string || case
when @object.nan? then 'NaN'
when @object.infinite? then @object.to_s[0...-'inity'.length].upcase
else @object.to_s
end
- end
-
- ##
- # Returns the value as an integer.
- #
- # @return [Integer]
- def to_i
- @object.to_i
- end
-
- ##
- # Returns the value as a floating point number.
- #
- # @return [Float]
- def to_f
- @object.to_f
- end
-
- ##
- # Returns the value as a decimal number.
- #
- # @return [BigDecimal]
- def to_d
- @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
- end
-
- ##
- # Returns the value as a rational number.
- #
- # @return [Rational]
- def to_r
- @object.to_r # only available on Ruby 1.9+
end
end # Double
end; end # RDF::Literal