Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow symbolic operation in logic hashes #8

Merged
merged 1 commit into from
Oct 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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