lib/rdf/model/literal/integer.rb
module RDF; class Literal
##
# An integer literal.
#
# @example Arithmetic with integer literals
# RDF::Literal(40) + 2 #=> RDF::Literal(42)
# RDF::Literal(45) - 3 #=> RDF::Literal(42)
# RDF::Literal(6) * 7 #=> RDF::Literal(42)
# RDF::Literal(84) / 2 #=> RDF::Literal(42)
#
# @see http://www.w3.org/TR/xmlschema11-2/#integer
# @see http://www.w3.org/TR/2004/REC-xmlschema-2-20041028/#integer
# @since 0.2.1
class Integer < Decimal
DATATYPE = RDF::URI("http://www.w3.org/2001/XMLSchema#integer")
GRAMMAR = /^[\+\-]?\d+$/.freeze
##
# @param [String, Integer, #to_i] value
# @param (see Literal#initialize)
def initialize(value, datatype: nil, lexical: nil, **options)
@datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
@string = lexical || (value if value.is_a?(String))
@object = case
when value.is_a?(::Integer) then value
when value.respond_to?(:to_i) then value.to_i
else 0
end
end
##
# Converts this literal into its canonical lexical representation.
#
# @return [RDF::Literal] `self`
# @see http://www.w3.org/TR/xmlschema11-2/#integer
def canonicalize!
@string = @object.to_s if @object
self
end
##
# Returns the predecessor value of `self`.
#
# @return [RDF::Literal]
# @since 0.2.3
def pred
RDF::Literal(to_i.pred)
end
##
# Returns the successor value of `self`.
#
# @return [RDF::Literal]
# @since 0.2.3
def succ
RDF::Literal(to_i.succ)
end
alias_method :next, :succ
##
# Returns `true` if the value is even.
#
# @return [Boolean]
# @since 0.2.3
def even?
to_i.even?
end
##
# Returns `true` if the value is odd.
#
# @return [Boolean]
# @since 0.2.3
def odd?
to_i.odd?
end
##
# Returns the absolute value of `self`.
#
# From the XQuery function [fn:abs](https://www.w3.org/TR/xpath-functions/#func-abs).
#
# @return [RDF::Literal]
# @see https://www.w3.org/TR/xpath-functions/#func-abs
# @since 0.2.3
def abs
(n = to_i) && n > 0 ? self : self.class.new(n.abs)
end
##
# Returns `self`.
#
# From the XQuery function [fn:round](https://www.w3.org/TR/xpath-functions/#func-round).
#
# @return [RDF::Literal]
# @see https://www.w3.org/TR/xpath-functions/#func-round
def round
self
end
##
# Returns `true` if the value is zero.
#
# @return [Boolean]
# @since 0.2.3
def zero?
to_i.zero?
end
##
# Returns `self` if the value is not zero, `nil` otherwise.
#
# @return [Boolean]
# @since 0.2.3
def nonzero?
to_i.nonzero? ? self : nil
end
##
# Returns the value as a string.
#
# @return [String]
def to_s
@string || @object.to_s
end
##
# Returns the value as an `OpenSSL::BN` instance.
#
# @return [OpenSSL::BN]
# @see http://ruby-doc.org/stdlib/libdoc/openssl/rdoc/classes/OpenSSL/BN.html
# @since 0.2.4
def to_bn
require 'openssl' unless defined?(OpenSSL::BN)
OpenSSL::BN.new(to_s)
end
end # Integer
end; end # RDF::Literal