json-logic-rb 0.1.1.beta2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6eafce983f8ec5d721d323a038d0e2c739d9e616069ec703847cd9a010bae2a
4
- data.tar.gz: 1a57bc686fab6a2cee116c072b62eb07a6856faa19f04b1ee1f3eb8b66ceaa8e
3
+ metadata.gz: 49483cded1408b390afa16c3b95a5c6ef8bc54052f3295ec8c9bc6ff80ae1c48
4
+ data.tar.gz: 3e98022c63ae218d3410696ab664bc71da00e282c04c8c63535d03c819bfe831
5
5
  SHA512:
6
- metadata.gz: a4afb877d45472c538008605048a0ba09800cb14f2c85336ffb4ee31093a7e8a08a5df7770091b58913009eb77bd88abf8a127621654e841b2eb3af47883333b
7
- data.tar.gz: 16650f3a7ec8cbcce5dc64e7fb634c269b4c95361ead60dfaa07efda47332704bf0ac0a660b6f7d4acfe2a0078955dff18367cb1958173a9643c6043bb14b063
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
- truthy?(JsonLogic.apply(rule_applied_to_each_item, item))
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 truthy?(last)
12
+ return last unless !!last
11
13
  end
12
14
  last
13
15
  end
@@ -1,9 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using JsonLogic::Semantics
3
4
  class JsonLogic::Operations::BoolCast < JsonLogic::Operation
4
5
  def self.op_name = "!!"
5
6
 
6
7
  def call((a), _data)
7
- truthy?(a)
8
+ !!a
8
9
  end
9
10
  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
- truthy?(JsonLogic.apply(rule_applied_to_each_item, item))
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 truthy?(JsonLogic.apply(args[i], data))
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
- truthy?(JsonLogic.apply(rule_applied_to_each_item, item))
11
+ !!(JsonLogic.apply(rule_applied_to_each_item, item))
10
12
  end
11
13
  end
12
14
  end
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ using JsonLogic::Semantics
4
+
3
5
  class JsonLogic::Operations::Not < JsonLogic::Operation
4
6
  def self.op_name = "!";
5
7
 
6
- def call((a), _data) = !truthy?(a)
8
+ def call((a), _data) = !a
7
9
  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 truthy?(v)
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
- truthy?(JsonLogic.apply(rule_applied_to_each_item, item))
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::StrictNotEqual < JsonLogic::Operation
4
6
  def self.op_name = "!=="
5
7
 
@@ -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 truthy?(JsonLogic.apply(cond_rule, data))
9
+ if !!JsonLogic.apply(cond_rule, data)
8
10
  JsonLogic.apply(then_rule, data)
9
11
  else
10
12
  JsonLogic.apply(else_rule, data)
@@ -6,11 +6,18 @@ module JsonLogic
6
6
 
7
7
  def truthy?(v)
8
8
  case v
9
- when nil then false
10
- when TrueClass, FalseClass then v
11
- when Numeric then !v.zero?
12
- when String, Array then !v.empty?
13
- else true
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
- Float(s) rescue Float::NAN
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
- return false if a.is_a?(Numeric) && (a.to_f.nan? || b.to_f.nan?)
36
- return a.is_a?(Numeric) ? (a.to_f == b.to_f) : a.eql?(b)
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 == true || a == false || b == true || b == false
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
- [String, Integer, TrueClass, FalseClass, NilClass, Array].each do |klass|
65
- refine klass do
66
- def ==(other) = JsonLogic::Semantics.eq(self, other)
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
- def >=(other)
74
- c = JsonLogic::Semantics.cmp(self, other)
75
- c && (c == 1 || c == 0)
76
- end
84
+ def to_bool
85
+ JsonLogic::Semantics.truthy?(self)
86
+ end
87
+ end
77
88
 
78
- def <(other)
79
- c = JsonLogic::Semantics.cmp(self, other)
80
- c && c == -1
81
- end
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
- def <=(other)
84
- c = JsonLogic::Semantics.cmp(self, other)
85
- c && (c == -1 || c == 0)
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
@@ -1,3 +1,3 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module JsonLogic; VERSION = '0.1.1.beta2'; end
3
+ module JsonLogic; VERSION = '0.1.1.beta3'; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-logic-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.beta2
4
+ version: 0.1.1.beta3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tavrel Kate