Class: RDF::Literal::Integer

Inherits:
Decimal show all
Includes:
Numeric
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:

Constant Summary

DATATYPE =

Since:

  • 0.2.1

XSD.integer
GRAMMAR =

Since:

  • 0.2.1

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

Constants inherited from Decimal

DATATYPE, GRAMMAR

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Method Summary (collapse)

Methods inherited from Decimal

#<=>, #==

Methods inherited from RDF::Literal

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

Methods included from Term

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

Methods included from Value

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

Constructor Details

- (Integer) initialize(value, options = {})

A new instance of Integer

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (String) :lexical — default: nil

Since:

  • 0.2.1



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rdf/model/literal/integer.rb', line 22

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || DATATYPE)
  @string   = options[:lexical] if options.has_key?(:lexical)
  @string   = value if !defined?(@string) && 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

- (RDF::Literal) *(other)

Returns the product of self times other.

Parameters:

  • (#to_i) other

Returns:

Since:

  • 0.2.3



152
153
154
# File 'lib/rdf/model/literal/integer.rb', line 152

def *(other)
  RDF::Literal(to_i * other.to_i)
end

- (RDF::Literal) +(other)

Returns the sum of self plus other.

Parameters:

  • (#to_i) other

Returns:

Since:

  • 0.2.3



132
133
134
# File 'lib/rdf/model/literal/integer.rb', line 132

def +(other)
  RDF::Literal(to_i + other.to_i)
end

- (RDF::Literal) +@

Returns self.

Returns:

Since:

  • 0.2.3



113
114
115
# File 'lib/rdf/model/literal/integer.rb', line 113

def +@
  self # unary plus
end

- (RDF::Literal) -(other)

Returns the difference of self minus other.

Parameters:

  • (#to_i) other

Returns:

Since:

  • 0.2.3



142
143
144
# File 'lib/rdf/model/literal/integer.rb', line 142

def -(other)
  RDF::Literal(to_i - other.to_i)
end

- (RDF::Literal) -@

Returns self negated.

Returns:

Since:

  • 0.2.3



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

def -@
  RDF::Literal(-to_i) # unary minus
end

- (RDF::Literal) /(other)

Returns the quotient of self divided by other.

Parameters:

  • (#to_i) other

Returns:

Raises:

  • (ZeroDivisionError)

    if divided by zero

Since:

  • 0.2.3



163
164
165
# File 'lib/rdf/model/literal/integer.rb', line 163

def /(other)
  RDF::Literal(to_i / other.to_i)
end

- (RDF::Literal) abs

Returns the absolute value of self.

Returns:

Since:

  • 0.2.3



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

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

- (RDF::Literal) canonicalize!

Converts this literal into its canonical lexical representation.

Returns:

See Also:

Since:

  • 0.2.1



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

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

- (Boolean) even?

Returns true if the value is even.

Returns:

Since:

  • 0.2.3



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

def even?
  to_i.even?
end

- (Boolean) nonzero?

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

Returns:

Since:

  • 0.2.3



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

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

- (Boolean) odd?

Returns true if the value is odd.

Returns:

Since:

  • 0.2.3



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

def odd?
  to_i.odd?
end

- (RDF::Literal) pred

Returns the successor value of self.

Returns:

Since:

  • 0.2.3



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

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

- (RDF::Literal) succ Also known as: next

Returns the predecessor value of self.

Returns:

Since:

  • 0.2.3



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

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

- (OpenSSL::BN) to_bn

Returns the value as an OpenSSL::BN instance.

Returns:

  • (OpenSSL::BN)

See Also:

Since:

  • 0.2.4



215
216
217
218
# File 'lib/rdf/model/literal/integer.rb', line 215

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

- (BigDecimal) to_d

Returns the value as a decimal number.

Returns:

  • (BigDecimal)

Since:

  • 0.2.1



197
198
199
# File 'lib/rdf/model/literal/integer.rb', line 197

def to_d
  @object.respond_to?(:to_d) ? @object.to_d : BigDecimal(@object.to_s)
end

- (Float) to_f

Returns the value as a floating point number.

Returns:

  • (Float)

Since:

  • 0.2.1



189
190
191
# File 'lib/rdf/model/literal/integer.rb', line 189

def to_f
  @object.to_f
end

- (Integer) to_i Also known as: ord, to_int

Returns the value as an integer.

Returns:

Since:

  • 0.2.1



179
180
181
# File 'lib/rdf/model/literal/integer.rb', line 179

def to_i
  @object.to_i
end

- (Rational) to_r

Returns the value as a rational number.

Returns:

  • (Rational)

Since:

  • 0.2.1



205
206
207
# File 'lib/rdf/model/literal/integer.rb', line 205

def to_r
  @object.to_r
end

- (String) to_s

Returns the value as a string.

Returns:

  • (String)

Since:

  • 0.2.1



171
172
173
# File 'lib/rdf/model/literal/integer.rb', line 171

def to_s
  @string || @object.to_s
end

- (Boolean) zero?

Returns true if the value is zero.

Returns:

Since:

  • 0.2.3



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

def zero?
  to_i.zero?
end