Class: Ruby::Rego::Result
- Inherits:
-
Object
- Object
- Ruby::Rego::Result
- Defined in:
- lib/ruby/rego/result.rb
Overview
Represents the outcome of evaluating a policy or expression.
Instance Attribute Summary collapse
-
#bindings ⇒ Hash{String => Value}
readonly
Variable bindings captured during evaluation.
-
#errors ⇒ Array<Object>
readonly
Errors collected during evaluation.
-
#success ⇒ Boolean
readonly
True when evaluation succeeded and produced a value.
-
#value ⇒ Value
readonly
Evaluated value.
Instance Method Summary collapse
-
#initialize(value:, success:, bindings: {}, errors: []) ⇒ Result
constructor
Create a result wrapper.
-
#success? ⇒ Boolean
Convenience success predicate.
-
#to_h ⇒ Hash{Symbol => Object}
Convert the result to a hash of the raw evaluated values (each via Value#to_ruby).
-
#to_json(*args) ⇒ String
Serialize the result as JSON.
-
#undefined? ⇒ Boolean
True when the value is undefined.
Constructor Details
#initialize(value:, success:, bindings: {}, errors: []) ⇒ Result
Create a result wrapper.
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
#bindings ⇒ Hash{String => Value} (readonly)
Variable bindings captured during evaluation.
19 20 21 |
# File 'lib/ruby/rego/result.rb', line 19 def bindings @bindings end |
#errors ⇒ Array<Object> (readonly)
Errors collected during evaluation.
29 30 31 |
# File 'lib/ruby/rego/result.rb', line 29 def errors @errors end |
#success ⇒ Boolean (readonly)
True when evaluation succeeded and produced a value.
24 25 26 |
# File 'lib/ruby/rego/result.rb', line 24 def success @success end |
#value ⇒ Value (readonly)
Evaluated value.
14 15 16 |
# File 'lib/ruby/rego/result.rb', line 14 def value @value end |
Instance Method Details
#success? ⇒ Boolean
Convenience success predicate.
48 49 50 |
# File 'lib/ruby/rego/result.rb', line 48 def success? success end |
#to_h ⇒ Hash{Symbol => Object}
Convert the result to a hash of the raw evaluated values (each via Value#to_ruby). This is the internal shape; it can contain values that are not directly JSON-safe — a string with invalid-UTF-8 bytes, or the undefined sentinel left inside a collection (e.g. an array element that evaluated to undefined). Use #to_json for a JSON-safe serialization (it scrubs both, matching OPA).
65 66 67 68 69 70 71 72 |
# File 'lib/ruby/rego/result.rb', line 65 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.
78 79 80 81 82 83 84 |
# File 'lib/ruby/rego/result.rb', line 78 def to_json(*args) = args.first sanitized = sanitize_json(to_h) return JSON.generate(sanitized) unless .is_a?(Hash) JSON.generate(sanitized, ) end |
#undefined? ⇒ Boolean
True when the value is undefined.
55 56 57 |
# File 'lib/ruby/rego/result.rb', line 55 def undefined? value.is_a?(UndefinedValue) end |