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 serializable hash.
-
#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 serializable hash.
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.
75 76 77 78 79 80 |
# File 'lib/ruby/rego/result.rb', line 75 def to_json(*args) = args.first return JSON.generate(to_h) unless .is_a?(Hash) JSON.generate(to_h, ) 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 |