Skip to content

Commit

Permalink
Merge pull request #13 from eropple/interpolation-for-in-operator
Browse files Browse the repository at this point in the history
Interpolation for in operator
  • Loading branch information
bhgames authored Apr 30, 2019
2 parents 8327464 + 512c3f6 commit 3815c7d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions json_logic.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'bundler', '~> 1.13'
spec.add_development_dependency 'rake', '~> 10.0'
spec.add_development_dependency 'minitest', '~> 5.0'
spec.add_development_dependency 'pry'
end
2 changes: 1 addition & 1 deletion lib/json_logic/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Operation
'%' => ->(v, d) { v.map(&:to_i).reduce(:%) },
'^' => ->(v, d) { v.map(&:to_f).reduce(:**) },
'merge' => ->(v, d) { v.flatten },
'in' => ->(v, d) { v[1].include? v[0] },
'in' => ->(v, d) { interpolated_block(v[1], d).include? v[0] },
'cat' => ->(v, d) { v.map(&:to_s).join },
'log' => ->(v, d) { puts v }
}
Expand Down
22 changes: 22 additions & 0 deletions test/json_logic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,26 @@ def test_array_with_logic
{ "x" => true, "y" => 42}
)
end

def test_in_with_variable
assert_equal true, JSONLogic.apply(
{
"in" => [
{"var" => "x"},
{"var" => "x"}
]
},
{ "x" => "foo"}
)

assert_equal false, JSONLogic.apply(
{
"in" => [
{"var" => "x"},
{"var" => "y"},
]
},
{ "x" => "foo", "y" => "bar" }
)
end
end

0 comments on commit 3815c7d

Please sign in to comment.