json-logic-rb 0.1.2 → 0.1.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/semantics.rb +31 -17
- 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: e78d982fcb2d524f9cf465fe79b6248a4b0db63b188059e712ec2eaeac4f92fe
|
|
4
|
+
data.tar.gz: d3088cd8e398155b9d94e5c9c34435b4af1fcd0c7ba71f6aedb7333802372218
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c960976bb1311c6ff7528464a62a7e655702bca7565c9594fa11a1b103c84f3beb045934c387adde643d9f21fbf4daeaf4c46f6e55a96e5866f95b5b86163da3
|
|
7
|
+
data.tar.gz: f623b9be9b861890f73d7284050b8fd26debfc4d928495ed5ef406ccc0d4abbc7cec93ddf23485bcd199f0b85f7cec37cba0232ffa8589db429d80c28c5d6ae8
|
data/lib/json_logic/semantics.rb
CHANGED
|
@@ -21,13 +21,20 @@ module JsonLogic
|
|
|
21
21
|
end
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
+
def to_primitive(v)
|
|
25
|
+
case v
|
|
26
|
+
when Array then v.join(',')
|
|
27
|
+
else v
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
24
31
|
def num(v)
|
|
25
32
|
case v
|
|
26
33
|
when Numeric then v.to_f
|
|
27
34
|
when TrueClass then 1.0
|
|
28
35
|
when FalseClass then 0.0
|
|
29
36
|
when NilClass then 0.0
|
|
30
|
-
when Array then num(v
|
|
37
|
+
when Array then num(to_primitive(v))
|
|
31
38
|
when String
|
|
32
39
|
s = v.strip
|
|
33
40
|
return 0.0 if s.empty?
|
|
@@ -44,23 +51,36 @@ module JsonLogic
|
|
|
44
51
|
def eq(a, b)
|
|
45
52
|
if a.class == b.class
|
|
46
53
|
if a.is_a?(Numeric)
|
|
47
|
-
|
|
48
|
-
return
|
|
54
|
+
ax = a.to_f; bx = b.to_f
|
|
55
|
+
return false if ax.nan? || bx.nan?
|
|
56
|
+
return ax == bx
|
|
49
57
|
else
|
|
50
58
|
return a.eql?(b)
|
|
51
59
|
end
|
|
52
60
|
end
|
|
53
61
|
|
|
54
|
-
if a.
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
return
|
|
62
|
+
if a.nil? && b.nil?
|
|
63
|
+
return true
|
|
64
|
+
elsif a.nil? || b.nil?
|
|
65
|
+
return false
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
if a.is_a?(TrueClass) || a.is_a?(FalseClass)
|
|
69
|
+
return eq(num(a), b)
|
|
70
|
+
end
|
|
71
|
+
if b.is_a?(TrueClass) || b.is_a?(FalseClass)
|
|
72
|
+
return eq(a, num(b))
|
|
58
73
|
end
|
|
59
74
|
|
|
60
75
|
if (a.is_a?(String) && b.is_a?(Numeric)) || (a.is_a?(Numeric) && b.is_a?(String))
|
|
61
|
-
|
|
62
|
-
return false if
|
|
63
|
-
return
|
|
76
|
+
ax = num(a); bx = num(b)
|
|
77
|
+
return false if ax.nan? || bx.nan?
|
|
78
|
+
return ax == bx
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
if (a.is_a?(Array) && (b.is_a?(String) || b.is_a?(Numeric))) ||
|
|
82
|
+
(b.is_a?(Array) && (a.is_a?(String) || a.is_a?(Numeric)))
|
|
83
|
+
return eq(to_primitive(a), to_primitive(b))
|
|
64
84
|
end
|
|
65
85
|
|
|
66
86
|
false
|
|
@@ -86,7 +106,7 @@ module JsonLogic
|
|
|
86
106
|
end
|
|
87
107
|
end
|
|
88
108
|
|
|
89
|
-
[String, Integer, Float].each do |klass|
|
|
109
|
+
[String, Integer, Float, NilClass, Array, TrueClass, FalseClass].each do |klass|
|
|
90
110
|
refine klass do
|
|
91
111
|
def ==(other) = JsonLogic::Semantics.eq(self, other)
|
|
92
112
|
def >(other) = (c = JsonLogic::Semantics.cmp(self, other)) && c == 1
|
|
@@ -95,11 +115,5 @@ module JsonLogic
|
|
|
95
115
|
def <=(other) = (c = JsonLogic::Semantics.cmp(self, other)) && (c == -1 || c == 0)
|
|
96
116
|
end
|
|
97
117
|
end
|
|
98
|
-
|
|
99
|
-
[Array, TrueClass, FalseClass, NilClass].each do |klass|
|
|
100
|
-
refine klass do
|
|
101
|
-
def ==(other) = JsonLogic::Semantics.eq(self, other)
|
|
102
|
-
end
|
|
103
|
-
end
|
|
104
118
|
end
|
|
105
119
|
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.
|
|
4
|
+
version: 0.1.4
|
|
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-
|
|
11
|
+
date: 2025-11-03 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
|