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

Since:

• 0.2.1

Constant Summary

DATATYPE =

Since:

• 0.2.1

`RDF::XSD.integer`
GRAMMAR =

Since:

• 0.2.1

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

Instance Method Summary collapse

• Returns the absolute value of `self`.

• Converts this literal into its canonical lexical representation.

• Returns `true` if the value is even.

• constructor

A new instance of Integer.

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

• Returns `true` if the value is odd.

• Returns the predecessor value of `self`.

• Returns `self`.

• #succ ⇒ RDF::Literal (also: #next)

Returns the successor value of `self`.

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

• Returns the value as a string.

• Returns `true` if the value is zero.

Methods inherited from Numeric

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

Constructor Details

#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Integer

Returns a new instance of Integer

Parameters:

• value (String, Integer, #to_i)
• value (Object)
• language (Symbol)

(nil) Language is downcased to ensure proper matching

• lexical (String)

(nil) Supplied lexical representation of this literal, otherwise it comes from transforming `value` to a string form..

• datatype (URI)

(nil)

• validate (Boolean)

(false)

• canonicalize (Boolean)

(false)

Since:

• 0.2.1

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

Instance Method Details

#abs ⇒ RDF::Literal

Returns the absolute value of `self`.

Returns:

Since:

• 0.2.3

 ``` 83 84 85``` ```# File 'lib/rdf/model/literal/integer.rb', line 83 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.

Returns:

• `self`

Since:

• 0.2.1

 ``` 36 37 38 39``` ```# File 'lib/rdf/model/literal/integer.rb', line 36 def canonicalize! @string = @object.to_s if @object self end```

#even? ⇒ Boolean

Returns `true` if the value is even.

Returns:

Since:

• 0.2.3

 ``` 65 66 67``` ```# File 'lib/rdf/model/literal/integer.rb', line 65 def even? to_i.even? end```

#nonzero? ⇒ Boolean

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

Returns:

Since:

• 0.2.3

 ``` 109 110 111``` ```# File 'lib/rdf/model/literal/integer.rb', line 109 def nonzero? to_i.nonzero? ? self : nil end```

#odd? ⇒ Boolean

Returns `true` if the value is odd.

Returns:

Since:

• 0.2.3

 ``` 74 75 76``` ```# File 'lib/rdf/model/literal/integer.rb', line 74 def odd? to_i.odd? end```

#pred ⇒ RDF::Literal

Returns the predecessor value of `self`.

Returns:

Since:

• 0.2.3

 ``` 46 47 48``` ```# File 'lib/rdf/model/literal/integer.rb', line 46 def pred RDF::Literal(to_i.pred) end```

#round ⇒ RDF::Literal

Returns `self`.

Returns:

Since:

• 0.2.1

 ``` 91 92 93``` ```# File 'lib/rdf/model/literal/integer.rb', line 91 def round self end```

#succ ⇒ RDF::LiteralAlso known as: next

Returns the successor value of `self`.

Returns:

Since:

• 0.2.3

 ``` 55 56 57``` ```# File 'lib/rdf/model/literal/integer.rb', line 55 def succ RDF::Literal(to_i.succ) end```

#to_bn ⇒ OpenSSL::BN

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

Returns:

• (OpenSSL::BN)

Since:

• 0.2.4

 ``` 127 128 129 130``` ```# File 'lib/rdf/model/literal/integer.rb', line 127 def to_bn require 'openssl' unless defined?(OpenSSL::BN) OpenSSL::BN.new(to_s) end```

#to_s ⇒ String

Returns the value as a string.

Returns:

• (String)

Since:

• 0.2.1

 ``` 117 118 119``` ```# File 'lib/rdf/model/literal/integer.rb', line 117 def to_s @string || @object.to_s end```

#zero? ⇒ Boolean

Returns `true` if the value is zero.

Returns:

Since:

• 0.2.3

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