Class: RDF::Literal::Decimal

Inherits:
Numeric show all
Defined in:
lib/rdf/model/literal/decimal.rb

Overview

A decimal literal.

Examples:

Arithmetic with decimal literals

RDF::Literal(BigDecimal('1.0')) + 0.5   #=> RDF::Literal(BigDecimal('1.5'))
RDF::Literal(BigDecimal('1.0')) - 0.5   #=> RDF::Literal(BigDecimal('0.5'))
RDF::Literal(BigDecimal('1.0')) * 0.5   #=> RDF::Literal(BigDecimal('0.5'))
RDF::Literal(BigDecimal('1.0')) / 0.5   #=> RDF::Literal(BigDecimal('2.0'))

See Also:

Since:

  • 0.2.1

Direct Known Subclasses

Integer

Constant Summary collapse

DATATYPE =

Since:

  • 0.2.1

RDF::XSD.decimal
GRAMMAR =

Since:

  • 0.2.1

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

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Attribute Summary

Attributes inherited from RDF::Literal

#datatype, #language

Instance Method Summary collapse

Methods inherited from Numeric

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

Methods inherited from RDF::Literal

#==, #compatible?, #comperable_datatype?, datatype_map, datatyped_class, #eql?, #escape, #freeze, #has_datatype?, #has_language?, #hash, #humanize, #inspect, #literal?, new, #object, #plain?, #simple?, #valid?, #validate!, #value, #value_hash

Methods included from Term

#<=>, #==, #compatible?, #eql?, #term?, #to_base, #to_term

Methods included from Value

#anonymous?, #canonicalize, #constant?, #graph?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

#initialize(value, options = {}) ⇒ Decimal

Returns a new instance of Decimal.

Parameters:

  • value (BigDecimal)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :lexical (String) — default: nil

Since:

  • 0.2.1



20
21
22
23
24
25
26
27
28
# File 'lib/rdf/model/literal/decimal.rb', line 20

def initialize(value, options = {})
  @datatype = RDF::URI(options[:datatype] || self.class.const_get(:DATATYPE))
  @string   = options[:lexical] if options.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

#absRDF::Literal

Returns the absolute value of ‘self`.

Returns:

Since:

  • 0.2.3



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.

Returns:

See Also:

Since:

  • 0.2.1



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

#ceilRDF::Literal

Returns the smallest integer greater than or equal to ‘self`.

Examples:

RDF::Literal(1).ceil            #=> RDF::Literal(1)

Returns:

Since:

  • 0.2.1



74
75
76
# File 'lib/rdf/model/literal/decimal.rb', line 74

def ceil
  self.class.new(to_d.ceil)
end

#floorRDF::Literal

Returns the largest integer less than or equal to ‘self`.

Examples:

RDF::Literal(1).floor            #=> RDF::Literal(1)

Returns:

Since:

  • 0.2.1



85
86
87
# File 'lib/rdf/model/literal/decimal.rb', line 85

def floor
  self.class.new(to_d.floor)
end

#nonzero?Boolean

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

Returns:

Since:

  • 0.2.3



103
104
105
# File 'lib/rdf/model/literal/decimal.rb', line 103

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

#roundRDF::Literal

Returns the number with no fractional part that is closest to the argument. If there are two such numbers, then the one that is closest to positive infinity is returned. An error is raised if arg is not a numeric value.

Returns:

Since:

  • 0.2.1



63
64
65
# File 'lib/rdf/model/literal/decimal.rb', line 63

def round
  self.class.new(to_d.round)
end

#to_sString

Returns the value as a string.

Returns:

  • (String)

See Also:

  • BigDecimal#to_s

Since:

  • 0.2.1



112
113
114
# File 'lib/rdf/model/literal/decimal.rb', line 112

def to_s
  @string || @object.to_s('F')
end

#zero?Boolean

Returns ‘true` if the value is zero.

Returns:

Since:

  • 0.2.3



94
95
96
# File 'lib/rdf/model/literal/decimal.rb', line 94

def zero?
  to_d.zero?
end