Module: Ruby::Rego::Builtins::NumericHelpers

Defined in:
lib/ruby/rego/builtins/numeric_helpers.rb

Overview

Shared numeric coercion helpers for builtins.

Class Method Summary collapse

Class Method Details

.integer_value(value, context:) ⇒ Integer

Parameters:

Returns:

  • (Integer)


15
16
17
18
19
20
21
22
# File 'lib/ruby/rego/builtins/numeric_helpers.rb', line 15

def self.integer_value(value, context:)
  Base.assert_type(value, expected: NumberValue, context: context)
  numeric = value.value
  return numeric if numeric.is_a?(Integer)
  return numeric.to_i if numeric.is_a?(Float) && numeric.finite? && numeric.modulo(1).zero?

  raise_integer_error(numeric, context)
end

.non_negative_integer(value, context:) ⇒ Integer

Parameters:

Returns:

  • (Integer)

Raises:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby/rego/builtins/numeric_helpers.rb', line 27

def self.non_negative_integer(value, context:)
  integer = integer_value(value, context: context)
  return integer if integer >= 0

  raise Ruby::Rego::BuiltinArgumentError.new(
    "Expected non-negative integer",
    expected: "non-negative integer",
    actual: integer,
    context: context,
    location: nil
  )
end