Class: RDF::Literal::Integer

Inherits:
Decimal show all
Defined in:
lib/rdf/model/literal/integer.rb

Overview

An integer literal.

Examples:

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 Also:

Since:

  • 0.2.1

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

XSD.integer
GRAMMAR =

Since:

  • 0.2.1

/^[\+\-]?\d+$/.freeze

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from Numeric

#*, #+, #+@, #-, #-@, #/, #<=>, #==, #to_d, #to_f, #to_i, #to_r

Methods inherited from RDF::Literal

#==, #anonymous?, #canonicalize, #comperable_datatype?, datatyped_class, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, new, #object, #plain?, #valid?, #validate!, #value

Methods included from Term

#<=>, #==, #constant?, #eql?, #variable?

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?

Constructor Details

#initialize(value, options = {}) ⇒ Integer

Returns a new instance of Integer.

Parameters:

  • value (Integer, #to_i)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1



21
22
23
24
25
26
27
28
29
30
31
# 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

Instance Method Details

#absRDF::Literal

Returns the absolute value of ‘self`.

Returns:

Since:

  • 0.2.3



85
86
87
# File 'lib/rdf/model/literal/integer.rb', line 85

def abs
  (n = to_i) && n > 0 ? self : RDF::Literal(n.abs)
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.

Returns:

See Also:

Since:

  • 0.2.1



38
39
40
41
# File 'lib/rdf/model/literal/integer.rb', line 38

def canonicalize!
  @string = @object.to_s if @object
  self
end

#even?Boolean

Returns ‘true` if the value is even.

Returns:

Since:

  • 0.2.3



67
68
69
# File 'lib/rdf/model/literal/integer.rb', line 67

def even?
  to_i.even?
end

#nonzero?Boolean

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

Returns:

Since:

  • 0.2.3



103
104
105
# File 'lib/rdf/model/literal/integer.rb', line 103

def nonzero?
  to_i.nonzero? ? self : nil
end

#odd?Boolean

Returns ‘true` if the value is odd.

Returns:

Since:

  • 0.2.3



76
77
78
# File 'lib/rdf/model/literal/integer.rb', line 76

def odd?
  to_i.odd?
end

#predRDF::Literal

Returns the successor value of ‘self`.

Returns:

Since:

  • 0.2.3



48
49
50
# File 'lib/rdf/model/literal/integer.rb', line 48

def pred
  RDF::Literal(to_i.pred)
end

#succRDF::Literal Also known as: next

Returns the predecessor value of ‘self`.

Returns:

Since:

  • 0.2.3



57
58
59
# File 'lib/rdf/model/literal/integer.rb', line 57

def succ
  RDF::Literal(to_i.succ)
end

#to_bnOpenSSL::BN

Returns the value as an ‘OpenSSL::BN` instance.

Returns:

  • (OpenSSL::BN)

See Also:

Since:

  • 0.2.4



121
122
123
124
# File 'lib/rdf/model/literal/integer.rb', line 121

def to_bn
  require 'openssl' unless defined?(OpenSSL::BN)
  OpenSSL::BN.new(to_s)
end

#to_sString

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



111
112
113
# File 'lib/rdf/model/literal/integer.rb', line 111

def to_s
  @string || @object.to_s
end

#zero?Boolean

Returns ‘true` if the value is zero.

Returns:

Since:

  • 0.2.3



94
95
96
# File 'lib/rdf/model/literal/integer.rb', line 94

def zero?
  to_i.zero?
end