From 14f63842573f05a99780ee8077a78c8d0136afc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Bu=C5=82at?= Date: Wed, 10 Oct 2018 10:56:12 +0200 Subject: [PATCH] Allow symbolic operation in logic hashes --- lib/json_logic/operation.rb | 4 ++-- test/json_logic_test.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/json_logic/operation.rb b/lib/json_logic/operation.rb index 3257591..0afa014 100644 --- a/lib/json_logic/operation.rb +++ b/lib/json_logic/operation.rb @@ -118,13 +118,13 @@ def self.perform(operator, values, data) end def self.is_standard?(operator) - LAMBDAS.keys.include?(operator) + LAMBDAS.key?(operator.to_s) end # Determine if values associated with operator need to be re-interpreted for each iteration(ie some kind of iterator) # or if values can just be evaluated before passing in. def self.is_iterable?(operator) - ['filter', 'some', 'all', 'none', 'in', 'map', 'reduce'].any? { |o| o == operator } + ['filter', 'some', 'all', 'none', 'in', 'map', 'reduce'].include?(operator.to_s) end def self.add_operation(operator, function) diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index f926062..bddfc5d 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -26,6 +26,12 @@ def test_filter assert_equal([{'id' => 2}], JSONLogic.filter(filter, data)) end + def test_symbol_operation + logic = {'==': [{var: "id"}, 1]} + data = JSON.parse(%Q|{"id": 1}|) + assert_equal(true, JSONLogic.apply(logic, data)) + end + def test_add_operation new_operation = ->(v, d) { v.map { |x| x + 5 } } JSONLogic.add_operation('fives', new_operation)