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.



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
975
976
977
# File 'lib/smplkit/flags/client.rb', line 936

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.



1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
# File 'lib/smplkit/flags/client.rb', line 1006

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.



1021
1022
1023
1024
1025
1026
1027
1028
1029
# File 'lib/smplkit/flags/client.rb', line 1021

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.



1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/smplkit/flags/client.rb', line 1031

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.



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

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)


1046
1047
1048
# File 'lib/smplkit/flags/client.rb', line 1046

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.



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

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)


979
980
981
982
983
984
985
986
987
# File 'lib/smplkit/flags/client.rb', line 979

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