json_logic_ruby 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/json_logic/concerns/trackable.rb +22 -1
- data/lib/json_logic/evaluator.rb +3 -21
- data/lib/json_logic/operations.rb +0 -2
- data/lib/json_logic/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c7c2e3086fde610813f6f857c8ce69fc592395f426d9dd4e9815b825675906a3
|
4
|
+
data.tar.gz: f59c1206313359a7bb1f79f1f3913c5d05a51b1797374de9db7e8fb5071779d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ed81d16120716c5c24ebfda1b9b8b53ebb7125af6a72fdcee7f6885810c442c767447d2515a7277c493c9d89b3092e4060c74016e10309fb6e56f2036fb896a
|
7
|
+
data.tar.gz: 748f0f643b065edcd85920b5001ff8f426fb0e30bcb903c2bc6da7d24c9d6d535563862286b08669dbad95c5f5a5e1a5b25d06e06328b18d64e37da3dba043ca
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module JsonLogic
|
4
4
|
module Trackable
|
5
|
+
COMPLEX_OPERATORS = %w[and or if ! !! ?:].freeze
|
6
|
+
|
5
7
|
attr_reader :tracker
|
6
8
|
|
7
9
|
def init_tracker(operator)
|
@@ -12,15 +14,34 @@ module JsonLogic
|
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
15
|
-
def commit_rule_result!(
|
17
|
+
def commit_rule_result!(operator, data, rules, result)
|
16
18
|
if COMPLEX_OPERATORS.include?(operator)
|
17
19
|
# change operand to parent & save result
|
18
20
|
@tracker.result = result
|
19
21
|
@tracker = @tracker.parent unless @tracker.parent.nil?
|
20
22
|
return result
|
21
23
|
end
|
24
|
+
var_name = get_var_name(operator, rules)
|
22
25
|
@tracker.add_data_point(var_name, operator, rules, data, result)
|
23
26
|
result
|
24
27
|
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# This method retrieves the variable name from a hash of rules based on the given operator.
|
32
|
+
# { "<=" : [ 25, { "var": "age" }, 75] }
|
33
|
+
# { "<=" : [ { "var" : "age" }, 20 ] }
|
34
|
+
#
|
35
|
+
# @param operator [String] The operator for which to retrieve the variable name.
|
36
|
+
# @param rules [Hash] A hash containing rule data.
|
37
|
+
# @return [String, nil] The variable name if found, otherwise nil.
|
38
|
+
|
39
|
+
def get_var_name(operator, rules)
|
40
|
+
args = rules[operator]
|
41
|
+
index = COMPLEX_OPERATORS.exclude?(operator) && args.length == 3 ? 1 : 0
|
42
|
+
args.dig(index, 'var')
|
43
|
+
rescue TypeError
|
44
|
+
nil
|
45
|
+
end
|
25
46
|
end
|
26
47
|
end
|
data/lib/json_logic/evaluator.rb
CHANGED
@@ -5,12 +5,12 @@ module JsonLogic
|
|
5
5
|
include Trackable
|
6
6
|
|
7
7
|
def apply(rules, data = {})
|
8
|
+
return rules.map { |val| apply(val, data) } if rules.is_a?(Array)
|
8
9
|
return rules unless rules.is_a?(Hash)
|
9
10
|
|
10
11
|
operator = rules.keys[0]
|
11
12
|
init_tracker(operator)
|
12
|
-
|
13
|
-
values = operator == 'map' ? [] : Array(rules[operator]).map { |rule| apply(rule, data) }
|
13
|
+
values = operator == 'map' ? [] : apply(rules[operator], data)
|
14
14
|
|
15
15
|
operators(operator, values, rules, data)
|
16
16
|
end
|
@@ -117,9 +117,7 @@ module JsonLogic
|
|
117
117
|
|
118
118
|
def execute_operation(operator, rules, data, *)
|
119
119
|
result = OPERATIONS[operator].call(*)
|
120
|
-
|
121
|
-
|
122
|
-
commit_rule_result!(var_name, operator, data, rules, result)
|
120
|
+
commit_rule_result!(operator, data, rules, result)
|
123
121
|
end
|
124
122
|
|
125
123
|
# This method retrieves the value of a variable with a given name from the data structure.
|
@@ -137,22 +135,6 @@ module JsonLogic
|
|
137
135
|
data.nil? ? default_value : data
|
138
136
|
end
|
139
137
|
|
140
|
-
# This method retrieves the variable name from a hash of rules based on the given operator.
|
141
|
-
# { "<=" : [ 25, { "var": "age" }, 75] }
|
142
|
-
# { "<=" : [ { "var" : "age" }, 20 ] }
|
143
|
-
#
|
144
|
-
# @param operator [String] The operator for which to retrieve the variable name.
|
145
|
-
# @param rules [Hash] A hash containing rule data.
|
146
|
-
# @return [String, nil] The variable name if found, otherwise nil.
|
147
|
-
|
148
|
-
def get_var_name(operator, rules)
|
149
|
-
args = rules[operator]
|
150
|
-
index = COMPLEX_OPERATORS.exclude?(operator) && args.length == 3 ? 1 : 0
|
151
|
-
args.dig(index, 'var')
|
152
|
-
rescue TypeError
|
153
|
-
nil
|
154
|
-
end
|
155
|
-
|
156
138
|
def missing(data, *args)
|
157
139
|
args.select { |arg| get_var_value(data, arg).nil? }
|
158
140
|
end
|
data/lib/json_logic/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_logic_ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Volodymyr Stashchenko
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-03-
|
12
|
+
date: 2024-03-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|