Class: RDF::Literal::Decimal
- Inherits:
-
RDF::Literal
- Object
- RDF::Literal
- RDF::Literal::Decimal
- Includes:
- Numeric
- Defined in:
- lib/rdf/model/literal/decimal.rb
Overview
A decimal literal.
Direct Known Subclasses
Constant Summary
- DATATYPE =
XSD.decimal
- GRAMMAR =
/^[\+\-]?\d+(\.\d*)?$/.freeze
Constants inherited from RDF::Literal
Instance Method Summary (collapse)
-
- (RDF::Literal) *(other)
Returns the product of
selftimesother. -
- (RDF::Literal) +(other)
Returns the sum of
selfplusother. -
- (RDF::Literal) +@
Returns
self. -
- (RDF::Literal) -(other)
Returns the difference of
selfminusother. -
- (RDF::Literal) -@
Returns
selfnegated. -
- (RDF::Literal) /(other)
Returns the quotient of
selfdivided byother. -
- (Integer) <=>(other)
Compares this literal to
otherfor sorting purposes. -
- (Boolean) ==(other)
(also: #===)
Returns
trueif this literal is equivalent toother. -
- (RDF::Literal) canonicalize!
Converts this literal into its canonical lexical representation.
-
- (Decimal) initialize(value, options = {})
constructor
A new instance of Decimal.
-
- (Boolean) nonzero?
Returns
selfif the value is not zero,nilotherwise. -
- (BigDecimal) to_d
Returns the value as a decimal number.
-
- (Float) to_f
Returns the value as a floating point number.
-
- (Integer) to_i
Returns the value as an integer.
-
- (Rational) to_r
Returns the value as a rational number.
-
- (String) to_s
Returns the value as a string.
-
- (Boolean) zero?
Returns
trueif the value is zero.
Methods inherited from RDF::Literal
#anonymous?, #canonicalize, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, #object, #valid?, #validate!, #value
Methods included from Term
Methods included from Value
#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_rdf, #uri?, #variable?
Constructor Details
- (Decimal) initialize(value, options = {})
A new instance of Decimal
22 23 24 25 26 27 28 29 30 |
# File 'lib/rdf/model/literal/decimal.rb', line 22 def initialize(value, = {}) @datatype = RDF::URI([:datatype] || DATATYPE) @string = [:lexical] if .has_key?(:lexical) @string = value if !defined?(@string) && value.is_a?(String) @object = case when value.is_a?(BigDecimal) then value else BigDecimal(value.to_s) end end |
Instance Method Details
- (RDF::Literal) *(other)
Returns the product of self times other.
150 151 152 |
# File 'lib/rdf/model/literal/decimal.rb', line 150 def *(other) RDF::Literal(to_d * (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end |
- (RDF::Literal) +(other)
Returns the sum of self plus other.
130 131 132 |
# File 'lib/rdf/model/literal/decimal.rb', line 130 def +(other) RDF::Literal(to_d + (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end |
- (RDF::Literal) +@
Returns self.
111 112 113 |
# File 'lib/rdf/model/literal/decimal.rb', line 111 def +@ self # unary plus end |
- (RDF::Literal) -(other)
Returns the difference of self minus other.
140 141 142 |
# File 'lib/rdf/model/literal/decimal.rb', line 140 def -(other) RDF::Literal(to_d - (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end |
- (RDF::Literal) -@
Returns self negated.
120 121 122 |
# File 'lib/rdf/model/literal/decimal.rb', line 120 def -@ RDF::Literal(-to_d) # unary minus end |
- (RDF::Literal) /(other)
Returns the quotient of self divided by other.
160 161 162 |
# File 'lib/rdf/model/literal/decimal.rb', line 160 def /(other) RDF::Literal(to_d / (other.respond_to?(:to_d) ? other.to_d : BigDecimal(other.to_s))) end |
- (Integer) <=>(other)
Compares this literal to other for sorting purposes.
58 59 60 61 62 63 64 65 66 |
# File 'lib/rdf/model/literal/decimal.rb', line 58 def <=>(other) case other when ::Numeric to_d <=> other when RDF::Literal::Decimal, RDF::Literal::Double to_d <=> other.to_d else super end end |
- (Boolean) ==(other) Also known as: ===
Returns true if this literal is equivalent to other.
74 75 76 |
# File 'lib/rdf/model/literal/decimal.rb', line 74 def ==(other) (cmp = (self <=> other)) ? cmp.zero? : false end |
- (RDF::Literal) canonicalize!
Converts this literal into its canonical lexical representation.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rdf/model/literal/decimal.rb', line 37 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 |
- (Boolean) nonzero?
Returns self if the value is not zero, nil otherwise.
102 103 104 |
# File 'lib/rdf/model/literal/decimal.rb', line 102 def nonzero? to_d.nonzero? ? self : nil end |
- (BigDecimal) to_d
Returns the value as a decimal number.
198 199 200 |
# File 'lib/rdf/model/literal/decimal.rb', line 198 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.
The usual accuracy limits and errors of binary float arithmetic apply.
189 190 191 |
# File 'lib/rdf/model/literal/decimal.rb', line 189 def to_f @object.to_f end |
- (Integer) to_i
Returns the value as an integer.
178 179 180 |
# File 'lib/rdf/model/literal/decimal.rb', line 178 def to_i @object.to_i end |
- (Rational) to_r
Returns the value as a rational number.
207 208 209 |
# File 'lib/rdf/model/literal/decimal.rb', line 207 def to_r @object.to_r # only available on Ruby 1.9+ end |
- (String) to_s
Returns the value as a string.
169 170 171 |
# File 'lib/rdf/model/literal/decimal.rb', line 169 def to_s @string || @object.to_s('F') end |
- (Boolean) zero?
Returns true if the value is zero.
93 94 95 |
# File 'lib/rdf/model/literal/decimal.rb', line 93 def zero? to_d.zero? end |