Class: Ruby::Rego::CompiledModule

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/rego/compiled_module.rb

Overview

Bundles compiled rule metadata for fast evaluation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(package_path:, rules_by_name:, imports: [], dependency_graph: {}) ⇒ CompiledModule

Create a compiled module bundle.

Parameters:

  • package_path (Array<String>)

    module package path

  • rules_by_name (Hash{String => Array<AST::Rule>})

    indexed rules

  • imports (Array<AST::Import>) (defaults to: [])

    imports from the module

  • dependency_graph (Hash{String => Array<String>}) (defaults to: {})

    rule dependencies



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby/rego/compiled_module.rb', line 13

def initialize(package_path:, rules_by_name:, imports: [], dependency_graph: {})
  state = {
    package_path: package_path,
    rules_by_name: rules_by_name,
    imports: imports,
    dependency_graph: dependency_graph
  }
  @package_path,
    @rules_by_name,
    @imports,
    @dependency_graph = Normalizer.new(state).normalize
end

Instance Attribute Details

#dependency_graphHash{String => Array<String>} (readonly)

Dependency graph for rule evaluation ordering.

Returns:

  • (Hash{String => Array<String>})


44
45
46
# File 'lib/ruby/rego/compiled_module.rb', line 44

def dependency_graph
  @dependency_graph
end

#importsArray<AST::Import> (readonly)

Import declarations.

Returns:



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

def imports
  @imports
end

#package_pathArray<String> (readonly)

The module package path.

Returns:

  • (Array<String>)


29
30
31
# File 'lib/ruby/rego/compiled_module.rb', line 29

def package_path
  @package_path
end

#rules_by_nameHash{String => Array<AST::Rule>} (readonly)

Rules indexed by name.

Returns:



34
35
36
# File 'lib/ruby/rego/compiled_module.rb', line 34

def rules_by_name
  @rules_by_name
end

Instance Method Details

#has_rule?(name) ⇒ Boolean

Check whether a rule exists.

rubocop:disable Naming/PredicatePrefix

Parameters:

  • name (String, Symbol)

    rule name

Returns:

  • (Boolean)

    true when present



66
67
68
# File 'lib/ruby/rego/compiled_module.rb', line 66

def has_rule?(name)
  rules_by_name.key?(name.to_s)
end

#lookup_rule(name) ⇒ Array<AST::Rule>

Fetch rules for a given name.

Parameters:

  • name (String, Symbol)

    rule name

Returns:

  • (Array<AST::Rule>)

    rules matching the name



50
51
52
# File 'lib/ruby/rego/compiled_module.rb', line 50

def lookup_rule(name)
  rules_by_name.fetch(name.to_s) { empty_rules }
end

#rule_namesArray<String>

List all rule names.

Returns:

  • (Array<String>)


57
58
59
# File 'lib/ruby/rego/compiled_module.rb', line 57

def rule_names
  rules_by_name.keys
end