class RDF::Literal::Integer
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 www.w3.org/TR/xmlschema11-2/#integer @see www.w3.org/TR/2004/REC-xmlschema-2-20041028/#integer @since 0.2.1
Constants
- DATATYPE
- GRAMMAR
Public Class Methods
@param [String, Integer
, to_i] value @param (see Literal#initialize)
# File lib/rdf/model/literal/integer.rb, line 21 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
Public Instance Methods
Returns the absolute value of ‘self`.
From the XQuery function [fn:abs](www.w3.org/TR/xpath-functions/#func-abs).
@return [RDF::Literal] @see www.w3.org/TR/xpath-functions/#func-abs @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 86 def abs (n = to_i) && n > 0 ? self : self.class.new(n.abs) end
Converts this literal into its canonical lexical representation.
@return [RDF::Literal] ‘self` @see www.w3.org/TR/xmlschema11-2/#integer
# File lib/rdf/model/literal/integer.rb, line 36 def canonicalize! @string = @object.to_s if @object self end
Returns ‘true` if the value is even.
@return [Boolean] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 65 def even? to_i.even? end
Returns ‘self` if the value is not zero, `nil` otherwise.
@return [Boolean] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 115 def nonzero? to_i.nonzero? ? self : nil end
Returns ‘true` if the value is odd.
@return [Boolean] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 74 def odd? to_i.odd? end
Returns the predecessor value of ‘self`.
@return [RDF::Literal] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 46 def pred RDF::Literal(to_i.pred) end
Returns ‘self`.
From the XQuery function [fn:round](www.w3.org/TR/xpath-functions/#func-round).
@return [RDF::Literal] @see www.w3.org/TR/xpath-functions/#func-round
# File lib/rdf/model/literal/integer.rb, line 97 def round self end
Returns the successor value of ‘self`.
@return [RDF::Literal] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 55 def succ RDF::Literal(to_i.succ) end
Returns the value as an ‘OpenSSL::BN` instance.
@return [OpenSSL::BN] @see ruby-doc.org/stdlib/libdoc/openssl/rdoc/classes/OpenSSL/BN.html @since 0.2.4
# File lib/rdf/model/literal/integer.rb, line 133 def to_bn require 'openssl' unless defined?(OpenSSL::BN) OpenSSL::BN.new(to_s) end
Returns the value as a string.
@return [String]
# File lib/rdf/model/literal/integer.rb, line 123 def to_s @string || @object.to_s end
Returns ‘true` if the value is zero.
@return [Boolean] @since 0.2.3
# File lib/rdf/model/literal/integer.rb, line 106 def zero? to_i.zero? end