Module: Smplkit::Flags::JsonLogicEvaluator Private

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

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
# File 'lib/smplkit/flags/client.rb', line 933

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
# File 'lib/smplkit/flags/client.rb', line 1003

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1018
1019
1020
1021
1022
1023
1024
1025
1026
# File 'lib/smplkit/flags/client.rb', line 1018

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1028
1029
1030
1031
1032
1033
1034
1035
1036
# File 'lib/smplkit/flags/client.rb', line 1028

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



1038
1039
1040
1041
# File 'lib/smplkit/flags/client.rb', line 1038

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


1043
1044
1045
# File 'lib/smplkit/flags/client.rb', line 1043

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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'lib/smplkit/flags/client.rb', line 986

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


976
977
978
979
980
981
982
983
984
# File 'lib/smplkit/flags/client.rb', line 976

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