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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 331a993f35775659005673c1627c25e9a6a9fba1b6ae457d56643c0dd19ea3ce
4
- data.tar.gz: 429fa17806f595566ec659c8209c60ed1cc74d7cdecafeaa10865a385ebb6b58
3
+ metadata.gz: c7c2e3086fde610813f6f857c8ce69fc592395f426d9dd4e9815b825675906a3
4
+ data.tar.gz: f59c1206313359a7bb1f79f1f3913c5d05a51b1797374de9db7e8fb5071779d9
5
5
  SHA512:
6
- metadata.gz: 5fb1dbd4b674963ff19754b60814e7a4ccf37db4bdb31b6d7a664ae0c48fc4244e7b4e2d87f3145a57b3d05d1f86b93ddc5feecd904b63b1efa8df4f4bd1a2b4
7
- data.tar.gz: 5f8560cf10d8a22a86905913a111533a5b11d73d8e473525c33e624ee9fba99ecc36c1b06496547eaeec187557ec6eb9b30e6796da66c4463ec2f06410c5e5d8
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!(var_name, operator, data, rules, 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
@@ -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
- var_name = get_var_name(operator, rules)
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
@@ -1,8 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonLogic
4
- COMPLEX_OPERATORS = %w[and or if].freeze
5
-
6
4
  OPERATIONS = {
7
5
  '==' => ->(a, b) { a == b },
8
6
  '!=' => ->(a, b) { a != b },
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonLogic
4
- VERSION = '0.2.3'
4
+ VERSION = '0.2.4'
5
5
  end
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.3
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-07 00:00:00.000000000 Z
12
+ date: 2024-03-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport