Skip to content

Commit

Permalink
Merge pull request #8 from barthez/master
Browse files Browse the repository at this point in the history
Allow symbolic operation in logic hashes
  • Loading branch information
bhgames authored Oct 22, 2018
2 parents 54657b5 + 14f6384 commit 8ce53e0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/json_logic/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/json_logic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8ce53e0

Please sign in to comment.