Module: Ruby::Rego::Builtins::Base

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

Overview

Shared helpers for built-in function implementations.

Class Method Summary collapse

Class Method Details

.assert_arity(args, expected, name: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • args (Array<Object>)
  • expected (Integer, Array<Integer>)
  • name (String, nil) (defaults to: nil)


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

def self.assert_arity(args, expected, name: nil)
  actual = args.size
  return if arity_valid?(actual, expected)

  raise_builtin_arity_error(actual, expected, name)
end

.assert_type(value, expected:, context: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • value (Object)
  • expected (Class, Array<Class>)
  • context (String, nil) (defaults to: nil)


26
27
28
29
30
31
32
33
34
35
# File 'lib/ruby/rego/builtins/base.rb', line 26

def self.assert_type(value, expected:, context: nil)
  expected_classes = normalize_expected(expected)
  return if expected_classes.any? { |klass| value.is_a?(klass) }

  raise_type_error(
    expected: expected_classes.map(&:name).join(" or "),
    actual: value.class.name,
    context: context
  )
end

.byte_safe_encoding?(string) ⇒ Boolean

The shared encoding guard for BYTE-oriented builtins (uri/net parsing, AWS SigV4 byte assembly): the string must be ASCII-compatible and validly encoded so its bytes can be scanned/concatenated. A binary (ASCII-8BIT) string from base64.decode passes; an invalid-UTF-8 or non-ASCII-compatible (UTF-16) string is rejected, letting the caller map it to undefined. (CHAR-oriented builtins use a stricter guard; this is deliberately the byte-oriented one.)

Parameters:

  • string (String)

Returns:

  • (Boolean)


56
57
58
# File 'lib/ruby/rego/builtins/base.rb', line 56

def self.byte_safe_encoding?(string)
  string.encoding.ascii_compatible? && string.valid_encoding?
end

.raise_argument_error(message, expected:, actual:, context: nil) ⇒ Object

Raises a BuiltinArgumentError (caught by the registry and surfaced as undefined): the shared BuiltinArgumentError.new(..., location: nil) construction. A builtin’s raise_* guard delegates here when its expected/actual are strings; guards that pass an Array or number build the error inline so this signature stays tight (String message, String expected, String|Integer actual). :reek:LongParameterList



86
87
88
89
90
91
92
93
94
# File 'lib/ruby/rego/builtins/base.rb', line 86

def self.raise_argument_error(message, expected:, actual:, context: nil)
  raise Ruby::Rego::BuiltinArgumentError.new(
    message,
    expected: expected,
    actual: actual,
    context: context,
    location: nil
  )
end

.to_ruby(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


39
40
41
# File 'lib/ruby/rego/builtins/base.rb', line 39

def self.to_ruby(value)
  value.is_a?(Ruby::Rego::Value) ? value.to_ruby : value
end

.to_value(value) ⇒ Ruby::Rego::Value

Parameters:

  • value (Object)

Returns:



45
46
47
# File 'lib/ruby/rego/builtins/base.rb', line 45

def self.to_value(value)
  Ruby::Rego::Value.from_ruby(value)
end