Class: Quonfig::EvalResult

Inherits:
Object
  • Object
show all
Defined in:
lib/quonfig/evaluator.rb

Overview

Result of a matched config evaluation. Provides the caller with both the raw JSON Value hash (#value) and a coerced Ruby value (#unwrapped_value). The test suite and integration helpers consume both shapes.

Constant Summary collapse

REASON_UNKNOWN =

Integer reason codes for the api-telemetry EvalSummaries wire format. Match sdk-node/src/reason.ts.

0
REASON_STATIC =
1
REASON_TARGETING_MATCH =
2
REASON_SPLIT =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value:, rule_index:, config:, weighted_value_index: nil) ⇒ EvalResult

Returns a new instance of EvalResult.



422
423
424
425
426
427
# File 'lib/quonfig/evaluator.rb', line 422

def initialize(value:, rule_index:, config:, weighted_value_index: nil)
  @value = value
  @rule_index = rule_index
  @config = config
  @weighted_value_index = weighted_value_index
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



419
420
421
# File 'lib/quonfig/evaluator.rb', line 419

def config
  @config
end

#rule_indexObject (readonly)

Returns the value of attribute rule_index.



419
420
421
# File 'lib/quonfig/evaluator.rb', line 419

def rule_index
  @rule_index
end

#valueObject (readonly)

Returns the value of attribute value.



419
420
421
# File 'lib/quonfig/evaluator.rb', line 419

def value
  @value
end

#weighted_value_indexObject

Returns the value of attribute weighted_value_index.



420
421
422
# File 'lib/quonfig/evaluator.rb', line 420

def weighted_value_index
  @weighted_value_index
end

Instance Method Details

#raw_valueObject

Raw underlying value without type coercion.



461
462
463
464
465
# File 'lib/quonfig/evaluator.rb', line 461

def raw_value
  return nil if @value.nil?

  @value[:value] || @value['value']
end

#typeObject

The declared Value type (‘string’, ‘int’, ‘bool’, …). Nil if unset.



468
469
470
471
472
# File 'lib/quonfig/evaluator.rb', line 468

def type
  return nil if @value.nil?

  @value[:type] || @value['type']
end

#unwrapped_valueObject

Ruby-native value after type coercion. Mirrors sdk-node Resolver#unwrapValue.



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'lib/quonfig/evaluator.rb', line 475

def unwrapped_value
  raw = raw_value
  case type
  when 'bool'        then !!raw
  when 'int'
    return raw if raw.is_a?(Integer)
    return raw.to_i if raw.is_a?(Numeric)
    Integer(raw.to_s, 10)
  when 'double'
    return raw.to_f if raw.is_a?(Numeric)
    Float(raw.to_s)
  when 'string'      then raw.to_s
  when 'string_list' then raw.is_a?(Array) ? raw.map(&:to_s) : []
  when 'log_level'   then raw.is_a?(Numeric) ? raw : raw.to_s
  when 'duration'    then duration_to_millis(raw)
  when 'json'
    # JSON values must be native JS/Ruby types on the wire.
    raw
  else
    raw
  end
end

#value_typeObject

Convenience for callers that don’t care about coercion — mirrors the value shape sdk-node emits.



500
501
502
# File 'lib/quonfig/evaluator.rb', line 500

def value_type
  type
end

#wire_reasonObject

Integer reason code for telemetry. Mirrors sdk-node’s computeReason: SPLIT when a weighted variant is picked, STATIC when the first rule of a config with no targeting rules matched, otherwise TARGETING_MATCH.



432
433
434
435
436
437
# File 'lib/quonfig/evaluator.rb', line 432

def wire_reason
  return REASON_SPLIT unless @weighted_value_index.nil?
  return REASON_STATIC if @rule_index == 0 && !EvalResult.send(:targeting_rules?, @config)

  REASON_TARGETING_MATCH
end