json_logic_ruby 0.2.1 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7359af03a31d1bcf8fd6674e36e246bf37ab3bb7f43318c46cc5508c61ecf2b6
4
- data.tar.gz: b6cc1cfe85ff696ba67712cc858f84042882e879b35aba964c8c8324469ecb16
3
+ metadata.gz: c7c2e3086fde610813f6f857c8ce69fc592395f426d9dd4e9815b825675906a3
4
+ data.tar.gz: f59c1206313359a7bb1f79f1f3913c5d05a51b1797374de9db7e8fb5071779d9
5
5
  SHA512:
6
- metadata.gz: 83bb07087a9fd29e1c756321d6de5b2c06e133a0677217b8f1654e5fef4c559c9f890d051c5a55c141f632667e78d9031845ee39fe4852c05e55a9e580eebe87
7
- data.tar.gz: 4fc21d649a8b76a24ff07fd14b509180b181981995b675920f27d0d5e79ebb3d3743d7e2adeb6ee98b55486def40cb6b90b4d8b95e492ba77148e6a846d36052
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
@@ -96,7 +96,7 @@ module JsonLogic
96
96
  end
97
97
 
98
98
  if rules.is_a?(Hash)
99
- rules.each { |_, rule| fetch_var_values(rule, var_name, values) }
99
+ rules.each_value { |rule| fetch_var_values(rule, var_name, values) }
100
100
  return values
101
101
  end
102
102
 
@@ -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.
@@ -134,23 +132,7 @@ module JsonLogic
134
132
  rescue TypeError
135
133
  data = data[key.to_i]
136
134
  end
137
- data || default_value
138
- end
139
-
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
135
+ data.nil? ? default_value : data
154
136
  end
155
137
 
156
138
  def missing(data, *args)
@@ -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.1'
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.1
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: 2023-11-23 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
@@ -27,7 +27,9 @@ dependencies:
27
27
  version: '7.0'
28
28
  description: Build complex rules, serialize them as JSON, and execute them in ruby.
29
29
  See https://jsonlogic.com
30
- email:
30
+ email:
31
+ - stashchenko@ukr.net
32
+ - savka.ai2015@gmail.com
31
33
  executables: []
32
34
  extensions: []
33
35
  extra_rdoc_files: []
@@ -39,7 +41,7 @@ files:
39
41
  - lib/json_logic/rule.rb
40
42
  - lib/json_logic/validator.rb
41
43
  - lib/json_logic/version.rb
42
- homepage: https://github.com/useful-libs/json_logic
44
+ homepage: https://github.com/useful-libs/json_logic_ruby
43
45
  licenses:
44
46
  - MIT
45
47
  metadata: