Class: RDF::Literal::Decimal

Inherits:
RDF::Literal show all
Includes:
Numeric
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:

Direct Known Subclasses

Integer

Constant Summary

DATATYPE =

Since:

  • 0.2.1

XSD.decimal
GRAMMAR =

Since:

  • 0.2.1

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

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Instance Method Summary (collapse)

Methods inherited from RDF::Literal

#anonymous?, #canonicalize, #eql?, #has_datatype?, #has_language?, #hash, #inspect, #invalid?, #literal?, #object, #valid?, #validate!, #value

Methods included from Term

#constant?, #variable?

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

Parameters:

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

    a customizable set of options

Options Hash (options):

  • (String) :lexical — default: nil

Since:

  • 0.2.1



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

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

Parameters:

  • (#to_d) other

Returns:

Since:

  • 0.2.3



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.

Parameters:

  • (#to_d) other

Returns:

Since:

  • 0.2.3



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.

Returns:

Since:

  • 0.2.3



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.

Parameters:

  • (#to_d) other

Returns:

Since:

  • 0.2.3



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.

Returns:

Since:

  • 0.2.3



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.

Parameters:

  • (#to_d) other

Returns:

Since:

  • 0.2.3



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.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



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.

Parameters:

  • (Object) other

Returns:

Since:

  • 0.3.0



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.

Returns:

See Also:

Since:

  • 0.2.1



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.

Returns:

Since:

  • 0.2.3



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.

Returns:

  • (BigDecimal)

See Also:

  • BigDecimal#to_d

Since:

  • 0.2.1



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.

Returns:

  • (Float)

See Also:

  • BigDecimal#to_f

Since:

  • 0.2.1



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.

Returns:

See Also:

  • BigDecimal#to_i

Since:

  • 0.2.1



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.

Returns:

  • (Rational)

See Also:

  • BigDecimal#to_r

Since:

  • 0.2.1



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.

Returns:

  • (String)

See Also:

  • BigDecimal#to_s

Since:

  • 0.2.1



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.

Returns:

Since:

  • 0.2.3



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

def zero?
  to_d.zero?
end