Class: RDF::Literal::Decimal
- Inherits:
-
Numeric
- Object
- RDF::Literal
- Numeric
- RDF::Literal::Decimal
- Defined in:
- lib/rdf/model/literal/decimal.rb
Overview
A decimal literal.
Direct Known Subclasses
Constant Summary collapse
- DATATYPE =
XSD.decimal
- GRAMMAR =
/^[\+\-]?\d+(\.\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.
-
#initialize(value, options = {}) ⇒ Decimal
constructor
A new instance of Decimal.
-
#nonzero? ⇒ Boolean
Returns ‘self` if the value is not zero, `nil` otherwise.
-
#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 = {}) ⇒ Decimal
Returns a new instance of Decimal.
20 21 22 23 24 25 26 27 28 |
# File 'lib/rdf/model/literal/decimal.rb', line 20 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?(BigDecimal) then value else BigDecimal(value.to_s) end end |
Instance Method Details
#abs ⇒ RDF::Literal
Returns the absolute value of ‘self`.
55 56 57 |
# File 'lib/rdf/model/literal/decimal.rb', line 55 def abs (d = to_d) && d > 0 ? self : RDF::Literal(d.abs) end |
#canonicalize! ⇒ RDF::Literal
Converts this literal into its canonical lexical representation.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rdf/model/literal/decimal.rb', line 35 def canonicalize! # Can't use simple %f transformation due to special requirements from # N3 tests in representation @string = begin i, f = @object.to_s('F').split('.') i.sub!(/^\+?0+(\d)$/, '\1') # remove the optional leading '+' sign and any extra leading zeroes f = f[0, 16] # truncate the fractional part after 15 decimal places f.sub!(/0*$/, '') # remove any trailing zeroes f = '0' if f.empty? # ...but there must be a digit to the right of the decimal point "#{i}.#{f}" end @object = BigDecimal(@string) unless @object.nil? self end |
#nonzero? ⇒ Boolean
Returns ‘self` if the value is not zero, `nil` otherwise.
73 74 75 |
# File 'lib/rdf/model/literal/decimal.rb', line 73 def nonzero? to_d.nonzero? ? self : nil end |
#to_s ⇒ String
Returns the value as a string.
82 83 84 |
# File 'lib/rdf/model/literal/decimal.rb', line 82 def to_s @string || @object.to_s('F') end |
#zero? ⇒ Boolean
Returns ‘true` if the value is zero.
64 65 66 |
# File 'lib/rdf/model/literal/decimal.rb', line 64 def zero? to_d.zero? end |