From dc35ea73178f470ac50dd72ca68747221cb316f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20M=C5=82o=C5=BAniak?= Date: Tue, 30 Oct 2018 11:59:05 +0100 Subject: [PATCH 1/2] Fix Minitest depracation warning --- test/json_logic_test.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index bddfc5d..563fae1 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -15,7 +15,12 @@ class JSONLogicTest < Minitest::Test define_method("test_#{count}") do result = JSONLogic.apply(pattern[0], pattern[1]) msg = "#{pattern[0].to_json} (data: #{pattern[1].to_json})" - assert_equal(pattern[2], result, msg) + + if pattern[2].nil? + assert_nil(result, msg) + else + assert_equal(pattern[2], result, msg) + end end count += 1 end From 268cc9577a21f5381f182c3ac148855dbd0ecbcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20M=C5=82o=C5=BAniak?= Date: Tue, 30 Oct 2018 12:25:33 +0100 Subject: [PATCH 2/2] Handle arrays with logic correctly --- lib/json_logic.rb | 5 +++++ test/json_logic_test.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/json_logic.rb b/lib/json_logic.rb index 3ea00ac..914573e 100644 --- a/lib/json_logic.rb +++ b/lib/json_logic.rb @@ -4,7 +4,12 @@ require 'json_logic/operation' module JSONLogic def self.apply(logic, data) + if logic.is_a?(Array) + return logic.map { |val| apply(val, data) } + end + return logic unless logic.is_a?(Hash) # pass-thru + data = data.stringify_keys if data.is_a?(Hash) # Stringify keys to keep out problems with symbol/string mismatch operator, values = logic.first # unwrap single-key hash values = [values] unless values.is_a?(Array) # syntactic sugar diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index 563fae1..4ecfa80 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -44,4 +44,19 @@ def test_add_operation data = JSON.parse(%Q|{"num": 1}|) assert_equal([6], JSONLogic.apply(rules, data)) end + + def test_array_with_logic + assert_equal [1, 2, 3], JSONLogic.apply([1, {"var" => "x"}, 3], {"x" => 2}) + + assert_equal [42], JSONLogic.apply( + { + "if" => [ + {"var" => "x"}, + [{"var" => "y"}], + 99 + ] + }, + { "x" => true, "y" => 42} + ) + end end