From 42f23c382cc66c910e76989664f8a1b51f1a95b0 Mon Sep 17 00:00:00 2001 From: liufengyun Date: Mon, 11 Nov 2013 18:54:45 +0800 Subject: [PATCH] support keys like $..book[?(@.price > 20)].price --- lib/jsonpath/enumerable.rb | 8 ++++---- test/test_jsonpath.rb | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/jsonpath/enumerable.rb b/lib/jsonpath/enumerable.rb index c4d985d..89b3702 100644 --- a/lib/jsonpath/enumerable.rb +++ b/lib/jsonpath/enumerable.rb @@ -33,10 +33,10 @@ def each(context = @object, key = nil, pos = 0, &blk) case node when Hash, Array (node.is_a?(Hash) ? node.keys : (0..node.size)).each do |e| - each(node, e, pos + 1) { |n| - @_current_node = n - yield n if process_function_or_literal(sub_path[1, sub_path.size - 1]) - } + @_current_node = node[e] + if process_function_or_literal(sub_path[1, sub_path.size - 1]) + each(@_current_node, nil, pos + 1, &blk) + end end else yield node if process_function_or_literal(sub_path[1, sub_path.size - 1]) diff --git a/test/test_jsonpath.rb b/test/test_jsonpath.rb index 50e4e81..40667b7 100644 --- a/test/test_jsonpath.rb +++ b/test/test_jsonpath.rb @@ -148,6 +148,10 @@ def test_support_filter_by_childnode_value_with_inconsistent_children assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@.price > 20)]").on(@object) end + def test_support_filter_by_childnode_value_and_select_child_key + assert_equal [23], JsonPath.new("$..book[?(@.price > 20)].price").on(@object) + end + def example_object { "store"=> { "book" => [