Class: RDF::Literal::Integer
- Inherits:
-
Decimal
- Object
- RDF::Literal
- Numeric
- Decimal
- RDF::Literal::Integer
- Defined in:
- lib/rdf/model/literal/integer.rb
Overview
An integer literal.
Constant Summary collapse
- DATATYPE =
XSD.integer
- GRAMMAR =
/^[\+\-]?\d+$/.freeze
Constants inherited from RDF::Literal
Instance Attribute Summary
Attributes inherited from RDF::Literal
Instance Method Summary collapse
-
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
-
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
-
#even? ⇒ Boolean
Returns ‘true` if the value is even.
-
#initialize(value, options = {}) ⇒ Integer
constructor
A new instance of Integer.
-
#nonzero? ⇒ Boolean
Returns ‘self` if the value is not zero, `nil` otherwise.
-
#odd? ⇒ Boolean
Returns ‘true` if the value is odd.
-
#pred ⇒ RDF::Literal
Returns the successor value of ‘self`.
-
#succ ⇒ RDF::Literal
(also: #next)
Returns the predecessor value of ‘self`.
-
#to_bn ⇒ OpenSSL::BN
Returns the value as an ‘OpenSSL::BN` instance.
-
#to_s ⇒ String
Returns the value as a string.
-
#zero? ⇒ Boolean
Returns ‘true` if the value is zero.
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.
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rdf/model/literal/integer.rb', line 21 def initialize(value, = {}) @datatype = RDF::URI([:datatype] || self.class.const_get(:DATATYPE)) @string = [:lexical] if .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
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
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.
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.
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.
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.
76 77 78 |
# File 'lib/rdf/model/literal/integer.rb', line 76 def odd? to_i.odd? end |
#pred ⇒ RDF::Literal
Returns the successor value of ‘self`.
48 49 50 |
# File 'lib/rdf/model/literal/integer.rb', line 48 def pred RDF::Literal(to_i.pred) end |
#succ ⇒ RDF::Literal Also known as: next
Returns the predecessor value of ‘self`.
57 58 59 |
# File 'lib/rdf/model/literal/integer.rb', line 57 def succ RDF::Literal(to_i.succ) end |
#to_bn ⇒ OpenSSL::BN
Returns the value as an ‘OpenSSL::BN` instance.
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_s ⇒ String
Returns the value as a string.
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.
94 95 96 |
# File 'lib/rdf/model/literal/integer.rb', line 94 def zero? to_i.zero? end |