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

new(value, options = {}) click to toggle source

@param [Integer, to_i] value @option options [String] :lexical (nil)

# File lib/rdf/model/literal/integer.rb, line 21
def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   ||= value if value.is_a?(String)
  @object   = case
    when value.is_a?(::String)    then Integer(value) rescue nil
    when value.is_a?(::Integer)   then value
    when value.respond_to?(:to_i) then value.to_i
    else Integer(value.to_s) rescue nil
  end
end

Public Instance Methods

abs() click to toggle source

Returns the absolute value of `self`.

@return [RDF::Literal] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 85
def abs
  (n = to_i) && n > 0 ? self : self.class.new(n.abs)
end
canonicalize!() click to toggle source

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 38
def canonicalize!
  @string = @object.to_s if @object
  self
end
even?() click to toggle source

Returns `true` if the value is even.

@return [Boolean] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 67
def even?
  to_i.even?
end
next()
Alias for: succ
nonzero?() click to toggle source

Returns `self` if the value is not zero, `nil` otherwise.

@return [Boolean] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 111
def nonzero?
  to_i.nonzero? ? self : nil
end
odd?() click to toggle source

Returns `true` if the value is odd.

@return [Boolean] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 76
def odd?
  to_i.odd?
end
pred() click to toggle source

Returns the predecessor value of `self`.

@return [RDF::Literal] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 48
def pred
  RDF::Literal(to_i.pred)
end
round() click to toggle source

Returns `self`.

@return [RDF::Literal]

# File lib/rdf/model/literal/integer.rb, line 93
def round
  self
end
succ() click to toggle source

Returns the successor value of `self`.

@return [RDF::Literal] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 57
def succ
  RDF::Literal(to_i.succ)
end
Also aliased as: next
to_bn() click to toggle source

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 129
def to_bn
  require 'openssl' unless defined?(OpenSSL::BN)
  OpenSSL::BN.new(to_s)
end
to_s() click to toggle source

Returns the value as a string.

@return [String]

# File lib/rdf/model/literal/integer.rb, line 119
def to_s
  @string || @object.to_s
end
zero?() click to toggle source

Returns `true` if the value is zero.

@return [Boolean] @since 0.2.3

# File lib/rdf/model/literal/integer.rb, line 102
def zero?
  to_i.zero?
end