json-logic-rb 0.1.1.beta1 → 0.1.1.beta3
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/operations/all.rb +3 -1
- data/lib/json_logic/operations/and.rb +3 -1
- data/lib/json_logic/operations/bool_cast.rb +2 -1
- data/lib/json_logic/operations/filter.rb +3 -1
- data/lib/json_logic/operations/if.rb +3 -1
- data/lib/json_logic/operations/none.rb +3 -1
- data/lib/json_logic/operations/not.rb +3 -1
- data/lib/json_logic/operations/or.rb +3 -1
- data/lib/json_logic/operations/reduce.rb +1 -1
- data/lib/json_logic/operations/some.rb +3 -1
- data/lib/json_logic/operations/strict_not_equal.rb +2 -0
- data/lib/json_logic/operations/ternary.rb +3 -1
- data/lib/json_logic/semantics.rb +44 -29
- 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: 49483cded1408b390afa16c3b95a5c6ef8bc54052f3295ec8c9bc6ff80ae1c48
|
|
4
|
+
data.tar.gz: 3e98022c63ae218d3410696ab664bc71da00e282c04c8c63535d03c819bfe831
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c0451ee54551025d9c56d0b92e3211f2135fdcf248714915278f4f9087f8bf862db657f5467902d6b26ea12497d5055c4390fac1dc668f0cc34f660a9db044f1
|
|
7
|
+
data.tar.gz: ce9bea35b50f696dc41b57dd100ea686104094c850114d7b0a2658900dfa56c1a9b34fa5e2a9672cccc25f6f3d6a22db456de8c023aa46d3f099a22a3d5e45b8
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::All < JsonLogic::EnumerableOperation
|
|
4
6
|
def self.op_name = "all"
|
|
5
7
|
|
|
@@ -8,7 +10,7 @@ class JsonLogic::Operations::All < JsonLogic::EnumerableOperation
|
|
|
8
10
|
return false if items.empty?
|
|
9
11
|
|
|
10
12
|
items.all? do |item|
|
|
11
|
-
|
|
13
|
+
!!JsonLogic.apply(rule_applied_to_each_item, item)
|
|
12
14
|
end
|
|
13
15
|
end
|
|
14
16
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::And < JsonLogic::LazyOperation
|
|
4
6
|
def self.op_name = "and"
|
|
5
7
|
|
|
@@ -7,7 +9,7 @@ class JsonLogic::Operations::And < JsonLogic::LazyOperation
|
|
|
7
9
|
last = nil
|
|
8
10
|
args.each do |a|
|
|
9
11
|
last = JsonLogic.apply(a, data)
|
|
10
|
-
return last unless
|
|
12
|
+
return last unless !!last
|
|
11
13
|
end
|
|
12
14
|
last
|
|
13
15
|
end
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::Filter < JsonLogic::EnumerableOperation
|
|
4
6
|
def self.op_name = "filter"
|
|
5
7
|
|
|
@@ -7,7 +9,7 @@ class JsonLogic::Operations::Filter < JsonLogic::EnumerableOperation
|
|
|
7
9
|
items, rule_applied_to_each_item = resolve_items_and_per_item_rule(args, data)
|
|
8
10
|
|
|
9
11
|
items.filter do |item|
|
|
10
|
-
|
|
12
|
+
!!JsonLogic.apply(rule_applied_to_each_item, item)
|
|
11
13
|
end
|
|
12
14
|
end
|
|
13
15
|
end
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::If < JsonLogic::LazyOperation
|
|
4
6
|
def self.op_name = "if"
|
|
5
7
|
|
|
6
8
|
def call(args, data)
|
|
7
9
|
i = 0
|
|
8
10
|
while i < args.size - 1
|
|
9
|
-
return JsonLogic.apply(args[i + 1], data) if
|
|
11
|
+
return JsonLogic.apply(args[i + 1], data) if !!(JsonLogic.apply(args[i], data))
|
|
10
12
|
i += 2
|
|
11
13
|
end
|
|
12
14
|
return JsonLogic.apply(args[-1], data) if args.size.odd?
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::None < JsonLogic::EnumerableOperation
|
|
4
6
|
def self.op_name = "none"
|
|
5
7
|
|
|
6
8
|
def call(args, data)
|
|
7
9
|
items, rule_applied_to_each_item = resolve_items_and_per_item_rule(args, data)
|
|
8
10
|
items.none? do |item|
|
|
9
|
-
|
|
11
|
+
!!(JsonLogic.apply(rule_applied_to_each_item, item))
|
|
10
12
|
end
|
|
11
13
|
end
|
|
12
14
|
end
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
using JsonLogic::Semantics
|
|
2
4
|
class JsonLogic::Operations::Or < JsonLogic::LazyOperation
|
|
3
5
|
def self.op_name = "or"
|
|
4
6
|
|
|
5
7
|
def call(args, data)
|
|
6
8
|
args.each do |a|
|
|
7
9
|
v = JsonLogic.apply(a, data)
|
|
8
|
-
return v if
|
|
10
|
+
return v if !!v
|
|
9
11
|
end
|
|
10
12
|
|
|
11
13
|
args.empty? ? nil : JsonLogic.apply(args.last, data)
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: tru
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::Some < JsonLogic::EnumerableOperation
|
|
4
6
|
def self.op_name = "some"
|
|
5
7
|
|
|
@@ -8,7 +10,7 @@ class JsonLogic::Operations::Some < JsonLogic::EnumerableOperation
|
|
|
8
10
|
return false if items.empty?
|
|
9
11
|
|
|
10
12
|
items.any? do |item|
|
|
11
|
-
|
|
13
|
+
!!JsonLogic.apply(rule_applied_to_each_item, item)
|
|
12
14
|
end
|
|
13
15
|
end
|
|
14
16
|
end
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
using JsonLogic::Semantics
|
|
4
|
+
|
|
3
5
|
class JsonLogic::Operations::Ternary < JsonLogic::LazyOperation
|
|
4
6
|
def self.op_name = "?:"
|
|
5
7
|
|
|
6
8
|
def call((cond_rule, then_rule, else_rule), data)
|
|
7
|
-
if
|
|
9
|
+
if !!JsonLogic.apply(cond_rule, data)
|
|
8
10
|
JsonLogic.apply(then_rule, data)
|
|
9
11
|
else
|
|
10
12
|
JsonLogic.apply(else_rule, data)
|
data/lib/json_logic/semantics.rb
CHANGED
|
@@ -6,11 +6,18 @@ module JsonLogic
|
|
|
6
6
|
|
|
7
7
|
def truthy?(v)
|
|
8
8
|
case v
|
|
9
|
-
when nil
|
|
10
|
-
|
|
11
|
-
when
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
when nil
|
|
10
|
+
false
|
|
11
|
+
when TrueClass, FalseClass
|
|
12
|
+
v
|
|
13
|
+
when Numeric
|
|
14
|
+
v.zero? ? false : true
|
|
15
|
+
when String
|
|
16
|
+
v.empty? ? false : true
|
|
17
|
+
when Array
|
|
18
|
+
v.empty? ? false : true
|
|
19
|
+
else
|
|
20
|
+
true
|
|
14
21
|
end
|
|
15
22
|
end
|
|
16
23
|
|
|
@@ -24,7 +31,11 @@ module JsonLogic
|
|
|
24
31
|
when String
|
|
25
32
|
s = v.strip
|
|
26
33
|
return 0.0 if s.empty?
|
|
27
|
-
|
|
34
|
+
begin
|
|
35
|
+
Float(s)
|
|
36
|
+
rescue ArgumentError
|
|
37
|
+
Float::NAN
|
|
38
|
+
end
|
|
28
39
|
else
|
|
29
40
|
Float::NAN
|
|
30
41
|
end
|
|
@@ -32,11 +43,15 @@ module JsonLogic
|
|
|
32
43
|
|
|
33
44
|
def eq(a, b)
|
|
34
45
|
if a.class == b.class
|
|
35
|
-
|
|
36
|
-
|
|
46
|
+
if a.is_a?(Numeric)
|
|
47
|
+
return false if a.to_f.nan? || b.to_f.nan?
|
|
48
|
+
return a.to_f == b.to_f
|
|
49
|
+
else
|
|
50
|
+
return a.eql?(b)
|
|
51
|
+
end
|
|
37
52
|
end
|
|
38
53
|
|
|
39
|
-
if a
|
|
54
|
+
if a.is_a?(TrueClass) || a.is_a?(FalseClass) || b.is_a?(TrueClass) || b.is_a?(FalseClass)
|
|
40
55
|
na = num(a); nb = num(b)
|
|
41
56
|
return false if na.nan? || nb.nan?
|
|
42
57
|
return na == nb
|
|
@@ -61,29 +76,29 @@ module JsonLogic
|
|
|
61
76
|
end
|
|
62
77
|
end
|
|
63
78
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
def >(other)
|
|
69
|
-
c = JsonLogic::Semantics.cmp(self, other)
|
|
70
|
-
c && c == 1
|
|
71
|
-
end
|
|
79
|
+
refine Object do
|
|
80
|
+
def !@
|
|
81
|
+
JsonLogic::Semantics.truthy?(self) ? false : true
|
|
82
|
+
end
|
|
72
83
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
84
|
+
def to_bool
|
|
85
|
+
JsonLogic::Semantics.truthy?(self)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
77
88
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
89
|
+
[String, Integer, Float].each do |klass|
|
|
90
|
+
refine klass do
|
|
91
|
+
def ==(other) = JsonLogic::Semantics.eq(self, other)
|
|
92
|
+
def >(other) = (c = JsonLogic::Semantics.cmp(self, other)) && c == 1
|
|
93
|
+
def >=(other) = (c = JsonLogic::Semantics.cmp(self, other)) && (c == 1 || c == 0)
|
|
94
|
+
def <(other) = (c = JsonLogic::Semantics.cmp(self, other)) && c == -1
|
|
95
|
+
def <=(other) = (c = JsonLogic::Semantics.cmp(self, other)) && (c == -1 || c == 0)
|
|
96
|
+
end
|
|
97
|
+
end
|
|
82
98
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
end
|
|
99
|
+
[Array, TrueClass, FalseClass, NilClass].each do |klass|
|
|
100
|
+
refine klass do
|
|
101
|
+
def ==(other) = JsonLogic::Semantics.eq(self, other)
|
|
87
102
|
end
|
|
88
103
|
end
|
|
89
104
|
end
|
data/lib/json_logic/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: json-logic-rb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.1.
|
|
4
|
+
version: 0.1.1.beta3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Tavrel Kate
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2025-10-
|
|
11
|
+
date: 2025-10-31 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: Ruby implementation of JsonLogic. JsonLogic rules are JSON trees. The
|
|
14
14
|
engine walks that tree and returns a Ruby value. Ships with a compliance runner
|