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
- .apply(logic, data) ⇒ Object private
- .compare(values, data) ⇒ Object private
- .eval_if(values, data) ⇒ Object private
- .eval_in(values, data) ⇒ Object private
- .eval_missing(values, data) ⇒ Object private
- .present?(value) ⇒ Boolean private
- .resolve_var(path, data, default = nil) ⇒ Object private
- .truthy?(value) ⇒ Boolean private
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.
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 |
# File 'lib/smplkit/flags/client.rb', line 877 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.
947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
# File 'lib/smplkit/flags/client.rb', line 947 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.
962 963 964 965 966 967 968 969 970 |
# File 'lib/smplkit/flags/client.rb', line 962 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.
972 973 974 975 976 977 978 979 980 |
# File 'lib/smplkit/flags/client.rb', line 972 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.
982 983 984 985 |
# File 'lib/smplkit/flags/client.rb', line 982 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.
987 988 989 |
# File 'lib/smplkit/flags/client.rb', line 987 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.
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 |
# File 'lib/smplkit/flags/client.rb', line 930 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.
920 921 922 923 924 925 926 927 928 |
# File 'lib/smplkit/flags/client.rb', line 920 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 |