Class: Ruby::Rego::Result

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

Overview

Represents the outcome of evaluating a policy or expression.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, success:, bindings: {}, errors: []) ⇒ Result

Create a result wrapper.

Parameters:

  • value (Object)

    evaluation value

  • success (Boolean)

    success flag

  • bindings (Hash{String, Symbol => Object}) (defaults to: {})

    variable bindings

  • errors (Array<Object>) (defaults to: [])

    collected errors



37
38
39
40
41
42
43
# File 'lib/ruby/rego/result.rb', line 37

def initialize(value:, success:, bindings: {}, errors: [])
  @value = Value.from_ruby(value)
  @bindings = {} # @type var @bindings: Hash[String, Value]
  add_bindings(bindings)
  @success = success
  @errors = errors.dup
end

Instance Attribute Details

#bindingsHash{String => Value} (readonly)

Variable bindings captured during evaluation.

Returns:

  • (Hash{String => Value})


19
20
21
# File 'lib/ruby/rego/result.rb', line 19

def bindings
  @bindings
end

#errorsArray<Object> (readonly)

Errors collected during evaluation.

Returns:

  • (Array<Object>)


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

def errors
  @errors
end

#successBoolean (readonly)

True when evaluation succeeded and produced a value.

Returns:

  • (Boolean)


24
25
26
# File 'lib/ruby/rego/result.rb', line 24

def success
  @success
end

#valueValue (readonly)

Evaluated value.

Returns:



14
15
16
# File 'lib/ruby/rego/result.rb', line 14

def value
  @value
end

Instance Method Details

#success?Boolean

Convenience success predicate.

Returns:

  • (Boolean)


48
49
50
# File 'lib/ruby/rego/result.rb', line 48

def success?
  success
end

#to_hHash{Symbol => Object}

Convert the result to a serializable hash.

Returns:

  • (Hash{Symbol => Object})


62
63
64
65
66
67
68
69
# File 'lib/ruby/rego/result.rb', line 62

def to_h
  {
    value: value.to_ruby,
    bindings: bindings.transform_values(&:to_ruby),
    success: success,
    errors: errors.map { |error| ErrorPayload.from(error) }
  }
end

#to_json(*args) ⇒ String

Serialize the result as JSON.

Parameters:

  • _args (Array<Object>)

Returns:

  • (String)


75
76
77
78
79
80
# File 'lib/ruby/rego/result.rb', line 75

def to_json(*args)
  options = args.first
  return JSON.generate(to_h) unless options.is_a?(Hash)

  JSON.generate(to_h, options)
end

#undefined?Boolean

True when the value is undefined.

Returns:

  • (Boolean)


55
56
57
# File 'lib/ruby/rego/result.rb', line 55

def undefined?
  value.is_a?(UndefinedValue)
end