Class: RDF::Literal::Decimal

Inherits:
Numeric show all
Defined in:
vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/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::URI("http://www.w3.org/2001/XMLSchema#decimal")
GRAMMAR =

Since:

  • 0.2.1

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

Constants inherited from RDF::Literal

FALSE, TRUE, ZERO

Constants included from N3::Terminals

N3::Terminals::ANON, N3::Terminals::BASE, N3::Terminals::BLANK_NODE_LABEL, N3::Terminals::DECIMAL, N3::Terminals::DOUBLE, N3::Terminals::ECHAR, N3::Terminals::ESCAPE_CHAR4, N3::Terminals::ESCAPE_CHAR8, N3::Terminals::EXPONENT, N3::Terminals::FORALL, N3::Terminals::INTEGER, N3::Terminals::IRIREF, N3::Terminals::IRI_RANGE, N3::Terminals::LANGTAG, N3::Terminals::PERCENT, N3::Terminals::PLX, N3::Terminals::PNAME_LN, N3::Terminals::PNAME_NS, N3::Terminals::PN_CHARS, N3::Terminals::PN_CHARS_BASE, N3::Terminals::PN_CHARS_BODY, N3::Terminals::PN_CHARS_U, N3::Terminals::PN_LOCAL, N3::Terminals::PN_LOCAL_BODY, N3::Terminals::PN_LOCAL_ESC, N3::Terminals::PN_PREFIX, N3::Terminals::PREFIX, N3::Terminals::QUICK_VAR_NAME, N3::Terminals::STRING_LITERAL_LONG_QUOTE, N3::Terminals::STRING_LITERAL_LONG_SINGLE_QUOTE, N3::Terminals::STRING_LITERAL_QUOTE, N3::Terminals::STRING_LITERAL_SINGLE_QUOTE, N3::Terminals::UCHAR, N3::Terminals::U_CHARS1, N3::Terminals::U_CHARS2, N3::Terminals::WS

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

#==, #as_datetime, #as_number, #compatible?, #comperable_datatype?, #datatype?, #eql?, #escape, #hash, #humanize, #inspect, #language?, #literal?, #method_missing, #object, #plain?, #respond_to_missing?, #simple?, #squish, #squish!, #valid?, #valid_extended?, #validate!, #value, #value_hash

Methods included from Term

#<=>, #==, #aggregate?, #as_datetime, #as_number, #compatible?, #eql?, #escape, #evaluate, #ndvars, #optimize, #sameTerm?, #term?, #terms, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, new, #node?, open, #optimize, #optimize!, parse, register_extension, #to_sxp_bin, #valid?, #validate!, #variable?

Methods included from Value

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

Constructor Details

#initialize(value, datatype: nil, lexical: nil, **options) ⇒ Decimal

Returns a new instance of Decimal.

Parameters:

  • value (String, BidDecimal, Numeric)
  • value (Object)
  • language (Symbol)

    (nil) Language is downcased to ensure proper matching

  • lexical (String) (defaults to: nil)

    (nil) Supplied lexical representation of this literal, otherwise it comes from transforming value to a string form..

  • datatype (URI) (defaults to: nil)

    (nil)

  • validate (Boolean)

    (false)

  • canonicalize (Boolean)

    (false)

Since:

  • 0.2.1



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 20

def initialize(value, datatype: nil, lexical: nil, **options)
  @datatype = RDF::URI(datatype || self.class.const_get(:DATATYPE))
  @string   = lexical || (value if value.is_a?(String))
  @object   = case
    when value.is_a?(::BigDecimal) then value
    when value.is_a?(::Float)      then BigDecimal(value.to_s)
    when value.is_a?(::Numeric)    then BigDecimal(value)
    else
      value = value.to_s
      value += "0" if value.end_with?(".")  # Normalization required in Ruby 2.4
      BigDecimal(value) rescue BigDecimal(0)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Literal

Instance Method Details

#absRDF::Literal

Returns the absolute value of self.

Returns:

Since:

  • 0.2.3



59
60
61
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 59

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 39

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::Integer

Returns the smallest integer greater than or equal to self.

Examples:

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

Returns:

Since:

  • 0.2.1



84
85
86
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 84

def ceil
  RDF::Literal::Integer.new(to_d.ceil)
end

#floorRDF::Literal::Integer

Returns the largest integer less than or equal to self.

Examples:

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

Returns:

Since:

  • 0.2.1



95
96
97
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 95

def floor
  RDF::Literal::Integer.new(to_d.floor)
end

#nonzero?Boolean

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

Returns:

Since:

  • 0.2.3



113
114
115
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 113

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

#roundRDF::Literal::Decimal

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



67
68
69
70
71
72
73
74
75
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 67

def round
  rounded = to_d.round(half: (to_d < 0 ? :down : :up))
  if rounded == -0.0
    # to avoid -0.0
    self.class.new(0.0)
  else
    self.class.new(rounded)
  end
end

#to_sString

Returns the value as a string.

Returns:

See Also:

  • BigDecimal#to_s

Since:

  • 0.2.1



122
123
124
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 122

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

#zero?Boolean

Returns true if the value is zero.

Returns:

Since:

  • 0.2.3



104
105
106
# File 'vendor/bundler/ruby/3.0.0/bundler/gems/rdf-237066655558/lib/rdf/model/literal/decimal.rb', line 104

def zero?
  to_d.zero?
end