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

RDF::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 Decimal

#ceil, #floor

Methods inherited from Numeric

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

Methods inherited from RDF::Literal

#==, #compatible?, #comperable_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #humanize, #inspect, #literal?, new, #object, #plain?, #simple?, #valid?, #validate!, #value, #value_hash

Methods included from Term

#<=>, #==, #compatible?, #eql?, #term?, #to_base, #to_term

Methods included from Value

#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

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

Returns a new instance of Integer.

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`.

Since:

  • 0.2.3



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

def abs
  (n = to_i) && n > 0 ? self : self.class.new(n.abs)
end

#canonicalize!RDF::Literal

Converts this literal into its canonical lexical representation.



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.

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.

Since:

  • 0.2.3



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

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

#odd?Boolean

Returns ‘true` if the value is odd.

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 predecessor value of ‘self`.

Since:

  • 0.2.3



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

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

#roundRDF::Literal

Returns ‘self`.

Since:

  • 0.2.1



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

def round
  self
end

#succRDF::Literal Also known as: next

Returns the successor value of ‘self`.

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.



129
130
131
132
# 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_sString

Returns the value as a string.

Since:

  • 0.2.1



119
120
121
# File 'lib/rdf/model/literal/integer.rb', line 119

def to_s
  @string || @object.to_s
end

#zero?Boolean

Returns ‘true` if the value is zero.

Since:

  • 0.2.3



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

def zero?
  to_i.zero?
end