Module: Smplkit::Flags::JsonLogicEvaluator

Defined in:
lib/smplkit/flags/client.rb

Overview

Vendored minimal JSON Logic evaluator covering the operators the smplkit platform ships in flag rules.

Stays in-tree so the Ruby SDK doesn’t depend on the json_logic gem being correct — the Java SDK followed the same pattern. Operators supported: ==, !=, <, <=, >, >=, in, var, and, or, !, if, missing, none.

Class Method Summary collapse

Class Method Details

.apply(logic, data) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/smplkit/flags/client.rb', line 393

def apply(logic, data)
  return logic unless logic.is_a?(Hash)
  return logic if logic.empty?

  op, values = logic.first
  values = [values] unless values.is_a?(Array)

  case op
  when "var"
    resolve_var(values[0], data, values[1])
  when "and"
    values.reduce(true) { |acc, v| acc && truthy?(apply(v, data)) }
  when "or"
    values.reduce(false) { |acc, v| acc || truthy?(apply(v, data)) }
  when "!"
    !truthy?(apply(values[0], data))
  when "if"
    eval_if(values, data)
  when "==", "==="
    apply(values[0], data) == apply(values[1], data)
  when "!=", "!=="
    apply(values[0], data) != apply(values[1], data)
  when "<"
    compare(values, data) { |a, b| a < b }
  when "<="
    compare(values, data) { |a, b| a <= b }
  when ">"
    compare(values, data) { |a, b| a > b }
  when ">="
    compare(values, data) { |a, b| a >= b }
  when "in"
    eval_in(values, data)
  when "missing"
    eval_missing(values, data)
  when "none"
    values_arr = apply(values[0], data) || []
    inner = values[1]
    values_arr.is_a?(Array) && values_arr.none? { |item| truthy?(apply(inner, item)) }
  else
    false
  end
end

.compare(values, data) ⇒ Object



463
464
465
466
467
468
469
470
471
472
473
474
475
476
# File 'lib/smplkit/flags/client.rb', line 463

def compare(values, data)
  applied = values.map { |v| apply(v, data) }
  return false if applied.any?(&:nil?)

  if applied.length == 2
    yield(applied[0], applied[1])
  elsif applied.length == 3
    yield(applied[0], applied[1]) && yield(applied[1], applied[2])
  else
    false
  end
rescue ArgumentError, TypeError
  false
end

.eval_if(values, data) ⇒ Object



478
479
480
481
482
483
484
485
486
# File 'lib/smplkit/flags/client.rb', line 478

def eval_if(values, data)
  i = 0
  while i + 1 < values.length
    return apply(values[i + 1], data) if truthy?(apply(values[i], data))

    i += 2
  end
  i < values.length ? apply(values[i], data) : nil
end

.eval_in(values, data) ⇒ Object



488
489
490
491
492
493
494
495
496
# File 'lib/smplkit/flags/client.rb', line 488

def eval_in(values, data)
  needle = apply(values[0], data)
  haystack = apply(values[1], data)
  return false if haystack.nil?

  haystack.include?(needle)
rescue NoMethodError, TypeError
  false
end

.eval_missing(values, data) ⇒ Object



498
499
500
501
# File 'lib/smplkit/flags/client.rb', line 498

def eval_missing(values, data)
  keys = values.is_a?(Array) ? values.flatten : [values]
  keys.reject { |k| present?(resolve_var(k, data)) }
end

.present?(value) ⇒ Boolean

Returns:

  • (Boolean)


503
504
505
# File 'lib/smplkit/flags/client.rb', line 503

def present?(value)
  !(value.nil? || value == "")
end

.resolve_var(path, data, default = nil) ⇒ Object



446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/smplkit/flags/client.rb', line 446

def resolve_var(path, data, default = nil)
  return data if path.nil? || path == "" || path == []

  keys = path.is_a?(Array) ? path : path.to_s.split(".")
  keys.reduce(data) do |scope, key|
    break default if scope.nil?

    if scope.is_a?(Hash)
      scope[key] || scope[key.to_s] || scope[key.to_sym]
    elsif scope.is_a?(Array) && key.to_s =~ /\A\d+\z/
      scope[key.to_i]
    else
      default
    end
  end || default
end

.truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


436
437
438
439
440
441
442
443
444
# File 'lib/smplkit/flags/client.rb', line 436

def truthy?(value)
  return false if value.nil?
  return false if value == false
  return false if value.is_a?(Numeric) && value.zero?
  return false if value == ""
  return false if value == []

  true
end